move actions to a separate state attribute
[scpubgit/DKit.git] / lib / DX / Op / HasAction.pm
1 package DX::Op::HasAction;
2
3 use Safe::Isa;
4 use Moo;
5
6 has arg_spec => (is => 'ro', required => 1);
7
8 with 'DX::Role::Op';
9
10 sub run {
11   my ($self, $state) = @_;
12   my @arg_spec = @{$self->arg_spec};
13   ($state, my %vars) = $self->_expand_args(
14     $state, Thing => $arg_spec[0], Action => $arg_spec[1]
15   );
16   die "Thing must be bound" unless $vars{Thing}->is_bound;
17   my $thing = $state->resolve_value($vars{Thing});
18   if ($thing->$_does('DX::Role::Fact') and $thing->has_required_action) {
19     return $state->bind_value(
20       $vars{Action}->id,
21       $state->actions->{$thing->required_action}
22     )->add_dependencies($vars{Action}->id => $vars{Thing}->id)
23      ->then($self->next);
24   }
25   return $state->backtrack;
26 }
27
28 1;