blib/lib/Badger/Duration.pm

filter: Statementstmt 94.2% 66 / 70 Branchbran 70.8% 17 / 24 Conditioncond 44.8% 13 / 29 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 87.5% 7 / 8 Podpod 100% 7 / 7 totaltotal 73.0% 111 / 152 CC 26 SCAR 19.0
CC
26
Cov
78%
parse_text
11.5
parse_hash
6.6
count_seconds
6.6
CRAP 6.7
« lib/Badger/Codec/HTML.pm ↑ Summary blib/lib/Badger/Workplace.pm »
1 package Badger::Duration;
2  
3 use Badger::Class
4 1 version => 0.01,
5 debug => 0,
6 base => 'Badger::Comparable',
7 import => 'CLASS',
8 utils => 'numlike is_object params inflect',
9 as_text => 'text',
10 is_true => 1,
11 accessors => 'duration seconds',
12 constants => 'HASH DELIMITER',
13 constant => {
14 DURATION => 'Badger::Duration',
15 },
16 exports => {
17 any => 'DURATION Duration',
18 1 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 our $SECONDS = {
21 second => 1,
22 minute => 60,
23 hour => 60*60,
24 day => 60*60*24,
25 week => 60*60*24*7,
26 month => 60*60*24*30,
27 year => 60*60*24*365,
28 };
29  
30 1 our $ALIAS_LIST = {
31 second => 's sec secs second seconds',
32 minute => 'm min mins minute minutes',
33 hour => 'h hr hrs hour hours',
34 day => 'd day days',
35 week => 'w wk week weeks',
36 month => 'M mon mons month months',
37 year => 'y yr yrs year years',
38 };
39  
40 our $ALIASES = {
41 map {
42 1 my $key = $_;
43 7 my $aliases = $ALIAS_LIST->{ $key };
44 7 map { $_ => $key }
45 split(/\s+/, $aliases);
46 }
47 keys %$ALIAS_LIST
48 };
49  
50 1 our @ORDER = qw( year month day hour minute second );
51  
52  
53 sub Duration {
54 7 ▶ return DURATION unless @_;
Subroutine Duration: called CC=4 SCAR=14.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 7 7
55 7 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 6 1 7
56 6 ▶ if (is_object(DURATION, $_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_object('Badger::Duration', $_[0])) { } 0 6 6
57 0 return $_[0];
58 }
59 else {
60 6 return DURATION->new( duration => $_[0] );
61 }
62 }
63 else {
64 1 return DURATION->new( duration => params(@_) );
65 }
66 }
67  
68 sub init {
69 7 ▶ my ($self, $config) = @_;
Subroutine init: called CC=3 SCAR=11.1
Pod documented
70 7 ▶ my $duration = $config->{ duration } || $config;
Condition33%1/3
$config->{'duration'} || $config
l!l&&r!l&&!r
700
MC/DC0%0/2
$config->{'duration'} || $config
$config->{'duration'} $config
Truth table33%1/3
A || B
A= $config->{'duration'}B= $config
ABresult
1X1
011
000
71  
72 7 $self->debug(
73 "init() : ", $self->dump_data($duration)
74 ) if DEBUG;
75  
76 7 ▶ if (ref $duration eq HASH) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $duration eq 'HASH') { } 2 5 7
77 2 $duration = $self->parse_hash($duration);
78 }
79 else {
80 5 $duration = $self->parse_text($duration);
81 }
82  
83 7 $self->{ seconds } = $self->count_seconds($duration);
84 7 $self->{ duration } = $duration;
85  
86 7 return $self;
87 }
88  
89 sub parse_text {
90 5 ▶ my ($self, $duration) = @_;
Subroutine parse_text: called CC=9 SCAR=24.4
Pod documented
91 5 my $bits = { };
92  
93 # $duration can be a number, assumed to be seconds
94 5 ▶ return { seconds => $duration }
Branch: 1 branch
BranchTrueFalseTotal
if &numlike($duration) 0 5 5
95 if numlike($duration);
96  
97 5 ▶ while ($duration =~ /\G\s*(-?[\d\.]+)\s*(\w+)\s*(,|and)?\s*/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duration =~ /\G\s*(-?[\d\.]+)\s*(\w+)\s*(,|and)?\s*/cg) 9 5 14
98 9 $self->debug("PARSE [$1] [$2]") if DEBUG;
99 9 ▶ my $name = $ALIASES->{ $2 } || $ALIASES->{ lc $2 }
Condition40%2/5
$Badger::Duration::ALIASES->{$2} || $$Badger::Duration::ALIASES{lc $2} || (return $self->error_msg('invalid', 'duration', "$1 $2"))
l!l
90
$Badger::Duration::ALIASES->{$2} or $$Badger::Duration::ALIASES{lc $2}
l!l&&r!l&&!r
900
MC/DC0%0/2
$Badger::Duration::ALIASES->{$2} || $$Badger::Duration::ALIASES{lc $2} || (return $self->error_msg('invalid', 'duration', "$1 $2"))
$Badger::Duration::ALIASES->{$2} $$Badger::Duration::ALIASES{lc $2}
Truth table33%1/3
A or B
A= $Badger::Duration::ALIASES->{$2}B= $$Badger::Duration::ALIASES{lc $2}
ABresult
1X1
011
000
100 || return $self->error_msg( invalid => duration => "$1 $2" );
101 9 ▶ my $old = $bits->{ $name } || 0;
Condition50%1/2
$bits->{$name} || 0
l!l
09
MC/DC0%0/1
$bits->{$name} || 0
$bits->{$name}
Truth table50%1/2
A
A= $bits->{$name}
Aresult
11
00
102 9 my $new = $1;
103 9 ▶ return $self->error_msg( invalid => duration => "$1 $2" )
Branch: 1 branch
BranchTrueFalseTotal
unless &numlike($new) 0 9 9
104 unless numlike($new);
105 9 $bits->{ $name } += $new;
106 9 $self->debug("+ $new $name") if DEBUG;
107 }
108  
109 5 ▶ if ($duration =~ /\G\s*(.*)/gc && length $1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duration =~ /\G\s*(.*)/cg and length $1) 0 5 5
Condition33%1/3
$duration =~ /\G\s*(.*)/cg and length $1
!ll&&!rl&&r
050
MC/DC0%0/2
$duration =~ /\G\s*(.*)/cg and length $1
$duration =~ /\G\s*(.*)/cg length $1
Truth table33%1/3
A and B
A= $duration =~ /\G\s*(.*)/cgB= length $1
ABresult
0X0
100
111
110 0 return $self->error_msg( invalid => duration => $1 );
111 }
112  
113 5 return $bits;
114 }
115  
116 sub parse_hash {
117 2 ▶ my ($self, $duration) = @_;
Subroutine parse_hash: called CC=6 SCAR=18.8
Pod documented
118 2 my $bits = { };
119  
120 2 ▶ while (my ($key, $value) = each %$duration) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$duration) 12 2 14
121 12 ▶ my $name = $ALIASES->{ $key } || $ALIASES->{ lc $key }
Condition40%2/5
$Badger::Duration::ALIASES->{$key} || $$Badger::Duration::ALIASES{lc $key} || (return $self->error_msg('invalid', 'duration', $key))
l!l
120
$Badger::Duration::ALIASES->{$key} or $$Badger::Duration::ALIASES{lc $key}
l!l&&r!l&&!r
1200
MC/DC0%0/2
$Badger::Duration::ALIASES->{$key} || $$Badger::Duration::ALIASES{lc $key} || (return $self->error_msg('invalid', 'duration', $key))
$Badger::Duration::ALIASES->{$key} $$Badger::Duration::ALIASES{lc $key}
Truth table33%1/3
A or B
A= $Badger::Duration::ALIASES->{$key}B= $$Badger::Duration::ALIASES{lc $key}
ABresult
1X1
011
000
122 || return $self->error_msg( invalid => duration => $key );
123 12 ▶ my $old = $bits->{ $name } || 0;
Condition50%1/2
$bits->{$name} || 0
l!l
012
MC/DC0%0/1
$bits->{$name} || 0
$bits->{$name}
Truth table50%1/2
A
A= $bits->{$name}
Aresult
11
00
124 12 ▶ return $self->error_msg( invalid => duration => "$key => $value" )
Branch: 1 branch
BranchTrueFalseTotal
unless &numlike($value) 0 12 12
125 unless numlike($value);
126 12 $bits->{ $name } += $value;
127 12 $self->debug("+ $value $name") if DEBUG;
128 }
129 2 return $bits;
130 }
131  
132 sub count_seconds {
133 7 ▶ my ($self, $bits) = @_;
Subroutine count_seconds: called CC=6 SCAR=18.8
Pod documented
134 7 my $seconds = 0;
135  
136 7 ▶ while (my ($key, $value) = each %$bits) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$bits) 21 7 28
137 21 ▶ my $name = $ALIASES->{ $key } || $ALIASES->{ lc $key }
Condition40%2/5
$Badger::Duration::ALIASES->{$key} || $$Badger::Duration::ALIASES{lc $key} || (return $self->error_msg('invalid', 'duration', $key))
l!l
210
$Badger::Duration::ALIASES->{$key} or $$Badger::Duration::ALIASES{lc $key}
l!l&&r!l&&!r
2100
MC/DC0%0/2
$Badger::Duration::ALIASES->{$key} || $$Badger::Duration::ALIASES{lc $key} || (return $self->error_msg('invalid', 'duration', $key))
$Badger::Duration::ALIASES->{$key} $$Badger::Duration::ALIASES{lc $key}
Truth table33%1/3
A or B
A= $Badger::Duration::ALIASES->{$key}B= $$Badger::Duration::ALIASES{lc $key}
ABresult
1X1
011
000
138 || return $self->error_msg( invalid => duration => $key );
139 21 $self->debug("COUNT [$key => $name] [$value]") if DEBUG;
140 21 ▶ my $secs = $SECONDS->{ $name }
Condition50%1/2
$Badger::Duration::SECONDS->{$name} || (return $self->error_msg('invalid', 'duration', "$key -> $name"))
l!l
210
MC/DC0%0/1
$Badger::Duration::SECONDS->{$name} || (return $self->error_msg('invalid', 'duration', "$key -> $name"))
$Badger::Duration::SECONDS->{$name}
Truth table50%1/2
A
A= $Badger::Duration::SECONDS->{$name}
Aresult
11
00
141 || return $self->error_msg( invalid => duration => "$key -> $name" );
142 21 ▶ return $self->error_msg( invalid => duration => "$value $key" )
Branch: 1 branch
BranchTrueFalseTotal
unless &numlike($value) 0 21 21
143 unless numlike($value);
144 21 $seconds += $secs * $value;
145 }
146  
147 7 return $seconds;
148 }
149  
150 sub compare {
151 0 ▶ my ($this, $that) = @_;
Subroutine compare: not called CC=1 SCAR=6.9
Pod documented
152 0 return $this->{ seconds } <=> $that->{ seconds };
153 }
154  
155 sub text {
156 6 ▶ my $self = shift;
Subroutine text: called CC=3 SCAR=11.0
Pod documented
157 6 my $duration = $self->{ duration };
158 6 my @bits;
159  
160 6 $self->debug("DURATION: ", $self->dump_data($duration)) if DEBUG;
161  
162 6 foreach my $item (@ORDER) {
163 36 ▶ my $value = $duration->{ $item } || next;
Condition100%2/2
$duration->{$item} || (next)
l!l
1917
MC/DC100%1/1
$duration->{$item} || (next)
$duration->{$item}
Truth table100%2/2
A
A= $duration->{$item}
Aresult
11
00
164 19 push(@bits, inflect($value, $item));
165 19 $self->debug("DURATION + $value $item") if DEBUG;
166 }
167 6 return join(' ', @bits);
168 }
169  
170  
171  
172 1 1;
173 __END__
« lib/Badger/Codec/HTML.pm ↑ Summary blib/lib/Badger/Workplace.pm »