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