File Coverage

blib/lib/DateTime/Format/Natural/Wrappers.pm
Criterion Covered Total %
statement 31 31 100.0
branch 8 10 80.0
condition n/a
subroutine 7 7 100.0
pod n/a
total 46 48 95.8


line stmt bran cond sub pod time code
1             package DateTime::Format::Natural::Wrappers;
2              
3 26     26   251 use strict;
  26         68  
  26         789  
4 26     26   189 use warnings;
  26         59  
  26         11561  
5              
6             our $VERSION = '0.03';
7              
8             sub _add
9             {
10 2893     2893   7616 my $self = shift;
11 2893         8107 $self->_math(@_);
12             }
13              
14             sub _subtract
15             {
16 3513     3513   11352 my $self = shift;
17 3513         9126 $self->_math(@_);
18             }
19              
20             sub _math
21             {
22 6406     6406   10404 my $self = shift;
23 6406         14122 my ($unit, $value) = @_;
24              
25 6406         59799 my ($method) = (caller(1))[3] =~ /.+::(.+)$/;
26 6406         27071 $method =~ s/^_//;
27              
28 6406 50       21970 $unit .= 's' unless $unit =~ /s$/;
29 6406         32689 $self->{datetime}->$method($unit => $value);
30              
31 6406         7109497 chop $unit;
32 6406         37058 $self->{modified}{$unit}++;
33             }
34              
35             sub _add_or_subtract
36             {
37 3860     3860   7992 my $self = shift;
38              
39 3860 100       10851 if (ref $_[0] eq 'HASH') {
    50          
40 3724         6631 my %opts = %{$_[0]};
  3724         14992  
41 3724 100       14875 if ($opts{when} > 0) {
    100          
42 1671         6598 $self->_add($opts{unit} => $opts{value});
43             }
44             elsif ($opts{when} < 0) {
45 1351         4302 $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         419 $self->_add(@_);
52             }
53             }
54              
55             sub _set
56             {
57 13335     13335   25796 my $self = shift;
58 13335         41154 my %values = @_;
59              
60 13335         55410 $self->{datetime}->set(%values);
61              
62 13335         6667296 foreach my $unit (keys %values) {
63 27122         100797 $self->{modified}{$unit}++;
64             }
65             }
66              
67             1;
68             __END__
69              
70             =head1 NAME
71              
72             DateTime::Format::Natural::Wrappers - Wrappers for DateTime operations
73              
74             =head1 SYNOPSIS
75              
76             Please see the DateTime::Format::Natural documentation.
77              
78             =head1 DESCRIPTION
79              
80             The C<DateTime::Format::Natural::Wrappers> class provides internal wrappers
81             for DateTime operations.
82              
83             =head1 SEE ALSO
84              
85             L<DateTime::Format::Natural>
86              
87             =head1 AUTHOR
88              
89             Steven Schubiger <schubiger@cpan.org>
90              
91             =head1 LICENSE
92              
93             This program is free software; you may redistribute it and/or
94             modify it under the same terms as Perl itself.
95              
96             See L<http://dev.perl.org/licenses/>
97              
98             =cut