35e423239ca3fc4a2cd606b3fb7ebbfdab9d4d1c
[scpubgit/DX.git] / lib / DX / ActionBuilder / BoundValue.pm
1 package DX::ActionBuilder::BoundValue;
2
3 use DX::Action::SetBoundValue;
4 use DX::Action::AddBoundValue;
5 use DX::Class;
6
7 with 'DX::Role::ActionBuilder';
8
9 has target_path => (is => 'ro', required => 1);
10
11 has rebind_path => (is => 'ro', required => 1);
12
13 has bound_to_path => (is => 'ro', required => 1);
14
15 has inner_action_builder => (is => 'ro', required => 1);
16
17 sub action_for_set_value {
18   my ($self, $value) = @_;
19   my $inner_action = $self->inner_action_builder->action_for_set_value($value);
20   return undef unless $inner_action;
21   DX::Action::SetBoundValue->new(
22     target_path => $self->target_path,
23     bound_to_path => $self->bound_to_path,
24     rebind_path => $self->rebind_path,
25     new_value => $value,
26     inner_action => $inner_action,
27   )
28 }
29
30 sub action_for_add_member {
31   my ($self, $at, $value) = @_;
32   $at = $at->string_value if ref($at);
33   my $inner_action = $self->inner_action_builder
34                           ->action_for_add_member($at, $value);
35   return undef unless $inner_action;
36   DX::Action::AddBoundValue->new(
37     target_path => [ @{$self->target_path}, $at ],
38     bound_to_path => [ @{$self->bound_to_path}, $at ],
39     rebind_path => $self->rebind_path,
40     new_value => $value,
41     inner_action => $inner_action
42   );
43 }
44
45 sub apply_to_value {
46   my ($self, $value, $inner_value) = @_;
47   my $new_value = $value->but_set_action_builder($self)
48                         ->but_set_identity_path($inner_value->identity_path);
49   return $new_value unless $new_value->isa('DX::Value::Dict');
50   my %m = %{$new_value->members};
51   return $new_value->but(
52     members => {
53       map {
54         my $this_inner = $inner_value->get_member_at($_);
55         ($_ => $self->but(
56                  target_path => [ @{$self->target_path}, $_ ],
57                  bound_to_path => [ @{$self->bound_to_path}, $_ ],
58                  inner_action_builder => $this_inner->action_builder,
59                )->apply_to_value($m{$_}, $this_inner))
60       } keys %m
61     },
62   );
63 }
64
65 1;