lib/Badger/Period.pm

filter: Statementstmt 76.0% 76 / 100 Branchbran 45.6% 21 / 46 Conditioncond 39.6% 25 / 63 MC/DCmcdc 5.5% 2 / 36 Subroutinesub 73.6% 14 / 19 Podpod 12.5% 2 / 16 totaltotal 50.0% 140 / 280 CC 49 SCAR 25.7
CC
49
Cov
58%
leap_year
30.0
days_in_month
23.6
duration
20.0
CRAP 13.1
« lib/Badger/Test.pm ↑ Summary lib/Badger/Filesystem/Virtual.pm »
1 package Badger::Period;
2  
3 use Badger::Class
4 2 ▶ version => 0.01,
Subroutine BEGIN: called CC=1 SCAR=0.0
5 debug => 0,
6 base => 'Badger::Comparable',
7 utils => 'numlike is_object',
8 accessors => 'uri',
9 as_text => 'uri',
10 is_true => 1,
11 constants => 'HASH ARRAY DELIMITER',
12 constant => {
13 FIELD_NAMES => undef,
14 TYPE_NAME => 'period',
15 },
16 messages => {
17 bad_type => 'Invalid %s: %s',
18 bad_duration => 'Invalid duration: %s',
19 2 };
20  
21 2 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use POSIX 'strftime';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 our @YMD = qw( year month day );
25 2 our @HMS = qw( hour minute second );
26 2 our @SMHD = qw( second minute hour day );
27 2 our @YMDHMS = (@YMD, @HMS);
28 2 our $SECONDS = {
29 s => 1,
30 m => 60,
31 h => 60*60,
32 d => 60*60*24,
33 M => 60*60*24*30,
34 y => 60*60*24*365,
35 };
36  
37  
38 0 ▶sub split_regex {
Subroutine split_regex: not called CC=1 SCAR=6.9
Pod undocumented
39 0 shift->not_implemented;
40 }
41  
42  
43 0 ▶sub join_format {
Subroutine join_format: not called CC=1 SCAR=6.9
Pod undocumented
44 0 shift->not_implemented;
45 }
46  
47  
48 1 ▶sub text_format {
Subroutine text_format: called CC=1 SCAR=0.0
Pod undocumented
49 1 shift->join_format;
50 }
51  
52  
53 28 ▶sub field_names {
Subroutine field_names: called CC=4 SCAR=14.9
Pod undocumented
54 28 my $class = shift;
55 28 ▶ my $names = $class->FIELD_NAMES
Condition50%1/2
$class->FIELD_NAMES || (return $class->not_implemented)
l!l
280
MC/DC0%0/1
$class->FIELD_NAMES || (return $class->not_implemented)
$class->FIELD_NAMES
Truth table50%1/2
A
A= $class->FIELD_NAMES
Aresult
11
00
56 || return $class->not_implemented;
57  
58 28 ▶ $names = [ split(DELIMITER, $names) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $names eq "ARRAY" 28 0 28
59 unless ref $names eq ARRAY;
60  
61 return wantarray
62 28 ▶ ? @$names
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 28 0 28
63 : \@$names
64 }
65  
66  
67 #-----------------------------------------------------------------------
68 # Methods
69 #-----------------------------------------------------------------------
70  
71 5 ▶sub new {
Subroutine new: called CC=13 SCAR=28.0
Pod documented
72 5 my $class = shift;
73 5 my @fields = $class->field_names;
74 5 ▶ my $self = bless { map { ($_, 0) } @fields }, ref $class || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
140
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
75 5 my ($config, $time);
76
77 5 ▶ if (@_ > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ > 1) { } 0 5 5
elsif (@_ == 1 and defined $_[0]) { } 3 2 5
78 # multiple arguments are named params
79 0 $config = { @_ };
80 }
81 ▶ elsif (@_ == 1 && defined $_[0]) {
Condition66%2/3
@_ == 1 and defined $_[0]
!ll&&!rl&&r
203
MC/DC50%1/2
@_ == 1 and defined $_[0]
@_ == 1 defined $_[0]
Truth table66%2/3
A and B
A= @_ == 1B= defined $_[0]
ABresult
0X0
100
111
82 # single argument is a hash of named params, a timestamp or time in
83 # seconds since the epoch
84 3 ▶ $config = ref $_[0] eq HASH ? shift : { time => shift };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 3 3
85 }
86 # otherwise we default to now
87 else {
88 2 $config = { time => time() };
89 }
90  
91 5 ▶ if ($time = $config->{ time }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($time = $config->{"time"}) 5 0 5
92 5 ▶ if (numlike $time) {
Branch: 2 branches
BranchTrueFalseTotal
if (&numlike($time)) { } 2 3 5
elsif (&is_object(scalar(ref $class || $class), $time)) { } 0 3 3
93 # $time is seconds since epoch
94 2 (@$config{ @YMDHMS }) = reverse( ( localtime($time) )[0..5] );
95 2 $config->{ year }+= 1900;
96 2 $config->{ month }++;
97 2 $config->{ etime } = $time;
98 }
99 ▶ elsif (is_object(ref $class || $class, $time)) {
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
120
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
100 0 $config->{ uri } = $time->uri;
101 0 $config->{ etime } = $time->epoch_time;
102 0 $self->split_uri($config->{ uri }, $config);
103 }
104 else {
105 # $time is a timestamp so split and rejoin into canonical form
106 3 $config->{ uri } = $time;
107 3 $self->split_uri($config);
108 }
109 4 $self->join_uri($config);
110 }
111  
112 # set any fields defined in config, allowing singular (second,month,
113 # etc) and plural (seconds, months, etc)
114 4 foreach my $field (@fields) {
115 12 ▶ $self->{ $field } = $config->{ $field } || $config->{"${field}s"} || 0;
Condition40%2/5
$config->{$field} || $$config{"${field}s"} || 0
l!l
120
$config->{$field} or $$config{"${field}s"}
l!l&&r!l&&!r
1200
MC/DC0%0/2
$config->{$field} || $$config{"${field}s"} || 0
$config->{$field} $$config{"${field}s"}
Truth table33%1/3
A or B
A= $config->{$field}B= $$config{"${field}s"}
ABresult
1X1
011
000
116 }
117  
118 4 $self->join_uri;
119  
120 4 return $self;
121 }
122  
123  
124 1 ▶sub copy {
Subroutine copy: called CC=1 SCAR=0.0
Pod undocumented
125 1 my $self = shift;
126 1 $self->new( $self->{ uri } );
127 }
128  
129  
130 3 ▶sub split_uri {
Subroutine split_uri: called CC=4 SCAR=14.2
Pod undocumented
131 3 my $self = shift;
132 3 ▶ my $target = shift || $self;
Condition33%1/3
shift() || $self
l!l&&r!l&&!r
300
MC/DC0%0/2
shift() || $self
shift() $self
Truth table33%1/3
A || B
A= shift()B= $self
ABresult
1X1
011
000
133 3 my $regex = $self->split_regex;
134 3 my @fields = $self->field_names;
135  
136 3 ▶ $target->{ uri } = '' unless defined $target->{ uri };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target->{"uri"} 0 3 3
137  
138 6 (@$target{ @fields } = map { 0+$_ } $target->{ uri } =~ m/$regex/o)
139 3 ▶ || return $self->error_msg( bad_type => $self->TYPE_NAME, $target->{ uri } );
Branch: 1 branch
BranchTrueFalseTotal
@$target{@fields} = map({0 + $_;} $target->{"uri"} =~ /$regex/o) or return $self->error_msg("bad_type", $self->TYPE_NAME, $target->{"uri"}) 2 1 3
140 }
141  
142  
143 9 ▶sub join_uri {
Subroutine join_uri: called CC=3 SCAR=11.2
Pod undocumented
144 9 my $self = shift;
145 9 ▶ my $target = shift || $self;
Condition66%2/3
shift() || $self
l!l&&r!l&&!r
450
MC/DC0%0/2
shift() || $self
shift() $self
Truth table66%2/3
A || B
A= shift()B= $self
ABresult
1X1
011
000
146 9 my @fields = $self->field_names;
147  
148 return ($target->{ uri } = sprintf(
149 $self->join_format,
150 27 ▶ map { defined $_ ? $_ : 0 }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 27 0 27
151 9 @$target{ @fields }
152 ));
153 }
154  
155  
156 0 ▶sub epoch_time {
Subroutine epoch_time: not called CC=2 SCAR=17.9
Pod undocumented
157 0 my $self = shift;
158  
159 0 ▶ return $self->{ etime } ||= timelocal(
Condition0%0/3
$self->{"etime"} ||= timelocal($self->posix_args)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"etime"} ||= timelocal($self->posix_args)
$self->{"etime"} timelocal($self->posix_args)
Truth table0%0/3
A ||= B
A= $self->{"etime"}B= timelocal($self->posix_args)
ABresult
1X1
011
000
160 $self->posix_args
161 );
162 }
163  
164  
165 1 ▶sub posix_args {
Subroutine posix_args: called CC=4 SCAR=14.3
Pod undocumented
166 1 my $self = shift;
167 return (
168 (
169 4 ▶ map { $self->{ $_ } || 0 }
Condition66%4/6
$self->{$_} || 0
l!l
13
$self->{'month'} || 1
l!l
10
$self->{'year'} || 1900
l!l
10
MC/DC100%1/1
$self->{$_} || 0
$self->{$_}
MC/DC0%0/1
$self->{'month'} || 1
$self->{'month'}
MC/DC0%0/1
$self->{'year'} || 1900
$self->{'year'}
Truth table100%2/2
A
A= $self->{$_}
Aresult
11
00
Truth table50%1/2
A
A= $self->{'month'}
Aresult
11
00
Truth table50%1/2
A
A= $self->{'year'}
Aresult
11
00
170 @SMHD
171 ),
172 ($self->{ month } || 1) - 1,
173 1 ($self->{ year } || 1900) - 1900
174 );
175 }
176  
177  
178 1 ▶sub format {
Subroutine format: called CC=1 SCAR=0.0
Pod undocumented
179 1 my $self = shift;
180 1 my $fmt = shift;
181 1 return strftime(
182 $fmt,
183 $self->posix_args
184 );
185 }
186  
187  
188 1 ▶sub text {
Subroutine text: called CC=2 SCAR=7.6
Pod undocumented
189 1 my $self = shift;
190 1 my @fields = $self->field_names;
191 return $self->{ text }
192 1 ▶ ||= sprintf( $self->text_format, @$self{ @fields } );
Condition33%1/3
$self->{"text"} ||= sprintf($self->text_format, @$self{@fields})
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"text"} ||= sprintf($self->text_format, @$self{@fields})
$self->{"text"} sprintf($self->text_format, @$self{@fields})
Truth table33%1/3
A ||= B
A= $self->{"text"}B= sprintf($self->text_format, @$self{@fields})
ABresult
1X1
011
000
193 }
194  
195  
196  
197 0 ▶sub duration {
Subroutine duration: not called CC=4 SCAR=30.0
Pod undocumented
198 0 my ($self, $duration) = @_;
199  
200 # $duration can be a number, assumed to be seconds
201 0 ▶ return $duration
Branch: 1 branch
BranchTrueFalseTotal
if &numlike($duration) 0 0 0
202 if numlike($duration);
203  
204 # Otherwise the $duration should be of the form "3 minutes". We only
205 # look at the first character of the word (e.g. "3 m"), which creates a
206 # potential conflict between "m(inute) and m(onth)". So we use a capital
207 # 'M' for month. This is based on code by Mark Fisher in CGI.pm.
208  
209 0 $duration =~ s/month/Month/i;
210  
211 # TODO: make this parser a bit smarter so we can support multiple
212 # items (e.g. "2 hours 30 minutes") as per adjust()
213 0 ▶ if ($duration =~ /^ ( -? (?: \d+ | \d*\.\d+ ) ) \s* ([smhdMy]?) /x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duration =~ /^ ( -? (?: \d+ | \d*\.\d+ ) ) \s* ([smhdMy]?) /x) { } 0 0 0
214 0 ▶ return ($SECONDS->{ $2 } || 1) * $1;
Condition0%0/2
$Badger::Period::SECONDS->{$2} || 1
l!l
00
MC/DC0%0/1
$Badger::Period::SECONDS->{$2} || 1
$Badger::Period::SECONDS->{$2}
Truth table0%0/2
A
A= $Badger::Period::SECONDS->{$2}
Aresult
11
00
215 }
216 else {
217 0 return $self->error_msg( bad_duration => $duration );
218 }
219 }
220  
221  
222 10 ▶sub compare {
Subroutine compare: called CC=10 SCAR=27.4
Pod documented
223 10 my $self = shift;
224 10 my @fields = $self->field_names;
225  
226 # optimisation: if the $self object has an epoch time and a single
227 # numerical argument is passed (also an epoch time) then we can do a
228 # simple comparison
229 return $self->{ etime } <=> $_[0]
230 if $self->{ etime }
231 10 ▶ && @_ == 1
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"etime"} and @_ == 1 and &numlike($_[0]) 0 10 10
Condition33%2/6
$self->{"etime"} and @_ == 1 and &numlike($_[0])
!ll&&!rl&&r
1000
$self->{"etime"} and @_ == 1
!ll&&!rl&&r
1000
MC/DC0%0/3
$self->{"etime"} and @_ == 1 and &numlike($_[0])
$self->{"etime"} @_ == 1 &numlike($_[0])
Truth table25%1/4
A and B and C
A= $self->{"etime"}B= @_ == 1C= &numlike($_[0])
ABCresult
0XX0
10X0
1100
1111
232 && numlike $_[0];
233  
234 # otherwise we upgrade any argument(s) to another timestamp and comare
235 # them piecewise
236 10 ▶ my $comp = @_ && is_object(ref $self || $self, $_[0])
Branch: 1 branch
BranchTrueFalseTotal
@_ && &is_object(scalar(ref $self || $self), $_[0]) ? : 10 0 10
Condition33%2/6
@_ and &is_object(scalar(ref $self || $self), $_[0])
!ll&&!rl&&r
0010
ref $self || $self
l!l&&r!l&&!r
1000
MC/DC0%0/2
@_ and &is_object(scalar(ref $self || $self), $_[0])
@_ &is_object(scalar(ref $self || $self), $_[0])
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A and B
A= @_B= &is_object(scalar(ref $self || $self), $_[0])
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
237 ? shift
238 : $self->new(@_);
239
240 10 foreach my $item (@fields) {
241 30 ▶ if ($self->{ $item } < $comp->{ $item }) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{$item} < $comp->{$item}) { } 5 25 30
elsif ($self->{$item} > $comp->{$item}) { } 5 20 25
242 5 return -1; # -1 - self earlier than comparison timestamp
243 }
244 elsif ($self->{ $item } > $comp->{ $item }) {
245 5 return 1; # 1 - self later than comparison timestamp
246 }
247 }
248 0 return 0; # 0 - same time
249 }
250  
251  
252 1 ▶sub days_in_month {
Subroutine days_in_month: called CC=8 SCAR=31.6
Pod undocumented
253 1 my $self = shift;
254 1 ▶ my $month = shift || $self->{ month };
Condition33%1/3
shift() || $self->{'month'}
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || $self->{'month'}
shift() $self->{'month'}
Truth table33%1/3
A || B
A= shift()B= $self->{'month'}
ABresult
1X1
011
000
255  
256 1 ▶ if ($month == 4 || $month == 6 || $month == 9 || $month == 11) {
Branch: 2 branches
BranchTrueFalseTotal
if ($month == 4 or $month == 6 or $month == 9 or $month == 11) { } 1 0 1
elsif ($month == 2) { } 0 0 0
Condition33%3/9
$month == 4 or $month == 6 or $month == 9 or $month == 11
l!l&&r!l&&!r
010
$month == 4 or $month == 6 or $month == 9
l!l&&r!l&&!r
001
$month == 4 or $month == 6
l!l&&r!l&&!r
001
MC/DC0%0/4
$month == 4 or $month == 6 or $month == 9 or $month == 11
$month == 4 $month == 6 $month == 9 $month == 11
Truth table20%1/5
A or B or C or D
A= $month == 4B= $month == 6C= $month == 9D= $month == 11
ABCDresult
1XXX1
01XX1
001X1
00011
00000
257 1 return 30;
258 }
259 elsif ($month == 2) {
260 0 ▶ return $self->leap_year(@_) ? 29 : 28;
Branch: 1 branch
BranchTrueFalseTotal
$self->leap_year(@_) ? : 0 0 0
261 }
262 else {
263 0 return 31;
264 }
265 }
266  
267  
268 0 ▶sub leap_year {
Subroutine leap_year: not called CC=5 SCAR=34.0
Pod undocumented
269 0 my $self = shift;
270 0 ▶ my $year = shift || $self->{ year };
Condition0%0/3
shift() || $self->{'year'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->{'year'}
shift() $self->{'year'}
Truth table0%0/3
A || B
A= shift()B= $self->{'year'}
ABresult
1X1
011
000
271  
272 0 ▶ if ($year % 4) {
Branch: 3 branches
BranchTrueFalseTotal
if ($year % 4) { } 0 0 0
elsif ($year % 400 == 0) { } 0 0 0
elsif ($year % 100 == 0) { } 0 0 0
273 0 return 0;
274 }
275 elsif ($year % 400 == 0) {
276 0 return 1;
277 }
278 elsif ($year % 100 == 0) {
279 0 return 0;
280 }
281 else {
282 0 return 1;
283 }
284 }
285  
286  
287 2 1;
288 __END__
« lib/Badger/Test.pm ↑ Summary lib/Badger/Filesystem/Virtual.pm »