Object.pm

filter: Statementstmt 94.4% 137 / 145 Branchbran 86.0% 43 / 50 Conditioncond 72.2% 13 / 18 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 84.3% 27 / 32 Podpod 100% 12 / 12 totaltotal 88.8% 238 / 268 CC 35 SCAR 12.8
CC
35
Cov
91%
_validate_and_expand
9.0
_collapse_range
7.0
stringify_collapsed
3.1
CRAP 3.6
« Object/Interval.pm ↑ Summary Object/Serial.pm »
1 package Range::Object;
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 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶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 7 printf reserved taint closure semicolon);
12 7 ▶no warnings qw(exec newline unopened);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 ### PACKAGE VARIABLE ###
18 #
19 # Version of this module.
20 #
21  
22 # This is for compatibility with older Perls
23 7 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 7 $VERSION = '0.94';
26  
27 ### PUBLIC CLASS METHOD (CONSTRUCTOR) ###
28 #
29 # Initializes new instance of $class from @input_range.
30 #
31  
32 43 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
33 43 my ($class, @input_range) = @_;
34  
35 43 my $self = bless { range => [] }, $class;
36  
37 43 return $self->add(@input_range);
38 }
39  
40 ### PUBLIC INSTANCE METHOD ###
41 #
42 # Validates @input_range of items and adds them to internal storage.
43 #
44  
45 76 ▶sub add {
Subroutine add: called CC=2 SCAR=7.0
Pod documented
46 76 my ($self, @input_range) = @_;
47  
48 # Nothing to do
49 76 ▶ return $self unless @input_range;
Branch: 1 branch
BranchTrueFalseTotal
unless @input_range 0 76 76
50  
51 76 my @validated_input = $self->_validate_and_expand(@input_range);
52  
53 # Expand existing range and overlay the new one
54 40 my %existing_values = map {; "$_" => 1 } $self->_full_range();
55 40 @existing_values{ @validated_input } = (1) x @validated_input;
56  
57 # Collapse resulting hash and replace current range with new values
58 40 $self->{range} = [ $self->_collapse_range( keys %existing_values ) ];
59  
60 40 return $self;
61 }
62  
63 ### PUBLIC INSTANCE METHOD ###
64 #
65 # Removes items in @input_range from internal storage.
66 #
67  
68 8 ▶sub remove {
Subroutine remove: called CC=2 SCAR=7.0
Pod documented
69 8 my ($self, @input_range) = @_;
70  
71 # Nothing to do
72 8 ▶ return $self unless @input_range;
Branch: 1 branch
BranchTrueFalseTotal
unless @input_range 0 8 8
73  
74 8 my @validated_input = $self->_validate_and_expand(@input_range);
75  
76 # Expand existing range and remove what needs to be removed
77 8 my %existing_values = map {; "$_" => 1 } $self->_full_range();
78 8 delete @existing_values{ @validated_input };
79  
80 # Collapse resulting hash and replace current range with new values
81 8 $self->{range} = [ $self->_collapse_range( keys %existing_values ) ];
82  
83 8 return $self;
84 }
85  
86 ### PUBLIC INSTANCE METHOD ###
87 #
88 # Returns sorted array or string representation of internal storage.
89 # In scalar context it can use optional list separator instead of
90 # default one.
91 #
92  
93 48 ▶sub range {
Subroutine range: called CC=2 SCAR=6.9
Pod documented
94 48 my ($self, $separator) = @_;
95  
96 48 ▶ return wantarray ? $self->_sort_range()
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 24 24 48
97 : $self->stringify($separator)
98 ;
99 }
100  
101 ### PUBLIC INSTANCE METHOD ###
102 #
103 # Returns sorted and collapsed representation of internal storage.
104 # In list context, resulting list consists of separate values and/or
105 # range hashrefs with three elements: start, end and count.
106 # In scalar context, result is a string of separate values and/or
107 # ranges separated by value returned by delimiter() method.
108 # Optional list separator can be used instead of default one in
109 # scalar context.
110 #
111  
112 48 ▶sub collapsed {
Subroutine collapsed: called CC=2 SCAR=6.9
Pod documented
113 48 my ($self, $separator) = @_;
114  
115 48 ▶ return wantarray ? @{ $self->{range} }
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 24 24 48
116 : $self->stringify_collapsed($separator)
117 ;
118 }
119  
120 ### PUBLIC INSTANCE METHOD ###
121 #
122 # Returns the number of separate items in internal storage.
123 #
124  
125 42 ▶sub size {
Subroutine size: called CC=3 SCAR=11.0
Pod documented
126 42 my ($self) = @_;
127  
128 42 my $size = 0;
129 42 for my $item ( @{ $self->{range} } ) {
130 236 ▶ $size += ref $item ? $item->{count} : 1;
Branch: 1 branch
BranchTrueFalseTotal
ref $item ? : 100 136 236
131 };
132  
133 42 return $size;
134 }
135  
136 ### PUBLIC INSTANCE METHOD ###
137 #
138 # Tests if items of @input_range are matching items in our internal storage.
139 # Returns true/false in scalar context, list of mismatching items in list
140 # context.
141 #
142  
143 852 ▶sub in {
Subroutine in: called CC=2 SCAR=6.9
Pod documented
144 852 my ($self, @input_range) = @_;
145  
146 852 my @validated_range = $self->_validate_and_expand(@input_range);
147  
148 852 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 42 810 852
149 # This should be reasonably fast
150 42 return grep { !defined $self->_search_range("$_") } @validated_range;
151 }
152 else {
153 # This should be even faster
154 810 ▶ return defined first {
Subroutine __ANON__: called CC=3 SCAR=11.0
155 810 my $result = $self->_search_range("$_");
156 810 ▶ !defined $result ? ''
Branch: 2 branches
BranchTrueFalseTotal
!defined($result) ? : 495 315 810
$result == 0 ? : 31 284 315
157 : $result == 0 ? 1
158 : $result
159 }
160 810 @validated_range;
161 };
162  
163 0 return; # Just for fun
164 }
165  
166 ### PUBLIC INSTANCE METHOD ###
167 #
168 # Returns string representation of all items in internal storage (sorted).
169 #
170  
171 36 ▶sub stringify {
Subroutine stringify: called CC=2 SCAR=7.6
Pod documented
172 36 my ($self, $separator) = @_;
173  
174 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
175  
176 36 return join $separator, $self->_sort_range();
177 }
178  
179 ### PUBLIC INSTANCE METHOD ###
180 #
181 # Returns string representation of collapsed current range.
182 #
183  
184 42 ▶sub stringify_collapsed {
Subroutine stringify_collapsed: called CC=3 SCAR=11.2
Pod documented
185 42 my ($self, $separator) = @_;
186  
187 42 ▶ $separator ||= $self->_list_separator();
Condition33%1/3
$separator ||= $self->_list_separator
l!l&&r!l&&!r
0420
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
188  
189 my @collapsed_range
190 = map {
191 ref($_) ? $self->_stringify_range( $_->{start}, $_->{end} )
192 298 ▶ : "$_"
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 100 198 298
193 }
194 42 @{ $self->{range} };
195  
196 42 return join $separator, @collapsed_range;
197 }
198  
199 ### PUBLIC INSTANCE METHOD ###
200 #
201 # Returns regex that is used to validate range items. Regex should
202 # include patterns both for separate disjointed items and contiguous
203 # ranges.
204 # Default pattern matches everything.
205 #
206  
207 0 ▶sub pattern { qr/.*?/xms }
Subroutine pattern: not called CC=1 SCAR=6.9
Pod documented
208  
209 ### PUBLIC INSTANCE METHOD ###
210 #
211 # Returns regex that is used to separate items in a range list.
212 # Default is no separator.
213 #
214  
215 0 ▶sub separator { qr//xms }
Subroutine separator: not called CC=1 SCAR=6.9
Pod documented
216  
217 ### PUBLIC INSTANCE METHOD ###
218 #
219 # Returns default range delimiter for current class.
220 #
221  
222 0 ▶sub delimiter { '-' }
Subroutine delimiter: not called CC=1 SCAR=6.9
Pod documented
223  
224 ############## PRIVATE METHODS BELOW ##############
225  
226 ### PRIVATE INSTANCE METHOD ###
227 #
228 # Returns default list separator for use with stringify() and
229 # stringify_collapsed()
230 #
231  
232 18 ▶sub _list_separator { q{,} }
Subroutine _list_separator: called CC=1 SCAR=0.0
233  
234 ### PRIVATE INSTANCE METHOD ###
235 #
236 # Validates and unpacks input @input_range of items, returns full list.
237 #
238  
239 576 ▶sub _validate_and_expand {
Subroutine _validate_and_expand: called CC=9 SCAR=22.0
240 576 my ($self, @input_range) = @_;
241  
242 # Nothing to do
243 576 ▶ return unless @input_range;
Branch: 1 branch
BranchTrueFalseTotal
unless @input_range 0 576 576
244  
245 # We need the patterns
246 576 my $pattern = $self->pattern();
247 576 my $separator = $self->separator();
248  
249 # We use hash to avoid duplicates
250 576 my %temp = ();
251  
252 # Expand and validate items in @input_range; add them if all is OK
253 ITEM:
254 576 ▶ while ( @input_range ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@input_range) 1303 557 1860
255 1303 my $item = shift @input_range;
256  
257 1303 ▶ if ( $separator && $item =~ $separator ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($separator and $item =~ /$separator/) 15 1288 1303
Condition66%2/3
$separator and $item =~ /$separator/
!ll&&!rl&&r
0128815
MC/DC50%1/2
$separator and $item =~ /$separator/
$separator $item =~ /$separator/
Truth table66%2/3
A and B
A= $separatorB= $item =~ /$separator/
ABresult
0X0
100
111
258 15 unshift @input_range, split $separator, $item;
259 15 next ITEM;
260 };
261  
262 1288 ▶ croak "Invalid input: $item"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $item or $item eq "" or not $item =~ /$pattern/ 19 1269 1288
Condition100%6/6
not defined $item or $item eq "" or not $item =~ /$pattern/
l!l&&r!l&&!r
3161269
not defined $item or $item eq ""
l!l&&r!l&&!r
121285
MC/DC100%3/3
not defined $item or $item eq "" or not $item =~ /$pattern/
not defined $item $item eq "" not $item =~ /$pattern/
Truth table100%4/4
not(A) or B or not(C)
A= not defined $itemB= $item eq ""C= not $item =~ /$pattern/
ABCresult
1XX1
01X1
0011
0000
263 if !defined $item || $item eq '' || $item !~ /$pattern/;
264  
265 # Default expansion mechanism is Perl range operator (..)
266 1269 my @items = eval { $self->_explode_range($item) };
267 1269 ▶ croak "Invalid input item '$item': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1269 1269
268  
269 # Store result to temp hash, avoiding duplicates
270 1269 @temp{ @items } = (1) x @items;
271 };
272  
273 # We need to sort items because order matters
274 557 my @result = $self->_sort_range( keys %temp );
275  
276 557 return @result;
277 }
278  
279 ### PRIVATE INSTANCE METHOD ###
280 #
281 # Explodes stringified range of items using Perl range operator.
282 #
283  
284 1281 ▶sub _explode_range {
Subroutine _explode_range: called CC=3 SCAR=11.0
285 1281 my ($self, $string) = @_;
286  
287 1281 my $delimiter = $self->delimiter();
288  
289 # Shortcut
290 1281 for ($string) {
291 # Remove whitespace and normalize separators
292 1281 s/\s+//g;
293 1281 s/;/,/g;
294  
295 # Replace delimiters with (..) honoring qw() constructs
296 1281 ▶ s{ \) \s* $delimiter \s* qw\( } {)..qw(}gx
Branch: 1 branch
BranchTrueFalseTotal
unless s/ (\d) \s* $delimiter \s* (\d) /$1..$2/gx 1211 70 1281
297 unless s{ (\d) \s* $delimiter \s* (\d) } {$1..$2}gx;
298 };
299  
300 1281 my $items_ref = eval "[$string]";
301  
302 1281 return @$items_ref;
303 }
304  
305 ### PRIVATE INSTANCE METHOD ###
306 #
307 # Tests if a sigle value is in current range.
308 #
309  
310 1038 ▶sub _search_range {
Subroutine _search_range: called CC=1 SCAR=0.0
311 1038 my ($self, $value) = @_;
312  
313 4336 ▶ return first {
Subroutine __ANON__: called CC=2 SCAR=6.9
314 4336 ▶ ref($_) ? $self->_is_in_range_hashref($_, $value)
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 2060 2276 4336
315 : $self->_equal_value("$_", $value)
316 }
317 1038 @{ $self->{range} };
318 }
319  
320 ### PRIVATE INSTANCE METHOD ###
321 #
322 # Tests if a single value is within boundaries of collapsed range item.
323 # Default method uses string comparison.
324 #
325  
326 1502 ▶sub _is_in_range_hashref {
Subroutine _is_in_range_hashref: called CC=2 SCAR=6.9
327 1502 my ($self, $range_ref, $value) = @_;
328  
329 return ( ($value ge $range_ref->{start})
330 && ($value le $range_ref->{end})
331 1502 ▶ );
Condition100%3/3
$value ge $range_ref->{'start'} && $value le $range_ref->{'end'}
!ll&&!rl&&r
476806220
MC/DC100%2/2
$value ge $range_ref->{'start'} && $value le $range_ref->{'end'}
$value ge $range_ref->{'start'} $value le $range_ref->{'end'}
Truth table100%3/3
A && B
A= $value ge $range_ref->{'start'}B= $value le $range_ref->{'end'}
ABresult
0X0
100
111
332 }
333  
334 ### PRIVATE INSTANCE METHOD ###
335 #
336 # Returns sorted list of all single items within current range.
337 # Default sort is string-based.
338 #
339 # Works in list context only, croaks if called otherwise.
340 #
341  
342 332 ▶sub _sort_range {
Subroutine _sort_range: called CC=3 SCAR=11.0
343 332 my ($self, @range) = @_;
344  
345 332 ▶ croak "Internal error: _sort_range can only be used in list context"
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 332 332
346 unless wantarray;
347  
348 332 ▶ return sort { $a cmp $b } @range ? @range : $self->_full_range();
Branch: 1 branch
BranchTrueFalseTotal
@range ? : 290 42 332
349 }
350  
351 ### PRIVATE INSTANCE METHOD ###
352 #
353 # Returns full list of items in current range.
354 #
355  
356 132 ▶sub _full_range {
Subroutine _full_range: called CC=3 SCAR=11.0
357 132 my ($self) = @_;
358  
359 132 ▶ croak "Internal error: _full_range can only be used in list context"
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 132 132
360 unless wantarray;
361  
362 132 my $delimiter = $self->delimiter();
363  
364 return map {
365 ref($_) ? $self->_explode_range( $_->{start}
366 . $delimiter
367 . $_->{end}
368 )
369 587 ▶ : "$_"
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 236 351 587
370 }
371 132 @{ $self->{range} };
372 }
373  
374 ### PRIVATE INSTANCE METHOD ###
375 #
376 # Returns collapsed list of current range items. Separate items are
377 # returned as is, contiguous ranges are collapsed and returned as
378 # hashrefs { start => $start, end => $end, count => $count }.
379 #
380 # Works in list context only, croaks if called otherwise.
381 #
382  
383 42 ▶sub _collapse_range {
Subroutine _collapse_range: called CC=7 SCAR=19.5
384 42 my ($self, @range) = @_;
385  
386 42 ▶ croak "Internal error: _collapse_range can only be used in list context"
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 42 42
387 unless wantarray;
388  
389 42 my ($first, $last, $count, @result);
390  
391 ITEM:
392 42 for my $item ( $self->_sort_range(@range) ) {
393 # If $first is defined, it means range has started
394 630 ▶ if ( !defined $first ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $first) 42 588 630
395 42 $first = $last = $item;
396 42 $count = 1;
397 42 next ITEM;
398 };
399  
400 # If $last immediately preceeds $item in range,
401 # $item becomes next $last
402 588 ▶ if ( $self->_next_in_range($last, $item) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_next_in_range($last, $item)) 394 194 588
403 394 $last = $item;
404 394 $count++;
405 394 next ITEM;
406 };
407  
408 # If $item doesn't follow $last and $last is defined,
409 # it means current contiguous range is complete
410 194 ▶ if ( !$self->_equal_value($first, $last) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_equal_value($first, $last)) 88 106 194
411 88 push @result, {
412 start => $first,
413 end => $last,
414 count => $count,
415 };
416 88 $first = $last = $item;
417 88 $count = 1;
418 88 next ITEM;
419 };
420  
421 # If $last wasn't defined, range was never contiguous
422 106 push @result, "$first";
423 106 $first = $last = $item;
424 106 $count = 1;
425 }
426  
427 # We're here when last item has been processed
428 42 ▶ push @result,
Branch: 1 branch
BranchTrueFalseTotal
$self->_equal_value($first, $last) ? : 30 12 42
429 $self->_equal_value($first, $last) ? "$first"
430 : {
431 start => $first,
432 end => $last,
433 count => $count,
434 }
435 ;
436  
437 42 return @result;
438 }
439  
440 ### PRIVATE INSTANCE METHOD ###
441 #
442 # Tests if two values are equal. This method has to be overridden.
443 #
444  
445 0 ▶sub _equal_value {
Subroutine _equal_value: not called CC=1 SCAR=6.9
446 0 my ($self, $first, $second) = @_;
447  
448 0 croak "Internal error: Can't use _equal_value with Range::Object";
449 }
450  
451 ### PRIVATE INSTANCE METHOD ###
452 #
453 # Tests if two values are consequent. This method has to be overridden.
454 #
455  
456 0 ▶sub _next_in_range {
Subroutine _next_in_range: not called CC=1 SCAR=6.9
457 0 my ($self, $first, $second) = @_;
458  
459 0 croak "Internal error: Can't use _next_in_range with Range::Object";
460 }
461  
462 ### PRIVATE INSTANCE METHOD ###
463 #
464 # Returns stringified representation of a range within $first and $last
465 # boundaries.
466 #
467  
468 225 ▶sub _stringify_range {
Subroutine _stringify_range: called CC=1 SCAR=0.0
469 225 my ($self, $first, $last) = @_;
470  
471 225 my $delimiter = $self->delimiter();
472  
473 225 return $first . $delimiter . $last;
474 }
475  
476 7 1;
477  
478 __END__
« Object/Interval.pm ↑ Summary Object/Serial.pm »