Natural/Wrappers.pm

filter: Statementstmt 100% 33 / 33 Branchbran 80.0% 8 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 96.0% 48 / 50 CC 7 SCAR 10.5
CC
7
Cov
95%
_add_or_subtract
5.0
_math
2.0
_set
2.0
CRAP 2.8
« Natural/Calc.pm ↑ Summary Natural/Lang/Base.pm »
1 package DateTime::Format::Natural::Wrappers;
2  
3 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 27 our $VERSION = '0.03';
7  
8 sub _add
9 2975 ▶{
Subroutine _add: called CC=1 SCAR=0.0
10 2975 my $self = shift;
11 2975 $self->_math(@_);
12 }
13  
14 sub _subtract
15 3491 ▶{
Subroutine _subtract: called CC=1 SCAR=0.0
16 3491 my $self = shift;
17 3491 $self->_math(@_);
18 }
19  
20 sub _math
21 6466 ▶{
Subroutine _math: called CC=2 SCAR=7.0
22 6466 my $self = shift;
23 6466 my ($unit, $value) = @_;
24  
25 6466 my ($method) = (caller(1))[3] =~ /.+::(.+)$/;
26 6466 $method =~ s/^_//;
27  
28 6466 ▶ $unit .= 's' unless $unit =~ /s$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $unit =~ /s$/ 6466 0 6466
29 6466 $self->{datetime}->$method($unit => $value);
30  
31 6466 chop $unit;
32 6466 $self->{modified}{$unit}++;
33 }
34  
35 sub _add_or_subtract
36 3944 ▶{
Subroutine _add_or_subtract: called CC=5 SCAR=16.1
37 3944 my $self = shift;
38  
39 3944 ▶ if (ref $_[0] eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 3808 136 3944
elsif (@_ == 2) { } 136 0 136
40 3808 my %opts = %{$_[0]};
41 3808 ▶ if ($opts{when} > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'when'} > 0) { } 1683 2125 3808
elsif ($opts{'when'} < 0) { } 1387 738 2125
42 1683 $self->_add($opts{unit} => $opts{value});
43 }
44 elsif ($opts{when} < 0) {
45 1387 $self->_subtract($opts{unit} => $opts{value});
46 }
47 }
48 elsif (@_ == 2) {
49 # Handle additions as expected and also subtractions
50 # as the inverse result of adding a negative number.
51 136 $self->_add(@_);
52 }
53 }
54  
55 sub _set
56 13694 ▶{
Subroutine _set: called CC=2 SCAR=6.9
57 13694 my $self = shift;
58 13694 my %values = @_;
59  
60 13694 $self->{datetime}->set(%values);
61  
62 13694 foreach my $unit (keys %values) {
63 28053 $self->{modified}{$unit}++;
64 }
65 }
66  
67 27 1;
68 __END__
« Natural/Calc.pm ↑ Summary Natural/Lang/Base.pm »