Commit | Line | Data |
1e90aa03 |
1 | package DX::ActionBuilder::BoundValue; |
2 | |
3 | use DX::Action::SetBoundValue; |
5b066a1c |
4 | use DX::Action::AddBoundValue; |
1e90aa03 |
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 | |
7f385fb2 |
17 | sub can_set_value { shift->inner_action_builder->can_set_value } |
18 | |
1e90aa03 |
19 | sub action_for_set_value { |
20 | my ($self, $value) = @_; |
21 | my $inner_action = $self->inner_action_builder->action_for_set_value($value); |
22 | return undef unless $inner_action; |
23 | DX::Action::SetBoundValue->new( |
24 | target_path => $self->target_path, |
3a630a54 |
25 | bound_to_path => $self->bound_to_path, |
1e90aa03 |
26 | rebind_path => $self->rebind_path, |
3a630a54 |
27 | new_value => $value, |
1e90aa03 |
28 | inner_action => $inner_action, |
29 | ) |
30 | } |
31 | |
5b066a1c |
32 | sub action_for_add_member { |
33 | my ($self, $at, $value) = @_; |
34 | $at = $at->string_value if ref($at); |
35 | my $inner_action = $self->inner_action_builder |
36 | ->action_for_add_member($at, $value); |
37 | return undef unless $inner_action; |
38 | DX::Action::AddBoundValue->new( |
39 | target_path => [ @{$self->target_path}, $at ], |
40 | bound_to_path => [ @{$self->bound_to_path}, $at ], |
41 | rebind_path => $self->rebind_path, |
42 | new_value => $value, |
43 | inner_action => $inner_action |
44 | ); |
45 | } |
46 | |
3a630a54 |
47 | sub apply_to_value { |
48 | my ($self, $value, $inner_value) = @_; |
7a5595d3 |
49 | my $new_value = $value->but_set_action_builder($self); |
3a630a54 |
50 | return $new_value unless $new_value->isa('DX::Value::Dict'); |
51 | my %m = %{$new_value->members}; |
52 | return $new_value->but( |
53 | members => { |
54 | map { |
55 | my $this_inner = $inner_value->get_member_at($_); |
56 | ($_ => $self->but( |
57 | target_path => [ @{$self->target_path}, $_ ], |
58 | bound_to_path => [ @{$self->bound_to_path}, $_ ], |
59 | inner_action_builder => $this_inner->action_builder, |
60 | )->apply_to_value($m{$_}, $this_inner)) |
61 | } keys %m |
62 | }, |
63 | ); |
64 | } |
65 | |
1e90aa03 |
66 | 1; |