move actions to a separate state attribute
[scpubgit/DKit.git] / lib / DX / Op / ModifyAction.pm
index 57696b4..a4975b4 100644 (file)
@@ -1,5 +1,6 @@
 package DX::Op::ModifyAction;
 
+use Safe::Isa;
 use DX::ObservationRequired;
 use Moo;
 
@@ -18,8 +19,10 @@ sub run {
   my ($self, $state) = @_;
   ($state, my %args) = $self->_expand_args($state, %{$self->_arg_map});
   my ($subject, @vars) = @args{sort keys %args};
-  die "Can't alter action on $subject" unless
-    my $orig_action = $state->resolve_value($subject)->required_action;
+  my $subject_fact = $state->resolve_value($subject);
+  die "Subject not a fact" unless $subject_fact->$_does('DX::Role::Fact');
+  die "Subject has no action" unless $subject_fact->has_required_action;
+  my $orig_action = $state->actions->{$subject_fact->required_action};
   my @deps = $state->action_dependencies(
     @{$orig_action->dependencies},
     map $_->id, @vars
@@ -31,9 +34,12 @@ sub run {
   my $action = $self->builder->(@builder_args)
                     ->but(dependencies => \@deps);
   my ($fact_type, $value) = $action->expected_effect;
-  my $final_value = $value->but(required_action => $action);
+  my $final_value = $value->but(required_action => $action->id);
   my $fact_set = $state->facts->{$fact_type}->with_value($final_value);
-  $state->but(facts => { %{$state->facts}, $fact_type => $fact_set })
+  $state->but(
+            facts => { %{$state->facts}, $fact_type => $fact_set },
+            actions => { %{$state->actions}, $action->id => $action },
+          )
         ->then($self->next);
 }