provide and preserve aperture information
[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::Utils qw(:event_types);
6 use DX::Class;
7
8 with 'DX::Role::ActionBuilder';
9
10 has target_path => (is => 'ro', required => 1);
11
12 has rebind_path => (is => 'ro', required => 1);
13
14 has bound_to_path => (is => 'ro', required => 1);
15
16 has inner_action_builder => (is => 'ro', required => 1);
17
18 sub can_set_value { shift->inner_action_builder->can_set_value }
19
20 sub aperture_for_set_value {
21   my ($self) = @_;
22   return [] unless $self->can_set_value;
23   [
24     [ VALUE_SET ,=> @{$self->target_path} ],
25     @{$self->inner_action_builder->aperture_for_set_value},
26   ]
27 }
28
29 sub action_for_set_value {
30   my ($self, $value) = @_;
31   my $inner_action = $self->inner_action_builder->action_for_set_value($value);
32   return undef unless $inner_action;
33   DX::Action::SetBoundValue->new(
34     target_path => $self->target_path,
35     bound_to_path => $self->bound_to_path,
36     rebind_path => $self->rebind_path,
37     new_value => $value,
38     inner_action => $inner_action,
39   )
40 }
41
42 sub can_add_member { shift->inner_action_builder->can_add_member }
43
44 sub aperture_for_add_member {
45   my ($self, $at) = @_;
46   return [] unless $self->can_add_member;
47   [
48     [ VALUE_SET ,=> @{$self->target_path} ],
49     @{$self->inner_action_builder->aperture_for_add_member($at)},
50   ]
51 }
52
53 sub action_for_add_member {
54   my ($self, $at, $value) = @_;
55   $at = $at->string_value if ref($at);
56   my $inner_action = $self->inner_action_builder
57                           ->action_for_add_member($at, $value);
58   return undef unless $inner_action;
59   DX::Action::AddBoundValue->new(
60     target_path => [ @{$self->target_path}, $at ],
61     bound_to_path => [ @{$self->bound_to_path}, $at ],
62     rebind_path => $self->rebind_path,
63     new_value => $value,
64     inner_action => $inner_action
65   );
66 }
67
68 sub apply_to_value {
69   my ($self, $value, $inner_value) = @_;
70   my $new_value = $value->but_set_action_builder($self);
71   return $new_value unless $new_value->isa('DX::Value::Dict');
72   my %m = %{$new_value->members};
73   return $new_value->but(
74     members => {
75       map {
76         my $this_inner = $inner_value->get_member_at($_);
77         ($_ => $self->but(
78                  target_path => [ @{$self->target_path}, $_ ],
79                  bound_to_path => [ @{$self->bound_to_path}, $_ ],
80                  inner_action_builder => $this_inner->action_builder,
81                )->apply_to_value($m{$_}, $this_inner))
82       } keys %m
83     },
84   );
85 }
86
87 1;