move actions to being held by fact objects
[scpubgit/DKit.git] / lib / DX / Op / ProposeAction.pm
CommitLineData
71217e42 1package DX::Op::ProposeAction;
2
3use DX::ObservationRequired;
4use Moo;
5
6with 'DX::Role::Op';
7
8has vars => (is => 'ro', required => 1);
9has builder => (is => 'ro', required => 1);
10
11has _arg_map => (is => 'lazy', builder => sub {
12 my ($self) = @_;
13 my $name = 'arg0';
14 +{ map +($name++, $_), @{$self->vars} };
15});
16
17sub run {
18 my ($self, $state) = @_;
19 ($state, my %args) = $self->_expand_args($state, %{$self->_arg_map});
20 my @vars = @args{sort keys %args};
deec7cc4 21 my @deps = $state->action_dependencies(map $_->id, @vars);
4ce2e778 22 my $action = $self->builder->(map $state->resolve_value($_), @vars)
deec7cc4 23 ->but(dependencies => \@deps);
4ce2e778 24 my ($fact_type, $value) = $action->expected_effect;
9c7b21a2 25 my $final_value = $value->but(required_action => $action);
26 my $fact_set = $state->facts->{$fact_type}->with_value($final_value);
27 $state->but(facts => { %{$state->facts}, $fact_type => $fact_set })
71217e42 28 ->then($self->next);
29}
30
311;