disturbingly, bound values appear to actually work
[scpubgit/DX.git] / lib / DX / Action / SetBoundValue.pm
1 package DX::Action::SetBoundValue;
2
3 use DX::Update::SetValue;
4 use DX::Class;
5
6 with 'DX::Role::Action';
7
8 has target_path => (is => 'ro', required => 1);
9
10 has rebind_path => (is => 'ro', required => 1);
11
12 has new_value => (is => 'ro', required => 1);
13
14 has inner_action => (is => 'ro', required => 1);
15
16 sub dry_run {
17   my ($self, $hyp) = @_;
18   my ($outer_hyp, @inner_events) = $self->inner_action->dry_run($hyp);
19   my ($scope, @events) = $outer_hyp->scope->apply_updates(
20                            DX::Update::SetValue->new(
21                              target_path => $self->target_path,
22                              new_value => $self->new_value,
23                            )
24                          );
25   my $new_bound = do {
26     my $targ = $scope;
27     $targ = $targ->get_member_at($_) for @{$self->rebind_path};
28     $targ;
29   };
30   my @actions = @{$outer_hyp->actions};
31   foreach my $idx (0.. $#actions) {
32     my $act = $actions[$idx];
33     if (
34       $act->isa('DX::Action::BindValue')
35         and join("\0", @{$act->target_path})
36               eq join("\0", @{$self->rebind_path})
37     ) {
38       my $bind = splice @actions, $idx, 1;
39       push @actions, $bind->but(new_value => $new_bound);
40       last;
41     }
42   }
43   return (
44     $outer_hyp->but(scope => $scope, actions => \@actions),
45     @inner_events, @events
46   );
47 }
48
49 sub run { die }
50
51 1;