Simple/Library/Operators.pm

filter: Statementstmt 98.3% 60 / 61 Branchbran 86.3% 19 / 22 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 14 / 14 Podpod n/a totaltotal 93.1% 95 / 102 CC 17 SCAR 10.3
CC
17
Cov
94%
__ANON__
4.0
__ANON__
4.0
__ANON__
3.0
CRAP 2.8
« Simple/Library/OO.pm ↑ Summary Simple/Library/Base.pm »
1 =head1 NAME
2  
3 Declare::Constraints::Simple::Library::Operators - Operators
4  
5 =cut
6  
7 package Declare::Constraints::Simple::Library::Operators;
8 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 ▶use Declare::Constraints::Simple-Library;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 13 ▶use Carp::Clan qw(^Declare::Constraints::Simple);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 SYNOPSIS
16  
17 # all hast to be valid
18 my $and_constraint = And( IsInt,
19 Matches(qr/0$/) );
20  
21 # at least one has to be valid
22 my $or_constraint = Or( IsInt, HasLength );
23  
24 # only one can be valid
25 my $xor_constraint = XOr( IsClass, IsObject );
26  
27 # reverse validity
28 my $not_an_integer = Not( IsInt );
29  
30 # case valid, validate 'bar' key depending on 'foo' keys value
31 my $struct_prof =
32 And( IsHashRef,
33 CaseValid( OnHashKeys(foo => IsEq("FooArray")),
34 OnHashKeys(bar => IsArrayRef),
35 OnHashKeys(foo => IsEq("FooHash")),
36 OnHashKeys(bar => IsHashRef) ));
37  
38 =head1 DESCRIPTION
39  
40 This module contains the frameworks operators. These constraint like
41 elements act on the validity of passed constraints.
42  
43 =head1 OPERATORS
44  
45 =head2 And(@constraints)
46  
47 Is true if all passed C<@constraints> are true on the value. Returns
48 the result of the first failing constraint.
49  
50 =cut
51  
52 constraint 'And',
53 13 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
54 13 my @vc = @_;
55 37 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
56 37 for (@vc) {
57 75 my $r = $_->($_[0]);
58 75 ▶ return $r unless $r->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->is_valid 22 53 75
59 }
60 15 return _true;
61 13 };
62 13 };
63  
64 =head2 Or(@constraints)
65  
66 Is true if at least one of the passed C<@contraints> is true. Returns the
67 last failing constraint's result if false.
68  
69 =cut
70  
71 constraint 'Or',
72 8 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 8 my @vc = @_;
74 19 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
75 19 my $last_r;
76 19 for (0 .. $#vc) {
77 32 my $v = $vc[$_];
78 32 my $r = $v->($_[0]);
79 32 ▶ return _true if $r->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
if $r->is_valid 13 19 32
80 19 ▶ return $r if $_ == $#vc;
Branch: 1 branch
BranchTrueFalseTotal
if $_ == $#vc 5 14 19
81 }
82 1 return _false('No constraints');
83 8 };
84 13 };
85  
86 =head2 XOr(@constraints)
87  
88 Valid only if a single one of the passed C<@constraints> is valid. Returns
89 the last failing constraint's result if false.
90  
91 =cut
92  
93 constraint 'XOr',
94 4 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
95 4 my @vc = @_;
96 4 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
97 4 my $m = 0;
98 4 for (@vc) {
99 7 my $r = $_->($_[0]);
100 7 ▶ $m++ if $r->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
if $r->is_valid 3 4 7
101 }
102 4 return _result(($m == 1), sprintf 'Got %d true returns', $m);
103 4 };
104 13 };
105  
106 =head2 Not($constraint)
107  
108 This is valid if the passed C<$constraint> is false. The main purpose
109 of this operator is to allow the easy reversion of a constraint's
110 trueness.
111  
112 =cut
113  
114 constraint 'Not',
115 5 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
116 5 my ($c) = @_;
117 5 ▶ croak '\'Not\' only accepts only a constraint as argument'
Branch: 1 branch
BranchTrueFalseTotal
if defined $c and not ref $c eq "CODE" 0 5 5
Condition66%2/3
defined $c and not ref $c eq "CODE"
!ll&&!rl&&r
140
MC/DC0%0/2
defined $c and not ref $c eq "CODE"
defined $c not ref $c eq "CODE"
Truth table66%2/3
A and not(B)
A= defined $cB= not ref $c eq "CODE"
ABresult
0X0
100
111
118 if defined $c and not ref($c) eq 'CODE';
119 5 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
120 5 ▶ return _true unless $c;
Branch: 1 branch
BranchTrueFalseTotal
unless $c 1 4 5
121 4 my $r = $c->($_[0]);
122 4 ▶ return _false('Constraint returned true') if $r->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
if $r->is_valid 2 2 4
123 2 return _true;
124 5 };
125 13 };
126  
127 =head2 CaseValid($test, $conseq, $test2, $conseq2, ...)
128  
129 This runs every given C<$test> argument on the value, until it finds
130 one that returns true. If none is found, false is returned. On a true
131 result, howver, the corresponding C<$conseq> constraint is applied to
132 the value and it's result returned. This allows validation depending
133 on other properties of the value:
134  
135 my $flexible = CaseValid( IsArrayRef,
136 And( HasArraySize(1,5),
137 OnArrayElements(0 => IsInt) ),
138 IsHashRef,
139 And( HasHashElements(qw( head tail )),
140 OnHashKeys(head => IsInt) ));
141  
142 Of course, you could model most of it probably with the other
143 operators, but this is a bit more readable. For default cases use
144 C<ReturnTrue> from L<Declare::Constraints::Simple::Library::General>
145 as test.
146  
147 =cut
148  
149 constraint 'CaseValid',
150 1 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
151 1 my @defs = @_;
152 1 my ($c, @cases);
153 1 ▶ while (my $test = shift @defs) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $test = shift @defs) 4 1 5
154 4 $c++;
155 4 ▶ croak "CaseValid test nr $c is not a constraint"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $test eq "CODE" 0 4 4
156 unless ref($test) eq 'CODE';
157  
158 4 my $conseq = shift @defs;
159 4 ▶ croak "CaseValid consequence nr $c is not a constraint"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $test eq "CODE" 0 4 4
160 unless ref($test) eq 'CODE';
161  
162 4 push @cases, [$test, $conseq];
163 }
164  
165 8 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
166 8 for my $case (@cases) {
167 19 my ($test, $conseq) = @$case;
168 19 ▶ next unless $test->($_[0])->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless &$test($_[0])->is_valid 11 8 19
169 8 return $conseq->($_[0]);
170 }
171 0 _false('No matching case');
172 1 };
173 13 };
174  
175 =head1 SEE ALSO
176  
177 L<Declare::Constraints::Simple>, L<Declare::Constraints::Simple::Library>
178  
179 =head1 AUTHOR
180  
181 Robert 'phaylon' Sedlacek C<E<lt>phaylon@dunkelheit.atE<gt>>
182  
183 =head1 LICENSE AND COPYRIGHT
184  
185 This module is free software, you can redistribute it and/or modify it
186 under the same terms as perl itself.
187  
188 =cut
189  
190 13 1;
« Simple/Library/OO.pm ↑ Summary Simple/Library/Base.pm »