37b317bcb38da851e4d640750e050440ed69e69a
[scpubgit/DX.git] / lib / DX / Role / BoundValueAction.pm
1 package DX::Role::BoundValueAction;
2
3 use DX::Role;
4
5 with 'DX::Role::Action';
6
7 has target_path => (is => 'ro', required => 1);
8
9 has bound_to_path => (is => 'ro', required => 1);
10
11 has rebind_path => (is => 'ro', required => 1);
12
13 has new_value => (is => 'ro', required => 1);
14
15 has inner_action => (is => 'ro', required => 1);
16
17 requires 'update_class';
18
19 sub dry_run {
20   my ($self, $hyp) = @_;
21   my ($outer_hyp, @inner_events) = $self->inner_action->dry_run(
22     $hyp->but(
23       action_applications => [ @{$hyp->action_applications}, $self ],
24     ),
25   );
26   my $new_bound_to = do {
27     my $targ = $outer_hyp->scope;
28     $targ = $targ->get_member_at($_) for @{$self->bound_to_path};
29     $targ;
30   };
31   my $new_ab = DX::ActionBuilder::BoundValue->new(
32     target_path => $self->target_path,
33     rebind_path => $self->rebind_path,
34     bound_to_path => $self->bound_to_path,
35     inner_action_builder => $new_bound_to->action_builder
36   );
37   my $value_with_ab = $new_ab->apply_to_value($self->new_value, $new_bound_to);
38   my ($scope, @events) = $outer_hyp->scope->apply_updates(
39                            $self->update_class->new(
40                              target_path => $self->target_path,
41                              new_value => $value_with_ab,
42                            )
43                          );
44   my $new_bound = do {
45     my $targ = $scope;
46     $targ = $targ->get_member_at($_) for @{$self->rebind_path};
47     $targ;
48   };
49   my @actions = @{$outer_hyp->actions};
50   foreach my $idx (0.. $#actions) {
51     my $act = $actions[$idx];
52     if (
53       $act->isa('DX::Action::BindValue')
54         and join("\0", @{$act->target_path})
55               eq join("\0", @{$self->rebind_path})
56     ) {
57       my $bind = splice @actions, $idx, 1;
58       push @actions, $bind->but(new_value => $new_bound);
59       last;
60     }
61   }
62   return (
63     $outer_hyp->but(scope => $scope, actions => \@actions),
64     @inner_events, @events
65   );
66 }
67
68 sub run { die }
69
70 1;