Easy/Units.pm

filter: Statementstmt 98.3% 58 / 59 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.6% 22 / 23 Podpod 100% 9 / 9 totaltotal 96.9% 96 / 99 CC 5 SCAR 3.5
CC
5
Cov
97%
new
3.0
__ANON__
2.1
to_string
2.0
CRAP 1.4
« Easy/Date.pm ↑ Summary Easy.pm »
1 package Date::Easy::Units;
2  
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 19 our $VERSION = '0.10'; # VERSION
8  
9 19 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 19 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 19 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 19 my @date_units = qw< days weeks months years >;
14 19 my @datetime_units = (qw< seconds minutes hours >, @date_units);
15 19 our @EXPORT_OK = @datetime_units;
16 19 our %EXPORT_TAGS = ( date => \@date_units, datetime => \@datetime_units, all => \@EXPORT_OK );
17  
18  
19 ##############################
20 # FUNCTIONS (*NOT* METHODS!) #
21 ##############################
22  
23 6 ▶sub seconds () { __PACKAGE__->new("seconds") }
Subroutine seconds: called CC=1 SCAR=0.0
Pod documented
24 6 ▶sub minutes () { __PACKAGE__->new("minutes") }
Subroutine minutes: called CC=1 SCAR=0.0
Pod documented
25 4 ▶sub hours () { __PACKAGE__->new("hours") }
Subroutine hours: called CC=1 SCAR=0.0
Pod documented
26 5 ▶sub days () { __PACKAGE__->new("days") }
Subroutine days: called CC=1 SCAR=0.0
Pod documented
27 4 ▶sub weeks () { __PACKAGE__->new("weeks") }
Subroutine weeks: called CC=1 SCAR=0.0
Pod documented
28 4 ▶sub months () { __PACKAGE__->new("months") }
Subroutine months: called CC=1 SCAR=0.0
Pod documented
29 5 ▶sub years () { __PACKAGE__->new("years") }
Subroutine years: called CC=1 SCAR=0.0
Pod documented
30  
31  
32 #######################
33 # REGULAR CLASS STUFF #
34 #######################
35  
36 sub new
37 53 ▶{
Subroutine new: called CC=3 SCAR=11.0
Pod documented
38 53 my ($class, $unit, $qty) = @_;
39 53 ▶ if (defined $qty)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $qty) { } 19 34 53
40 {
41 19 ▶ croak("can only do integer math") unless $qty == int($qty);
Branch: 1 branch
BranchTrueFalseTotal
unless $qty == int $qty 7 12 19
42 }
43 else
44 {
45 34 $qty = 1;
46 }
47 46 return bless { qty => $qty, unit => $unit }, $class;
48 }
49  
50  
51 sub to_string
52 14 ▶{
Subroutine to_string: called CC=2 SCAR=6.9
Pod documented
53 14 my $self = shift;
54  
55 14 my $qty = $self->{qty};
56 14 my $unit = $self->{unit};
57 14 ▶ $unit =~ s/s$// if $qty == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $qty == 1 7 7 14
58 14 return "$qty $unit";
59 }
60  
61  
62 ########################
63 # OVERLOADED OPERATORS #
64 ########################
65  
66 sub _add_to
67 29 ▶{
Subroutine _add_to: called CC=1 SCAR=0.0
68 29 my ($self, $operand) = @_;
69 29 my $unit = $self->{unit};
70 29 my $qty = $self->{qty};
71 29 my $method = "add_$unit";
72 29 return $operand->$method($qty);
73 }
74  
75 sub _subtract_from
76 16 ▶{
Subroutine _subtract_from: called CC=1 SCAR=0.0
77 16 my ($self, $operand) = @_;
78 16 my $unit = $self->{unit};
79 16 my $qty = $self->{qty};
80 16 my $method = "subtract_$unit";
81 16 return $operand->$method($qty);
82 }
83  
84 use overload
85 0 ▶ '""' => sub { shift->to_string },
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 14 ▶ 'cmp' => sub { $_[0]->to_string cmp $_[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
87  
88 12 ▶ '+' => sub { shift->_add_to( @_ ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
89 12 ▶ '-' => sub { $_[2] ? shift->_subtract_from( @_ ) : croak("can't subtract from a unit") },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 12 12
90 19 ▶ '*' => sub { __PACKAGE__->new( $_[0]->{unit}, $_[0]->{qty} * $_[1] ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
91 19 ;
92  
93  
94  
95 19 1;
96  
97  
98  
99 # ABSTRACT: units objects for easy date/datetime math
100 # COPYRIGHT
101  
102 __END__
« Easy/Date.pm ↑ Summary Easy.pm »