File Coverage

lib/Workflow/Condition/LazyAND.pm
Criterion Covered Total %
statement 32 32 100.0
branch 2 2 100.0
condition 1 3 33.3
subroutine 7 7 100.0
pod 1 1 100.0
total 43 45 95.5


line stmt bran cond sub pod time code
1             package Workflow::Condition::LazyAND;
2              
3 1     1   7 use strict;
  1         3  
  1         29  
4 1     1   8 use warnings;
  1         4  
  1         75  
5              
6             our $VERSION = '1.62';
7              
8 1     1   7 use base qw( Workflow::Condition::Nested );
  1         2  
  1         105  
9 1     1   6 use Workflow::Exception qw( condition_error configuration_error );
  1         3  
  1         68  
10 1     1   10 use English qw( -no_match_vars );
  1         1  
  1         7  
11              
12             __PACKAGE__->mk_accessors('conditions');
13              
14             sub _init {
15 2     2   5 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         5 my @conditions = ();
23 2         5 foreach my $key ( sort grep {m/^condition/} keys %{$params} ) {
  9         26  
  2         7  
24 5         20 push @conditions, $self->normalize_array( $params->{$key} );
25             }
26 2         7 $self->conditions( [@conditions] );
27              
28             }
29              
30             sub evaluate {
31 4     4 1 12 my ( $self, $wf ) = @_;
32 4         16 my $conditions = $self->conditions;
33              
34 4         50 my $total = 0;
35              
36 4         7 foreach my $cond ( @{$conditions} ) {
  4         17  
37 8         43 my $result = $self->evaluate_condition( $wf, $cond );
38 8 100       91 if ( not $result ) {
39 2         16 condition_error("Condition '$cond' returned 'false'");
40             }
41 6         14 $total++;
42             }
43              
44 2   33     7 return $total
45             || condition_error("No condition seems to have been run in LazyAND");
46             }
47              
48             1;
49              
50             __END__
51              
52             =pod
53              
54             =head1 NAME
55              
56             Workflow::Condition::LazyAND
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             the given conditions using lazy-evaluation, returning I<true> if B<all>
66             nested conditions are I<true>. If a nested condition evaluates to I<false>,
67             further evaluation is aborted and I<false> is returned.
68              
69             =head1 SYNOPSIS
70              
71             In condition.xml:
72              
73             <condition name="cond1" ... />
74             <condition name="cond2" ... />
75             <condition name="cond3" ... />
76              
77             <condition name="check_prereqs" class="Workflow::Condition::LazyAND">
78             <param name="condition" value="cond1" />
79             <param name="condition" value="cond2" />
80             <param name="condition" value="cond3" />
81             </condition>
82              
83             In workflow.xml:
84              
85             <state name="CHECK_PREREQS" autorun="yes">
86             <action name="null_1" resulting_state="HAVE_PREREQS">
87             <condition name="check_prereqs" />
88             </action>
89             <action name="null_2" resulting_state="FAILURE">
90             <condition name="!check_prereqs" />
91             </action>
92             </state>
93              
94             =cut
95              
96             =head1 PARAMETERS
97              
98             The following parameters may be configured in the C<param> entity of the
99             condition in the XML configuration:
100              
101             =head2 condition, conditionN
102              
103             The condition parameter may be specified as either a list of repeating
104             entries B<or> with a unique integer appended to the I<condition> string:
105              
106             <param name="condition" value="first_condition_to_test" />
107             <param name="condition" value="second_condition_to_test" />
108              
109             B<or>
110              
111             <param name="condition1" value="first_condition_to_test" />
112             <param name="condition2" value="second_condition_to_test" />
113              
114             =head1 COPYRIGHT
115              
116             Copyright (c) 2003-2023 Chris Winters. All rights reserved.
117              
118             This library is free software; you can redistribute it and/or modify
119             it under the same terms as Perl itself.
120              
121             Please see the F<LICENSE>
122              
123             =head1 AUTHORS
124              
125             Please see L<Workflow>
126              
127             =cut