move actions to a separate state attribute
[scpubgit/DKit.git] / lib / DX / Op / ModifyAction.pm
1 package DX::Op::ModifyAction;
2
3 use Safe::Isa;
4 use DX::ObservationRequired;
5 use Moo;
6
7 with 'DX::Role::Op';
8
9 has vars => (is => 'ro', required => 1);
10 has builder => (is => 'ro', required => 1);
11
12 has _arg_map => (is => 'lazy', builder => sub {
13   my ($self) = @_;
14   my $name = 'arg0';
15   +{ map +($name++, $_), @{$self->vars} };
16 });
17
18 sub run {
19   my ($self, $state) = @_;
20   ($state, my %args) = $self->_expand_args($state, %{$self->_arg_map});
21   my ($subject, @vars) = @args{sort keys %args};
22   my $subject_fact = $state->resolve_value($subject);
23   die "Subject not a fact" unless $subject_fact->$_does('DX::Role::Fact');
24   die "Subject has no action" unless $subject_fact->has_required_action;
25   my $orig_action = $state->actions->{$subject_fact->required_action};
26   my @deps = $state->action_dependencies(
27     @{$orig_action->dependencies},
28     map $_->id, @vars
29   );
30   my @builder_args = (
31     $orig_action,
32     map $state->resolve_value($_), @vars
33   );
34   my $action = $self->builder->(@builder_args)
35                     ->but(dependencies => \@deps);
36   my ($fact_type, $value) = $action->expected_effect;
37   my $final_value = $value->but(required_action => $action->id);
38   my $fact_set = $state->facts->{$fact_type}->with_value($final_value);
39   $state->but(
40             facts => { %{$state->facts}, $fact_type => $fact_set },
41             actions => { %{$state->actions}, $action->id => $action },
42           )
43         ->then($self->next);
44 }
45
46 1;