Object/Interval.pm

filter: Statementstmt 98.5% 131 / 133 Branchbran 86.5% 45 / 52 Conditioncond 60.6% 37 / 61 MC/DCmcdc 21.2% 7 / 33 Subroutinesub 100% 25 / 25 Podpod 90.9% 10 / 11 totaltotal 80.9% 255 / 315 CC 47 SCAR 21.3
CC
47
Cov
87%
_explode_range
21.1
_validate_and_expand
12.1
military
5.1
CRAP 8.4
« Object/Date.pm ↑ Summary Object.pm »
1 package Range::Object::Interval;
2  
3 # This is basically what common::sense does, but without the pragma itself
4 # to remain compatible with Perls older than 5.8
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings qw(FATAL closed internal debugging pack malloc portable
Subroutine BEGIN: called CC=1 SCAR=0.0
10 prototype inplace io pipe unpack deprecated glob digit
11 1 printf reserved taint closure semicolon);
12 1 ▶no warnings qw(exec newline unopened);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use base qw(Range::Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Overload definitions
20  
21 1 ▶use overload q{""} => 'stringify_collapsed',
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 fallback => 1;
23  
24 ### PUBLIC CLASS METHOD (CONSTRUCTOR) ###
25 #
26 # Initializes new instance of Range::Object::Interval from @input_range using
27 # $interval length.
28 #
29  
30 25 ▶sub new {
Subroutine new: called CC=4 SCAR=14.0
Pod documented
31 25 my ($class, $interval, @input_range) = @_;
32  
33 25 ▶ croak "Interval can be 15, 30 or 60 minutes not '$interval'"
Branch: 1 branch
BranchTrueFalseTotal
if $interval != 15 and $interval != 30 and $interval != 60 0 25 25
Condition83%5/6
$interval != 15 and $interval != 30 and $interval != 60
!ll&&!rl&&r
1780
$interval != 15 and $interval != 30
!ll&&!rl&&r
988
MC/DC0%0/3
$interval != 15 and $interval != 30 and $interval != 60
$interval != 15 $interval != 30 $interval != 60
Truth table75%3/4
A and B and C
A= $interval != 15B= $interval != 30C= $interval != 60
ABCresult
0XX0
10X0
1100
1111
34 if $interval != 15 && $interval != 30 && $interval != 60;
35  
36 25 my $self = bless { interval => $interval }, $class;
37  
38 25 return $self->add(@input_range);
39 }
40  
41 ### PUBLIC INSTANCE METHOD ###
42 #
43 # Returns sorted array or string representation of internal storage.
44 # In scalar context it can use optional list separator instead of
45 # default one.
46 #
47  
48 36 ▶sub range {
Subroutine range: called CC=2 SCAR=6.9
Pod documented
49 36 my ($self, $separator) = @_;
50  
51 36 ▶ return wantarray ? map { $self->_colonify($_) } $self->_full_range()
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 18 18 36
52 : $self->stringify($separator)
53 ;
54 }
55  
56 ### PUBLIC INSTANCE METHOD ###
57 #
58 # Returns sorted and collapsed representation of internal storage.
59 # In list context, resulting list consists of separate values and/or
60 # range hashrefs with three elements: start, end and count.
61 # In scalar context, result is a string of separate values and/or
62 # ranges separated by value returned by delimiter() method.
63 # Optional list separator can be used instead of default one in
64 # scalar context.
65 #
66  
67 36 ▶sub collapsed {
Subroutine collapsed: called CC=3 SCAR=11.0
Pod documented
68 36 my ($self, $separator) = @_;
69  
70 36 ▶ return $self->stringify_collapsed($separator) unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 18 18 36
71  
72 return map {
73 !ref($_) ? $self->_colonify($_)
74 : {
75 start => $self->_colonify($_->{start}),
76 end => $self->_colonify($_->{end}),
77 count => $_->{count},
78 }
79 72 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
!ref($_) ? : 22 50 72
80 18 @{ $self->{range} };
81 }
82  
83 ### PUBLIC INSTANCE METHOD ###
84 #
85 # Returns the current range in military format. In scalar context it
86 # can use optional list $separator instead of default one.
87 #
88 # Note that this format cannot be fed back to add() or remove() since
89 # there is no way to distinguish certain time intervals in military
90 # format from any run of the mill integer numbers.
91 #
92 # military() always returns collapsed output in both list and scalar
93 # context.
94 #
95  
96 18 ▶sub military {
Subroutine military: called CC=5 SCAR=16.3
Pod documented
97 18 my ($self, $separator) = @_;
98  
99 18 ▶ $separator ||= $self->_list_separator;
Condition33%1/3
$separator ||= $self->_list_separator
l!l&&r!l&&!r
0180
MC/DC0%0/2
$separator ||= $self->_list_separator
$separator $self->_list_separator
Truth table33%1/3
A ||= B
A= $separatorB= $self->_list_separator
ABresult
1X1
011
000
100  
101 18 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 9 9 18
102 return map {
103 !ref($_) ? 0 + $_
104 : {
105 start => 0 + $_->{start},
106 end => 0 + $_->{end},
107 count => $_->{count},
108 }
109 36 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
!ref($_) ? : 11 25 36
110 9 @{ $self->{range} };
111 }
112 else {
113 my @military_range
114 = map {
115 !ref($_) ? 0+$_
116 : $self->_stringify_range(0+$_->{start},
117 0+$_->{end})
118 36 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
!ref($_) ? : 11 25 36
119 9 @{ $self->{range} };
120  
121 9 return join $separator, @military_range;
122 };
123  
124 0 return; # Just in case, as usual
125 }
126  
127 ### PUBLIC INSTANCE METHOD ###
128 #
129 # Tests if items of @input_range are matching items in our internal storage.
130 # Returns true/false in scalar context, list of mismatching items in list
131 # context.
132 #
133  
134 319 ▶sub in {
Subroutine in: called CC=2 SCAR=6.9
Pod documented
135 319 my ($self, @input_range) = @_;
136  
137 319 ▶ return $self->SUPER::in(@input_range) unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 301 18 319
138  
139 18 my @invalid_values = map { $self->_colonify($_) }
140 $self->SUPER::in(@input_range);
141  
142 18 return @invalid_values;
143 }
144  
145 ### PUBLIC INSTANCE METHOD ###
146 #
147 # Returns string representation of all items in internal storage (sorted).
148 #
149  
150 18 ▶sub stringify {
Subroutine stringify: called CC=3 SCAR=11.4
Pod documented
151 18 my ($self, $separator) = @_;
152  
153 18 ▶ $separator ||= $self->_list_separator();
Condition33%1/3
$separator ||= $self->_list_separator
l!l&&r!l&&!r
0180
MC/DC0%0/2
$separator ||= $self->_list_separator
$separator $self->_list_separator
Truth table33%1/3
A ||= B
A= $separatorB= $self->_list_separator
ABresult
1X1
011
000
154 18 my $delimiter = $self->delimiter();
155  
156 my @full_range
157 242 = map { $self->_colonify($_) }
158 map {
159 18 ref($_) ? $self->_stringify_range( $_->{start}, $_->{end} )
160 242 ▶ : "$_"
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 242 242
161 }
162 $self->_full_range();
163  
164 18 return join $separator, @full_range;
165 }
166  
167 ### PUBLIC INSTANCE METHOD ###
168 #
169 # Returns string representation of collapsed current range.
170 #
171  
172 36 ▶sub stringify_collapsed {
Subroutine stringify_collapsed: called CC=3 SCAR=11.1
Pod documented
173 36 my ($self, $separator) = @_;
174  
175 36 ▶ $separator ||= $self->_list_separator();
Condition33%1/3
$separator ||= $self->_list_separator
l!l&&r!l&&!r
0360
MC/DC0%0/2
$separator ||= $self->_list_separator
$separator $self->_list_separator
Truth table33%1/3
A ||= B
A= $separatorB= $self->_list_separator
ABresult
1X1
011
000
176  
177 my @collapsed_range
178 144 = map { $self->_colonify($_) }
179 map {
180 ref($_) ? $self->_stringify_range( $_->{start}, $_->{end} )
181 144 ▶ : "$_"
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 100 44 144
182 }
183 36 @{ $self->{range} };
184  
185 36 return join $separator, @collapsed_range;
186 }
187  
188 ### PUBLIC INSTANCE METHOD ###
189 #
190 # Returns regex that is used to validate range items. Regex should
191 # include patterns both for separate disjointed items and contiguous
192 # ranges.
193 #
194  
195 350 ▶sub pattern {
Subroutine pattern: called CC=1 SCAR=0.0
Pod documented
196 350 return qr/
197 (?<! [:0-9] ) # Can't have colon or digits behind
198 (?: # Group but don't collect
199 \d{2} # Two digits
200 : # Colon
201 \d{2} # Two digits
202 ) # ... end group
203 /xms
204 }
205  
206 ### PUBLIC INSTANCE METHOD ###
207 #
208 # Returns regex that is used to separate items in a range list.
209 #
210  
211 350 ▶sub separator {
Subroutine separator: called CC=1 SCAR=0.0
Pod documented
212 350 return qr/
213 [,;] # Comma or semicolon
214 \s* # Greedy whitespace
215 /xms
216 }
217  
218 ### PUBLIC INSTANCE METHOD ###
219 #
220 # Returns range delimiter, for Interval it is forward slash (/) in
221 # compliance with ISO 8601.
222 #
223  
224 1477 ▶sub delimiter { '/' }
Subroutine delimiter: called CC=1 SCAR=0.0
Pod documented
225  
226 ### PUBLIC INSTANCE METHOD ###
227 #
228 # Returns current object's interval length in minutes.
229 #
230  
231 717 ▶sub interval { $_[0]->{interval} }
Subroutine interval: called CC=1 SCAR=0.0
Pod undocumented
232  
233 ############## PRIVATE METHODS BELOW ##############
234  
235 ### PRIVATE INSTANCE METHOD ###
236 #
237 # Returns default list separator for use with stringify() and
238 # stringify_collapsed()
239 #
240  
241 72 ▶sub _list_separator { q{,} }
Subroutine _list_separator: called CC=1 SCAR=0.0
242  
243 ### PRIVATE INSTANCE METHOD ###
244 #
245 # Validates and unpacks input @input_range of items, returns full list.
246 #
247  
248 350 ▶sub _validate_and_expand {
Subroutine _validate_and_expand: called CC=12 SCAR=24.9
249 350 my ($self, @input_range) = @_;
250  
251 350 my $pattern = $self->pattern();
252 350 my $separator = $self->separator();
253 350 my $delimiter = $self->delimiter();
254 350 my $interval = $self->interval();
255  
256 # We use hash to remove duplicates
257 350 my %temp = ();
258  
259 # Validate and expand items in @input_range, add them if all OK
260 ITEM:
261 350 ▶ while ( @input_range ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@input_range) 774 337 1111
262 774 my $item = shift @input_range;
263  
264 774 ▶ if ( $separator && $item =~ $separator ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($separator and $item =~ /$separator/msx) 11 763 774
Condition66%2/3
$separator and $item =~ /$separator/msx
!ll&&!rl&&r
076311
MC/DC50%1/2
$separator and $item =~ /$separator/msx
$separator $item =~ /$separator/msx
Truth table66%2/3
A and B
A= $separatorB= $item =~ /$separator/msx
ABresult
0X0
100
111
265 11 unshift @input_range, split $separator, $item;
266 11 next ITEM;
267 };
268  
269 763 ▶ croak "Invalid input: $item"
Branch: 1 branch
BranchTrueFalseTotal
if $item and not $item =~ /$pattern/msx 1 762 763
Condition66%2/3
$item and not $item =~ /$pattern/msx
!ll&&!rl&&r
07621
MC/DC50%1/2
$item and not $item =~ /$pattern/msx
$item not $item =~ /$pattern/msx
Truth table66%2/3
A and not(B)
A= $itemB= not $item =~ /$pattern/msx
ABresult
0X0
100
111
270 if $item && $item !~ $pattern;
271  
272 # Analogous to Range::Dates but not quite so
273 762 my @items;
274 762 ▶ if ( $item =~ m{ ($pattern) / ($pattern) }xms ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item =~ m[ ($pattern) / ($pattern) ]msx) { } 25 737 762
elsif ($item =~ / \A (\d{2}) : (\d{2}) \z /msx) { } 736 1 737
275 25 my ($start, $end) = ($1, $2);
276  
277 # Remove colons
278 25 $start =~ s/://;
279 25 $end =~ s/://;
280  
281 25 my @full_list = $self->_explode_range($start.$delimiter.$end);
282 22 @temp{ @full_list } = (1) x @full_list;
283 }
284 elsif ( $item =~ / \A (\d{2}) : (\d{2}) \z /xms) {
285 736 my ($hr, $mn) = ($1, $2);
286  
287 736 ▶ croak "Invalid input: '$item'"
Branch: 1 branch
BranchTrueFalseTotal
if not $hr =~ / \A \d{1,2} \z /msx or $hr < 0 or $hr > 23 or $mn % $interval != 0 8 728 736
Condition66%6/9
not $hr =~ / \A \d{1,2} \z /msx or $hr < 0 or $hr > 23 or $mn % $interval != 0
l!l&&r!l&&!r
17728
not $hr =~ / \A \d{1,2} \z /msx or $hr < 0 or $hr > 23
l!l&&r!l&&!r
01735
not $hr =~ / \A \d{1,2} \z /msx or $hr < 0
l!l&&r!l&&!r
00736
MC/DC50%2/4
not $hr =~ / \A \d{1,2} \z /msx or $hr < 0 or $hr > 23 or $mn % $interval != 0
not $hr =~ / \A \d{1,2} \z /msx $hr < 0 $hr > 23 $mn % $interval != 0
Truth table60%3/5
not(A) or B or C or D
A= not $hr =~ / \A \d{1,2} \z /msxB= $hr < 0C= $hr > 23D= $mn % $interval != 0
ABCDresult
1XXX1
01XX1
001X1
00011
00000
288 if ( $hr !~ / \A \d{1,2} \z /xms
289 || $hr < 0 || $hr > 23
290 )
291 || ($mn % $interval) != 0;
292  
293 728 my $interval_value = sprintf "%02d%02d", $hr, $mn;
294 728 @temp{ $interval_value } = 1;
295 }
296 else {
297 1 croak "Invalid input: '$item'";
298 };
299 };
300  
301 # Order of items is important, and we treat them as numbers
302 337 my @validated_input = sort keys %temp;
303  
304 337 return @validated_input;
305 }
306  
307 ### PRIVATE INSTANCE METHOD ###
308 #
309 # Explodes stringified range of items using Perl range operator.
310 #
311  
312 143 ▶sub _explode_range {
Subroutine _explode_range: called CC=16 SCAR=30.5
313 143 my ($self, $item) = @_;
314  
315 143 ▶ croak "Invalid input: '$item'"
Branch: 1 branch
BranchTrueFalseTotal
unless $item =~ m[ \A (\d{4}) / (\d{4}) \z ]msx 0 143 143
316 unless $item =~ m{ \A (\d{4}) / (\d{4}) \z }xms;
317  
318 143 my ($first, $last) = ($1, $2);
319 143 my $interval = $self->interval;
320  
321 143 my ($fhr, $fmn) = $first =~ /\A (\d{2}) (\d{2}) \z/xms;
322 143 ▶ croak "Invalid input: '$first'"
Branch: 1 branch
BranchTrueFalseTotal
if $fhr < 0 or $fhr > 23 or $fmn % $interval != 0 0 143 143
Condition33%2/6
$fhr < 0 or $fhr > 23 or $fmn % $interval != 0
l!l&&r!l&&!r
00143
$fhr < 0 or $fhr > 23
l!l&&r!l&&!r
00143
MC/DC0%0/3
$fhr < 0 or $fhr > 23 or $fmn % $interval != 0
$fhr < 0 $fhr > 23 $fmn % $interval != 0
Truth table25%1/4
A or B or C
A= $fhr < 0B= $fhr > 23C= $fmn % $interval != 0
ABCresult
1XX1
01X1
0011
0000
323 if $fhr < 0 || $fhr > 23 or ($fmn % $interval) != 0;
324  
325 143 my ($lhr, $lmn) = $last =~ /\A (\d{2}) (\d{2}) \z/xms;
326 143 ▶ croak "Invalid input: '$last'"
Branch: 1 branch
BranchTrueFalseTotal
if $lhr < 0 or $lhr > 23 or $lmn % $interval != 0 3 140 143
Condition50%3/6
$lhr < 0 or $lhr > 23 or $lmn % $interval != 0
l!l&&r!l&&!r
03140
$lhr < 0 or $lhr > 23
l!l&&r!l&&!r
00143
MC/DC33%1/3
$lhr < 0 or $lhr > 23 or $lmn % $interval != 0
$lhr < 0 $lhr > 23 $lmn % $interval != 0
Truth table50%2/4
A or B or C
A= $lhr < 0B= $lhr > 23C= $lmn % $interval != 0
ABCresult
1XX1
01X1
0011
0000
327 if $lhr < 0 || $lhr > 23 or ($lmn % $interval) != 0;
328  
329 140 ▶ croak "Ending interval can't be less than starting interval"
Branch: 1 branch
BranchTrueFalseTotal
if $lhr < $fhr or $lhr == $fhr and $lmn < $fmn 0 140 140
Condition50%3/6
$lhr < $fhr or $lhr == $fhr and $lmn < $fmn
l!l&&r!l&&!r
00140
$lhr == $fhr and $lmn < $fmn
!ll&&!rl&&r
103370
MC/DC0%0/3
$lhr < $fhr or $lhr == $fhr and $lmn < $fmn
$lhr < $fhr $lhr == $fhr $lmn < $fmn
Truth table50%2/4
A or B and C
A= $lhr < $fhrB= $lhr == $fhrC= $lmn < $fmn
ABCresult
1XX1
0111
00X0
0100
330 if ($lhr < $fhr) || ($lhr == $fhr && $lmn < $fmn);
331  
332 140 my @result;
333 140 my ($hr, $mn) = ($fhr, $fmn);
334  
335 140 while (1) {
336 509 push @result, sprintf "%02d%02d", $hr, $mn;
337  
338 509 ▶ if ($mn < (60 - $interval)) { $mn += $interval }
Branch: 2 branches
BranchTrueFalseTotal
if ($mn < 60 - $interval) { } 208 301 509
elsif ($hr < 23) { } 301 0 301
339 301 elsif ($hr < 23) { $hr++; $mn = 0; }
340 0 else { $hr = $mn = 0; };
341  
342 509 ▶ push @result, sprintf "%02d%02d", $hr, $mn and last
Branch: 1 branch
BranchTrueFalseTotal
if $hr == $lhr and $mn == $lmn 140 369 509
Condition85%6/7
$hr == $lhr && $mn == $lmn && (push(@result, sprintf('%02d%02d', $hr, $mn)) && (last))
!ll
369140
$hr == $lhr and $mn == $lmn
!ll&&!rl&&r
29673140
push @result, sprintf("%02d%02d", $hr, $mn) and last
!ll
0140
MC/DC0%0/3
$hr == $lhr && $mn == $lmn && (push(@result, sprintf('%02d%02d', $hr, $mn)) && (last))
$hr == $lhr $mn == $lmn push @result, sprintf("%02d%02d", $hr, $mn)
Truth table50%2/4
A and B && C
A= $hr == $lhrB= $mn == $lmnC= push @result, sprintf("%02d%02d", $hr, $mn)
ABCresult
0XX0
10X0
1100
1111
343 if ($hr == $lhr && $mn == $lmn);
344 };
345  
346 140 return @result;
347 }
348  
349 ### PRIVATE INSTANCE METHOD ###
350 #
351 # Tests if two values are equal.
352 #
353  
354 656 ▶sub _equal_value {
Subroutine _equal_value: called CC=1 SCAR=0.0
355 656 my ($self, $first, $last) = @_;
356  
357 656 return !!($first eq $last);
358 }
359  
360 ### PRIVATE INSTANCE METHOD ###
361 #
362 # Tests if two values are consequent.
363 #
364  
365 224 ▶sub _next_in_range {
Subroutine _next_in_range: called CC=3 SCAR=11.0
366 224 my ($self, $first, $last) = @_;
367  
368 224 my $interval = $self->interval;
369  
370 224 my ($fhr, $fmn) = $first =~ /\A (\d{2}) (\d{2}) \z/xms;
371 224 my ($lhr, $lmn) = $last =~ /\A (\d{2}) (\d{2}) \z/xms;
372  
373 # Increment by interval
374 224 $fmn += $interval;
375  
376 # Check for overflown value
377 224 ▶ if ($fmn == 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fmn == 60) 134 90 224
378 134 $fhr++;
379 134 $fmn = 0;
380 };
381  
382 224 ▶ return !!($fhr == $lhr && $fmn == $lmn);
Condition100%3/3
$fhr == $lhr && $fmn == $lmn
!ll&&!rl&&r
504170
MC/DC100%2/2
$fhr == $lhr && $fmn == $lmn
$fhr == $lhr $fmn == $lmn
Truth table100%3/3
A && B
A= $fhr == $lhrB= $fmn == $lmn
ABresult
0X0
100
111
383 }
384  
385 ### INTERNAL INSTANCE METHOD ###
386 #
387 # Formats time from 4-digit internal representation to HH:MM
388 #
389  
390 930 ▶sub _colonify {
Subroutine _colonify: called CC=4 SCAR=14.1
391 930 my ($self, $value) = @_;
392  
393 930 my $delim = $self->delimiter();
394  
395 930 ▶ croak "Can't colonify invalid value '$value'"
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A (\d{2}) (\d{2}) \z/msx or $value =~ /\A (\d{2}) (\d{2}) ($delim) (\d{2}) (\d{2}) \z/msx 0 930 930
Condition66%2/3
$value =~ /\A (\d{2}) (\d{2}) \z/msx or $value =~ /\A (\d{2}) (\d{2}) ($delim) (\d{2}) (\d{2}) \z/msx
l!l&&r!l&&!r
8301000
MC/DC0%0/2
$value =~ /\A (\d{2}) (\d{2}) \z/msx or $value =~ /\A (\d{2}) (\d{2}) ($delim) (\d{2}) (\d{2}) \z/msx
$value =~ /\A (\d{2}) (\d{2}) \z/msx $value =~ /\A (\d{2}) (\d{2}) ($delim) (\d{2}) (\d{2}) \z/msx
Truth table66%2/3
A or B
A= $value =~ /\A (\d{2}) (\d{2}) \z/msxB= $value =~ /\A (\d{2}) (\d{2}) ($delim) (\d{2}) (\d{2}) \z/msx
ABresult
1X1
011
000
396 unless $value =~ /\A (\d{2}) (\d{2}) \z/xms
397 || $value =~ /\A (\d{2}) (\d{2}) ($delim) (\d{2}) (\d{2}) \z/xms;
398  
399 930 ▶ return $3 ne $delim
Branch: 1 branch
BranchTrueFalseTotal
$3 ne $delim ? : 830 100 930
400 ? sprintf "%02d:%02d", $1, $2
401 : sprintf "%02d:%02d".$delim."%02d:%02d", $1, $2, $4, $5
402 ;
403 }
404  
405 1 1;
406  
407 __END__
« Object/Date.pm ↑ Summary Object.pm »