File Coverage

blib/lib/Workflow/Condition/LazyOR.pm
Criterion Covered Total %
statement 31 31 100.0
branch 2 2 100.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 42 42 100.0


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