pervasive type constraints
[scpubgit/DX.git] / lib / DX / ActionBuilder / Normal.pm
CommitLineData
efad53c4 1package DX::ActionBuilder::Normal;
2
0498469a 3use DX::Action::SetValue;
4use DX::Action::AddValue;
6aa36401 5use DX::Utils qw(:event_types);
efad53c4 6use DX::Class;
7
8with 'DX::Role::ActionBuilder';
9
2548ce61 10has target_path => (is => 'ro', required => 1, isa => ValuePath);
efad53c4 11
6aa36401 12sub aperture_for_set_value {
13 [ [ VALUE_SET ,=> @{$_[0]->target_path} ] ]
14}
15
efad53c4 16sub action_for_set_value {
17 my ($self, $value) = @_;
18 DX::Action::SetValue->new(
19 target_path => $self->target_path,
3a630a54 20 new_value => $self->apply_to_value($value),
efad53c4 21 );
22}
23
6aa36401 24sub aperture_for_add_member {
25 my ($self, $at) = @_;
26 [ [ VALUE_EXISTS ,=> @{$self->target_path}, $at ] ]
27}
28
0498469a 29sub action_for_add_member {
30 my ($self, $at, $value) = @_;
3a630a54 31 my $ab = $self->specialize_for_member($at);
0498469a 32 DX::Action::AddValue->new(
3a630a54 33 target_path => $ab->target_path,
34 new_value => $ab->apply_to_value($value),
0498469a 35 );
36}
37
38sub action_for_remove_member { die 'WHUT' }
39
3a630a54 40sub apply_to_value {
41 my ($self, $value) = @_;
7a5595d3 42 my $new_value = $value->but_set_action_builder($self);
3a630a54 43 return $new_value unless $new_value->isa('DX::Value::Dict');
44 my %m = %{$new_value->members};
45 return $new_value->but(
46 members => {
47 map +($_ => $self->specialize_for_member($_)->apply_to_value($m{$_})),
48 keys %m,
49 },
50 );
51}
52
53sub specialize_for_member {
54 my ($self, $at) = @_;
55 $self->but(
56 target_path => [
57 @{$self->target_path},
58 (ref($at) ? $at->string_value : $at)
59 ],
60 );
61}
62
efad53c4 631;