File Coverage

blib/lib/DateTime/Format/Natural/Utils.pm
Criterion Covered Total %
statement 51 54 94.4
branch 7 8 87.5
condition 10 12 83.3
subroutine 9 9 100.0
pod 0 1 0.0
total 77 84 91.6


line stmt bran cond sub pod time code
1             package DateTime::Format::Natural::Utils;
2              
3 26     26   253 use strict;
  26         68  
  26         892  
4 26     26   177 use warnings;
  26         86  
  26         972  
5 26     26   229 use base qw(Exporter);
  26         78  
  26         3993  
6 26     26   212 use boolean qw(true false);
  26         82  
  26         404  
7              
8             our ($VERSION, @EXPORT_OK);
9              
10             $VERSION = '0.08';
11             @EXPORT_OK = qw(trim);
12              
13             sub _valid_date
14             {
15 3383     3383   6499 my $self = shift;
16 3383         18398 return $self->_valid(@_,
17             { units => [ qw(year month day) ],
18             error => '(date is not valid)',
19             type => 'date',
20             },
21             );
22             }
23              
24             sub _valid_time
25             {
26 9043     9043   17382 my $self = shift;
27 9043         49350 return $self->_valid(@_,
28             { units => [ qw(hour minute second) ],
29             error => '(time is not valid)',
30             type => 'time',
31             },
32             );
33             }
34              
35             sub _valid
36             {
37 12426     12426   22707 my $self = shift;
38 12426         20584 my $opts = pop;
39 12426         37022 my %values = @_;
40              
41 12426         26419 delete $values{nanosecond}; # always valid
42              
43 12426         20819 my %set = map { $_ => $self->{datetime}->$_ } @{$opts->{units}};
  37278         216080  
  12426         29221  
44              
45 12426         109106 while (my ($unit, $value) = each %values) {
46 24458         81295 $set{$unit} = $value;
47             }
48              
49 12426         33283 my $checker = '_check' . "_$opts->{type}";
50              
51 12426 50       21014 if ($self->$checker(map $set{$_}, @{$opts->{units}})) {
  12426         71867  
52 12426         38154 return true;
53             }
54             else {
55 0         0 $self->_set_failure;
56 0         0 $self->_set_error($opts->{error});
57 0         0 return false;
58             }
59             }
60              
61             sub _trace_string
62             {
63 11699     11699   23311 my $self = shift;
64              
65 11699         58553 my ($trace, $modified, $keyword) = map $self->{$_}, qw(trace modified keyword);
66              
67 11699   50     33462 $trace ||= [];
68 11699   100     27336 $modified ||= {};
69 11699   100     26844 $keyword ||= '';
70              
71 11699 100 100     35174 return undef unless (@$trace || %$modified || length $keyword);
      66        
72              
73 11339         17785 my $i;
74 11339         17709 my %order = map { $_ => $i++ } @{$self->{data}->__units('ordered')};
  90712         193467  
  11339         68404  
75              
76             return join "\n", grep length, $keyword, @$trace,
77 33492         55637 map { my $unit = $_; "$unit: $modified->{$unit}" }
  33492         137767  
78 11339         63858 sort { $order{$a} <=> $order{$b} }
  35115         71911  
79             keys %$modified;
80             }
81              
82             sub trim
83             {
84 13381 100   13381 0 41101 local $_ = ref $_[0] eq 'SCALAR' ? ${$_[0]} : $_[0];
  13170         30289  
85              
86 13381         45474 s/^\s+//;
87 13381         42877 s/\s+$//;
88              
89 13381 100       33674 return ref $_[0] eq 'SCALAR' ? do { ${$_[0]} = $_; '' } : $_;
  13170         22770  
  13170         32232  
  13170         37268  
90             }
91              
92             1;
93             __END__
94              
95             =head1 NAME
96              
97             DateTime::Format::Natural::Utils - Handy utility functions/methods
98              
99             =head1 SYNOPSIS
100              
101             Please see the DateTime::Format::Natural documentation.
102              
103             =head1 DESCRIPTION
104              
105             The C<DateTime::Format::Natural::Utils> class consists of utility functions/methods.
106              
107             =head1 SEE ALSO
108              
109             L<DateTime::Format::Natural>
110              
111             =head1 AUTHOR
112              
113             Steven Schubiger <schubiger@cpan.org>
114              
115             =head1 LICENSE
116              
117             This program is free software; you may redistribute it and/or
118             modify it under the same terms as Perl itself.
119              
120             See L<http://dev.perl.org/licenses/>
121              
122             =cut