Collection/Comparator.pm

filter: Statementstmt 88.8% 56 / 63 Branchbran 75.0% 36 / 48 Conditioncond 61.5% 8 / 13 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 89.4% 17 / 19 Podpod 100% 13 / 13 totaltotal 81.1% 133 / 164 CC 30 SCAR 15.9
CC
30
Cov
81%
verify
11.7
notMatch
6.0
between
4.2
CRAP 4.9
« Model.pm ↑ Summary Collection.pm »
1 package Mojo::Util::Collection::Comparator;
2 32 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 32 our $VERSION = '0.0.19';
5  
6 32 ▶use List::Util qw(all any none);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶has 'specials' => sub {{
Subroutine __ANON__: called CC=1 SCAR=0.0
9 1 '==' => 'eq',
10 '>' => 'gt',
11 '>=' => 'ge',
12 '<' => 'lt',
13 '<=' => 'le',
14 '!=' => 'ne',
15 'not_like' => 'notLike',
16 'not_match' => 'notMatch'
17 32 }};
18  
19 =head2 between
20  
21 Check if search between given values
22  
23 Returns:
24 C<Int> 1/0
25  
26 =cut
27  
28 8 ▶sub between {
Subroutine between: called CC=4 SCAR=14.3
Pod documented
29 8 my ($self, $search, $values) = @_;
30  
31 8 ▶ if (scalar(@$values) != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$values != 2) 0 8 8
32 0 warn "between requires 2 values";
33  
34 0 return 0;
35 }
36  
37 8 my ($min, $max) = @$values;
38  
39 8 ▶ return $self->gt($search, $min) && $self->lt($search, $max) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->gt($search, $min) && $self->lt($search, $max) ? : 4 4 8
Condition100%3/3
$self->gt($search, $min) and $self->lt($search, $max)
!ll&&!rl&&r
134
MC/DC100%2/2
$self->gt($search, $min) and $self->lt($search, $max)
$self->gt($search, $min) $self->lt($search, $max)
Truth table100%3/3
A and B
A= $self->gt($search, $min)B= $self->lt($search, $max)
ABresult
0X0
100
111
40 }
41  
42 =head2 eq
43  
44 Check if a is equal to b
45  
46 Returns:
47 C<Int> 1/0
48  
49 =cut
50  
51 8 ▶sub eq {
Subroutine eq: called CC=3 SCAR=11.1
Pod documented
52 8 my ($self, $a, $b) = @_;
53  
54 8 ▶ return 0 if ($self->__both_undefined($a, $b));
Branch: 1 branch
BranchTrueFalseTotal
if $self->__both_undefined($a, $b) 0 8 8
55  
56 8 ▶ return ($a == $b) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a == $b ? : 2 6 8
57 }
58  
59 =head2 gt
60  
61 Check if a is greater than b
62  
63 Returns:
64 C<Int> 1/0
65  
66 =cut
67  
68 24 ▶sub gt {
Subroutine gt: called CC=3 SCAR=11.1
Pod documented
69 24 my ($self, $a, $b) = @_;
70  
71 24 ▶ return 0 if ($self->__both_undefined($a, $b));
Branch: 1 branch
BranchTrueFalseTotal
if $self->__both_undefined($a, $b) 0 24 24
72  
73 24 ▶ return ($a > $b) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a > $b ? : 20 4 24
74 }
75  
76 =head2 ge
77  
78 Check if a is greater than or equal to b
79  
80 Returns:
81 C<Int> 1/0
82  
83 =cut
84  
85 8 ▶sub ge {
Subroutine ge: called CC=3 SCAR=11.1
Pod documented
86 8 my ($self, $a, $b) = @_;
87  
88 8 ▶ return 0 if ($self->__both_undefined($a, $b));
Branch: 1 branch
BranchTrueFalseTotal
if $self->__both_undefined($a, $b) 0 8 8
89  
90 8 ▶ return ($a >= $b) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a >= $b ? : 4 4 8
91 }
92  
93 =head2 in
94  
95 Check if search is in array
96  
97 Returns:
98 C<Int> 1/0
99  
100 =cut
101  
102 12 ▶sub in {
Subroutine in: called CC=1 SCAR=0.0
Pod documented
103 12 my ($self, $search, $array) = @_;
104  
105 12 ▶ return any { $search eq $_ } @$array;
Subroutine __ANON__: called CC=1 SCAR=0.0
106 }
107  
108 =head2 like
109  
110 Alias for match
111  
112 Returns:
113 C<Int> 1/0
114  
115 =cut
116  
117 4 ▶sub like {
Subroutine like: called CC=1 SCAR=0.0
Pod documented
118 4 return shift->match(@_);
119 }
120  
121 =head2 lt
122  
123 Check if a is less than b
124  
125 Returns:
126 C<Int> 1/0
127  
128 =cut
129  
130 22 ▶sub lt {
Subroutine lt: called CC=3 SCAR=11.1
Pod documented
131 22 my ($self, $a, $b) = @_;
132  
133 22 ▶ return 0 if ($self->__both_undefined($a, $b));
Branch: 1 branch
BranchTrueFalseTotal
if $self->__both_undefined($a, $b) 0 22 22
134  
135 22 ▶ return ($a < $b) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a < $b ? : 10 12 22
136 }
137  
138 =head2 le
139  
140 Check if a is less than or equal to b
141  
142 Returns:
143 C<Int> 1/0
144  
145 =cut
146  
147 8 ▶sub le {
Subroutine le: called CC=3 SCAR=11.1
Pod documented
148 8 my ($self, $a, $b) = @_;
149  
150 8 ▶ return 0 if ($self->__both_undefined($a, $b));
Branch: 1 branch
BranchTrueFalseTotal
if $self->__both_undefined($a, $b) 0 8 8
151  
152 8 ▶ return ($a <= $b) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a <= $b ? : 4 4 8
153 }
154  
155 =head2 match
156  
157 Check if search match pattern
158  
159 Returns:
160 C<Int> 1/0
161  
162 =cut
163  
164 8 ▶sub match {
Subroutine match: called CC=2 SCAR=6.9
Pod documented
165 8 my ($self, $search, $pattern) = @_;
166  
167 8 ▶ return $search =~ m/$pattern/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$search =~ /$pattern/u ? : 3 5 8
168 }
169  
170 =head2 ne
171  
172 Check if a is not equal to b
173  
174 Returns:
175 C<Int> 1/0
176  
177 =cut
178  
179 4 ▶sub ne {
Subroutine ne: called CC=3 SCAR=11.1
Pod documented
180 4 my ($self, $a, $b) = @_;
181  
182 4 ▶ return 0 if ($self->__both_undefined($a, $b));
Branch: 1 branch
BranchTrueFalseTotal
if $self->__both_undefined($a, $b) 0 4 4
183  
184 4 ▶ return ($a != $b) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a != $b ? : 3 1 4
185 }
186  
187 =head2 notLike
188  
189 Alias for notMatch
190  
191 Returns:
192 C<Int> 1/0
193  
194 =cut
195  
196 0 ▶sub notLike {
Subroutine notLike: not called CC=1 SCAR=6.9
Pod documented
197 0 return shift->notMatch(@_);
198 }
199  
200 =head2 notMatch
201  
202 Check if search does not match pattern
203  
204 Returns:
205 C<Int> 1/0
206  
207 =cut
208  
209 0 ▶sub notMatch {
Subroutine notMatch: not called CC=2 SCAR=17.9
Pod documented
210 0 my ($self, $search, $pattern) = @_;
211  
212 0 ▶ return $search !~ m/$pattern/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!($search =~ /$pattern/u) ? : 0 0 0
213 }
214  
215 =head2 verify
216  
217 Check if a matches the conditions from b
218  
219 Returns:
220 C<Int> 1/0
221  
222 =cut
223  
224 128 ▶sub verify {
Subroutine verify: called CC=11 SCAR=24.6
Pod documented
225 128 my ($self, $a, $b) = @_;
226  
227 128 ▶ return 0 if ($self->__both_undefined($a, $b));
Branch: 1 branch
BranchTrueFalseTotal
if $self->__both_undefined($a, $b) 0 128 128
228
229 # Special method comparison
230 128 ▶ if (ref($b) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $b eq "HASH") 87 41 128
231 87 my @keys = keys(%$b);
232  
233 87 ▶ if (scalar(@keys) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @keys > 1) 8 79 87
234 8 ▶ return all { $self->verify($a, { $_ => $b->{ $_ } }) } @keys;
Subroutine __ANON__: called CC=1 SCAR=0.0
235 }
236  
237 79 my $method = $keys[0];
238 79 my $comparator = $method;
239  
240 79 ▶ if (! $self->can($method) && $self->specials->{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->can($method) and $self->specials->{$method}) 24 55 79
Condition66%2/3
not $self->can($method) and $self->specials->{$method}
!ll&&!rl&&r
55024
MC/DC50%1/2
not $self->can($method) and $self->specials->{$method}
not $self->can($method) $self->specials->{$method}
Truth table66%2/3
not(A) and B
A= not $self->can($method)B= $self->specials->{$method}
ABresult
0X0
100
111
241 24 $comparator = $self->specials->{$method};
242 }
243  
244 79 ▶ if (! $self->can($comparator)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($comparator)) 0 79 79
245 0 warn "$method is not defined";
246  
247 0 return 0;
248 }
249  
250 79 return $self->$comparator($a, $b->{ $method });
251 }
252  
253 41 ▶ if (ref($b) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $b eq "ARRAY") 8 33 41
254 8 return $self->in($a, $b);
255 }
256  
257 # String comparison
258 33 ▶ return (($a || '') eq ($b || '')) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($a || '') eq ($b || '') ? : 11 22 33
Condition50%2/4
$a || ''
l!l
330
$b || ''
l!l
330
MC/DC0%0/1
$a || ''
$a
MC/DC0%0/1
$b || ''
$b
Truth table50%1/2
A
A= $a
Aresult
11
00
Truth table50%1/2
A
A= $b
Aresult
11
00
259 }
260  
261 =head2 __both_undefined
262  
263 Check if both values are undefined
264  
265 Returns:
266 C<Int> 1/0
267  
268 =cut
269  
270 202 ▶sub __both_undefined {
Subroutine __both_undefined: called CC=3 SCAR=13.1
271 202 my ($self, $a, $b) = @_;
272  
273 202 ▶ return (!defined($a) && !defined($b)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!(defined $a || defined $b) ? : 0 202 202
Condition33%1/3
defined $a || defined $b
l!l&&r!l&&!r
20200
MC/DC0%0/2
defined $a || defined $b
defined $a defined $b
Truth table33%1/3
A || B
A= defined $aB= defined $b
ABresult
1X1
011
000
274 }
275  
276 32 1;
« Model.pm ↑ Summary Collection.pm »