File Coverage

lib/Workflow/Condition/GreedyOR.pm
Criterion Covered Total %
statement 32 32 100.0
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 44 44 100.0


line stmt bran cond sub pod time code
1             package Workflow::Condition::GreedyOR;
2              
3 1     1   7 use strict;
  1         5  
  1         38  
4 1     1   6 use warnings;
  1         3  
  1         70  
5              
6             our $VERSION = '1.62';
7              
8 1     1   6 use base qw( Workflow::Condition::Nested );
  1         3  
  1         486  
9 1     1   8 use Workflow::Exception qw( condition_error configuration_error );
  1         2  
  1         82  
10 1     1   7 use English qw( -no_match_vars );
  1         4  
  1         17  
11              
12             __PACKAGE__->mk_accessors('conditions');
13              
14             sub _init {
15 2     2   10 my ( $self, $params ) = @_;
16              
17             # This is a tricky one. The admin may have configured this by repeating
18             # the param name "condition" or by using unique names (e.g.: "condition1",
19             # "condition2", etc.). We'll need to string these back together as
20             # an array.
21             # Yes, I know. The regex doesn't require the suffix to be numeric.
22 2         3 my @conditions = ();
23 2         4 foreach my $key ( sort grep {m/^condition/} keys %{$params} ) {
  8         38  
  2         9  
24 4         17 push @conditions, $self->normalize_array( $params->{$key} );
25             }
26 2         8 $self->conditions( [@conditions] );
27              
28             }
29              
30             sub evaluate {
31 10     10 1 20 my ( $self, $wf ) = @_;
32 10         27 my $conditions = $self->conditions;
33              
34 10         124 my $result = 0;
35              
36 10         13 foreach my $cond ( @{$conditions} ) {
  10         24  
37 22 100       105 $result += $self->evaluate_condition( $wf, $cond ) ? 1 : 0;
38             }
39              
40 10 100       30 if ($result) {
41 6         15 return $result;
42             } else {
43             condition_error( "All of the conditions returned 'false': ",
44 4         10 join ', ', @{$conditions} );
  4         19  
45             }
46             }
47              
48             1;
49              
50             __END__
51              
52             =pod
53              
54             =head1 NAME
55              
56             Workflow::Condition::GreedyOR
57              
58             =head1 VERSION
59              
60             This documentation describes version 1.62 of this package
61              
62             =head1 DESCRIPTION
63              
64             Using nested conditions (See Workflow::Condition::Nested), this evaluates
65             I<all> given conditions, returning the count of successful checks. If
66             none of the nested conditions are true, an exeption is thrown.
67              
68             =head1 SYNOPSIS
69              
70             In condition.xml:
71              
72             <condition name="cond1" ... />
73             <condition name="cond2" ... />
74             <condition name="cond3" ... />
75              
76             <condition name="count_approvals" class="Workflow::Condition::GreedyOR">
77             <param name="condition" value="cond1" />
78             <param name="condition" value="cond2" />
79             <param name="condition" value="cond3" />
80             </condition>
81              
82             <condition name="check_approvals" class="Workflow::Condition::CheckReturn">
83             <param name="condition" value="count_approvals" />
84             <!-- operator "ge" means: greater than or equal to -->
85             <param name="operator" value="ge" />
86             <param name="argument" value="$context->{approvals_needed}" />
87             </condition>
88              
89             In workflow.xml:
90              
91             <state name="CHECK_APPROVALS" autorun="yes">
92             <action name="null_1" resulting_state="APPROVED">
93             <condition name="check_approvals" />
94             </action>
95             <action name="null_2" resulting_state="REJECTED">
96             <condition name="!check_approvals" />
97             </action>
98             </state>
99              
100             =cut
101              
102             =head1 PARAMETERS
103              
104             The following parameters may be configured in the C<param> entity of the
105             condition in the XML configuration:
106              
107             =head2 condition, conditionN
108              
109             The condition parameter may be specified as either a list of repeating
110             entries B<or> with a unique integer appended to the I<condition> string:
111              
112             <param name="condition" value="first_condition_to_test" />
113             <param name="condition" value="second_condition_to_test" />
114              
115             B<or>
116              
117             <param name="condition1" value="first_condition_to_test" />
118             <param name="condition2" value="second_condition_to_test" />
119              
120             =head1 COPYRIGHT
121              
122             Copyright (c) 2004-2023 Chris Winters. All rights reserved.
123              
124             This library is free software; you can redistribute it and/or modify
125             it under the same terms as Perl itself.
126              
127             Please see the F<LICENSE>
128              
129             =head1 AUTHORS
130              
131             Please see L<Workflow>
132              
133             =cut