Skip to content

Commit

Permalink
Merge pull request #202 from ehuelsmann/action-args
Browse files Browse the repository at this point in the history
Action args
  • Loading branch information
ehuelsmann committed Aug 6, 2023
2 parents 0ab9f2e + 284bfa5 commit cb8e055
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
44 changes: 24 additions & 20 deletions lib/Workflow.pm
Expand Up @@ -113,11 +113,12 @@ sub get_action_fields {
}

sub execute_action {
my ( $self, $action_name, $autorun ) = @_;
my ( $self, $action_name, $action_args ) = @_;

while ( $action_name ) {
my $wf_state =
$self->_execute_single_action( $action_name, $autorun );
$self->_execute_single_action( $action_name, $action_args );
$action_args = undef;

if ( not $wf_state->autorun ) {
last;
Expand Down Expand Up @@ -279,7 +280,7 @@ sub set {


sub _execute_single_action {
my ( $self, $action_name, $autorun ) = @_;
my ( $self, $action_name, $action_args ) = @_;

# This checks the conditions behind the scenes, so there's no
# explicit 'check conditions' step here
Expand All @@ -291,8 +292,12 @@ sub _execute_single_action {
my ( $new_state, $action_return );

try {
$action->validate($self);
$action->validate($self, $action_args);
$self->log->is_debug && $self->log->debug("Action validated ok");
if ($action_args) {
# Merge the action args into the context
$self->context->param( $action_args );
}
$action_return = $action->execute($self);
$self->log->is_debug && $self->log->debug("Action executed ok");

Expand Down Expand Up @@ -336,12 +341,11 @@ sub _execute_single_action {
croak $error;
}

$self->notify_observers( 'execute', $old_state, $action_name, $autorun );
$self->notify_observers( 'execute', $old_state, $action_name );

my $new_state_obj = $self->_get_workflow_state;
if ( $old_state ne $new_state ) {
$self->notify_observers( 'state change', $old_state, $action_name,
$autorun );
$self->notify_observers( 'state change', $old_state, $action_name );
}

return $new_state_obj;
Expand Down Expand Up @@ -518,10 +522,10 @@ This documentation describes version 1.57 of Workflow
my $context = $workflow->context;
$context->param( current_user => $user );
$context->param( sections => \@sections );
$context->param( path => $path_to_file );
# Execute one of them
$workflow->execute_action( 'upload file' );
$workflow->execute_action( 'upload file',
{ path => $path_to_file });
print "New state: ", $workflow->state, "\n";
Expand Down Expand Up @@ -873,22 +877,19 @@ No additional parameters.
B<execute> - Issued after a workflow is successfully executed and
saved.
Adds the parameters C<$old_state>, C<$action_name> and C<$autorun>.
Adds the parameters C<$old_state>, and C<$action_name>.
C<$old_state> includes the state of the workflow before the action
was executed, C<$action_name> is the action name that was executed and
C<$autorun> is set to 1 if the action just executed was started
using autorun.
was executed, C<$action_name> is the action name that was executed.
=item *
B<state change> - Issued after a workflow is successfully executed,
saved and results in a state change. The event will not be fired if
you executed an action that did not result in a state change.
Adds the parameters C<$old_state>, C<$action> and C<$autorun>.
Adds the parameters C<$old_state>, and C<$action>.
C<$old_state> includes the state of the workflow before the action
was executed, C<$action> is the action name that was executed and
C<$autorun> is set to 1 if the action just executed was autorun.
was executed, C<$action> is the action name that was executed.
=item *
Expand Down Expand Up @@ -935,13 +936,16 @@ than the entire system.
=head2 Object Methods
=head3 execute_action( $action_name, $autorun )
=head3 execute_action( $action_name, $args )
Execute the action C<$action_name>. Typically this changes the state
of the workflow. If C<$action_name> is not in the current state, fails
one of the conditions on the action, or fails one of the validators on
the action an exception is thrown. $autorun is used internally and
is set to 1 if the action was executed using autorun.
the action an exception is thrown.
The C<$args> provided, are checked against the validators to ensure the
context remains in a valid state; upon successful validation, the C<$args>
are merged into the context and the action is executed as described above.
After the action has been successfully executed and the workflow saved
we issue a 'execute' observation with the old state, action name and
Expand All @@ -950,7 +954,7 @@ So if you wanted to write an observer you could create a
method with the signature:
sub update {
my ( $class, $workflow, $action, $old_state, $action_name, $autorun )
my ( $class, $workflow, $action, $old_state, $action_name )
= @_;
if ( $action eq 'execute' ) { .... }
}
Expand Down
21 changes: 12 additions & 9 deletions lib/Workflow/Action.pm
Expand Up @@ -66,26 +66,28 @@ sub get_validators {
}

sub validate {
my ( $self, $wf ) = @_;
my ( $self, $wf, $action_args ) = @_;
my @validators = $self->get_validators;
return unless ( scalar @validators );

my $context = $wf->context;
$action_args //= {};
my %all_args = (
%{ $wf->context->param() },
%{$action_args}
);
foreach my $validator_info (@validators) {
my $validator = $validator_info->{validator};
my $args = $validator_info->{args};

# TODO: Revisit this statement it does not look right
# runtime_args becomes the WF object??
my @runtime_args = ($wf);
my @runtime_args = ();
foreach my $arg ( @{$args} ) {
if ( $arg =~ /^\$(.*)$/ ) {
push @runtime_args, $context->param($1);
push @runtime_args, $all_args{$1};
} else {
push @runtime_args, $arg;
}
}
$validator->validate(@runtime_args);
$validator->validate($wf, @runtime_args);
}
}

Expand Down Expand Up @@ -460,9 +462,10 @@ L<Workflow::Validator> object, while 'args' contains an arrayref of
arguments to pass to the validator, some of which may need to be
evaluated at runtime.
=head3 validate( $workflow )
=head3 validate( $workflow, $action_args )
Run through all validators for this action. If any fail they will
Run through all validators for this action, using the arguments
provided with the C<execute_action> call. If any fail they will
throw a L<Workflow::Exception>, the validation subclass.
=head3 execute( $workflow )
Expand Down
7 changes: 6 additions & 1 deletion lib/Workflow/Base.pm
Expand Up @@ -38,7 +38,12 @@ sub param {

if ( ref $name eq 'HASH' ) {
foreach my $param_name ( keys %{$name} ) {
$self->{PARAMS}{$param_name} = $name->{$param_name};
if (defined $name->{$param_name}) {
$self->{PARAMS}{$param_name} = $name->{$param_name};
}
else {
delete $self->{PARAMS}->{$param_name};
}
}
return { %{ $self->{PARAMS} } };
}
Expand Down

0 comments on commit cb8e055

Please sign in to comment.