File Coverage

lib/Workflow/Condition/CheckReturn.pm
Criterion Covered Total %
statement 35 42 83.3
branch 7 14 50.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 50 64 78.1


line stmt bran cond sub pod time code
1              
2             use strict;
3 1     1   6 use warnings;
  1         2  
  1         25  
4 1     1   4  
  1         1  
  1         46  
5             our $VERSION = '1.61';
6              
7             use base qw( Workflow::Condition::Nested );
8 1     1   6 use Workflow::Exception qw( condition_error configuration_error );
  1         1  
  1         138  
9 1     1   7 use English qw( -no_match_vars );
  1         1  
  1         60  
10 1     1   6  
  1         1  
  1         5  
11             __PACKAGE__->mk_accessors( 'condition', 'operator', 'argument' );
12              
13             my %supported_ops = (
14             eq => '==',
15             lt => '<',
16             gt => '>',
17             le => '<=',
18             ge => '>=',
19             ne => '!=',
20             );
21              
22             my ( $self, $params ) = @_;
23              
24 3     3   4 unless ( defined $params->{condition} ) {
25             configuration_error
26 3 50       8 "You must specify the name of the nested condition in the parameter 'condition' for ",
27 0         0 $self->name;
28             }
29             $self->condition( $params->{condition} );
30              
31 3         7 unless ( defined $params->{operator} ) {
32             configuration_error "You must define the value for 'operator' in ",
33 3 50       22 "declaration of condition ", $self->name;
34 0         0 }
35             $self->operator( $params->{operator} );
36              
37 3         9 unless ( defined $params->{argument} ) {
38             configuration_error "You must define the value for 'argument' in ",
39 3 50       24 "declaration of condition ", $self->name;
40 0         0 }
41             $self->argument( $params->{argument} );
42             }
43 3         5  
44             my ( $self, $wf ) = @_;
45             my $cond = $self->condition;
46             my $op = $self->operator;
47 6     6 1 8 my $arg = $self->argument;
48 6         25  
49 6         73 # warn "DEBUG: evaluating operator '$op'";
50 6         49  
51             my $numop = $supported_ops{$op};
52             if ( not $numop ) {
53             configuration_error "Unsupported operator '$op'";
54 6         51 }
55 6 50       11  
56 0         0 # Fetch argument from context or eval, if necessary
57             my $argval;
58             if ( $arg =~ /^[-]?\d+$/ ) { # numeric
59             $argval = $arg;
60 6         7 } elsif ( $arg =~ /^[a-zA-Z0-9_]+$/ ) { # alpha-numeric, plus '_'
61 6 50       31 $argval = $wf->context->param($arg);
    0          
62 6         8 } else {
63             $argval = eval $arg;
64 0         0 }
65              
66 0         0 my $condval = $self->evaluate_condition( $wf, $cond );
67              
68             if ( eval "\$condval $op \$argval" ) {
69 6         19 return 1;
70             } else {
71 6 100       332 condition_error "Condition failed: '$condval' $op '$argval'";
72 2         7 }
73              
74 4         26 configuration_error
75             "Unknown error in CheckReturn.pm: cond=$cond, op=$op, arg=$arg";
76             }
77 0            
78             1;
79              
80              
81             =pod
82              
83             =head1 NAME
84              
85             Workflow::Condition::CheckReturn
86              
87             =head1 VERSION
88              
89             This documentation describes version 1.61 of this package
90              
91             =head1 DESCRIPTION
92              
93             Using nested conditions (See Workflow::Condition::Nested), this evaluates
94             a given condition and compares the value returned with a given argument.
95              
96             =head1 SYNOPSIS
97              
98             In condition.xml:
99              
100             <condition name="check_approvals" class="Workflow::Condition::CheckReturn">
101             <param name="condition" value="count_approvals" />
102             <!-- operator "ge" means: greater than or equal to -->
103             <param name="operator" value="ge" />
104             <param name="argument" value="$context->{approvals_needed}" />
105             </condition>
106              
107             In workflow.xml:
108              
109             <state name="CHECK_APPROVALS" autorun="yes">
110             <action name="null_1" resulting_state="APPROVED">
111             <condition name="check_approvals" />
112             </action>
113             <action name="null_2" resulting_state="REJECTED">
114             <condition name="!check_approvals" />
115             </action>
116             </state>
117              
118             =cut
119              
120             =head1 PARAMETERS
121              
122             The following parameters may be configured in the C<param> entity of the
123             condition in the XML configuration:
124              
125             =head2 condition
126              
127             The name of the condition to be evaluated.
128              
129             =head2 argument
130              
131             The value to compare with the given condition. This can be one of the following:
132              
133             =over
134              
135             =item Integer
136              
137             The integer value is compared with the return value of the condition.
138              
139             =item String [a-zA-Z0-9_]
140              
141             The string is interpreted as the name of a workflow context parameter. The current
142             value of that parmeter is used in the comparison.
143              
144             =item String
145              
146             Any other string is evaluated in an C<eval> block. The result should be numeric.
147              
148             =back
149              
150             =head2 operator
151              
152             The name of the comparison operator to use. Supported values are:
153              
154             'eq', 'lt', 'gt', 'le', 'ge', 'ne'
155              
156             The string names are used to simplify the notation in the XML files. The
157             above strings map to the following numeric operators internally:
158              
159             '==', '<', '>', '<=', '>=', !=
160              
161             =head1 COPYRIGHT
162              
163             Copyright (c) 2004-2022 Chris Winters. All rights reserved.
164              
165             This library is free software; you can redistribute it and/or modify
166             it under the same terms as Perl itself.
167              
168             Please see the F<LICENSE>
169              
170             =head1 AUTHORS
171              
172             Please see L<Workflow>
173              
174             =cut