File Coverage

blib/lib/Mojo/Util/Collection/Comparator.pm
Criterion Covered Total %
statement 52 59 88.1
branch 35 48 72.9
condition 8 13 61.5
subroutine 16 18 88.8
pod 13 13 100.0
total 124 151 82.1


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