lib/DateTimeX/Web.pm

filter: Statementstmt 98.9% 186 / 188 Branchbran 77.7% 56 / 72 Conditioncond 66.6% 18 / 27 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 100% 38 / 38 Podpod 100% 18 / 18 totaltotal 90.2% 322 / 357 CC 49 SCAR 12.9
CC
49
Cov
91%
_datetime
6.1
from_object
6.1
format
6.0
CRAP 3.6
↑ Summary
1 package DateTimeX::Web;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 our $VERSION = '0.09';
8  
9 11 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use DateTime::Locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use DateTime::TimeZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use DateTime::Format::Mail;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use DateTime::Format::W3CDTF;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use DateTime::Format::MySQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use DateTime::Format::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 11 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 89 ▶sub _parse_options {
Subroutine _parse_options: called CC=5 SCAR=16.1
20 89 my $self = shift;
21  
22 89 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 7 82 89
23 7 ▶ return %{$_[0]} if ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 3 4 7
24 4 ▶ return @{$_[0]} if ref $_[0] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "ARRAY" 2 2 4
25 }
26 84 ▶ croak "Odd number of elements in hash assignment" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 2 82 84
27 82 return @_;
28 }
29  
30 34 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
31 34 my $class = shift;
32  
33 34 my %config = $class->_parse_options(@_);
34  
35 34 ▶ $config{on_error} ||= 'croak';
Condition100%2/2
$config{"on_error"} ||= "croak"
l!l
1222
MC/DC100%1/1
$config{"on_error"} ||= "croak"
$config{"on_error"}
Truth table100%2/2
A
A= $config{"on_error"}
Aresult
11
00
36  
37 34 my $self = bless {
38 config => \%config,
39 format => {
40 mail => DateTime::Format::Mail->new( loose => 1 ),
41 wwwc => DateTime::Format::W3CDTF->new,
42 mysql => DateTime::Format::MySQL->new,
43 http => 'DateTime::Format::HTTP', # ::HTTP has no 'new'
44 },
45 parser => {},
46 }, $class;
47  
48 34 ▶ $self->time_zone( $config{time_zone} || delete $config{timezone} || 'UTC' );
Condition100%5/5
$config{'time_zone'} || delete $config{'timezone'} || 'UTC'
l!l
826
$config{"time_zone"} or delete $config{"timezone"}
l!l&&r!l&&!r
7126
MC/DC100%2/2
$config{'time_zone'} || delete $config{'timezone'} || 'UTC'
$config{"time_zone"} delete $config{"timezone"}
Truth table100%3/3
A or B
A= $config{"time_zone"}B= delete $config{"timezone"}
ABresult
1X1
011
000
49 34 ▶ $self->locale( $config{locale} || 'en-US' );
Condition100%2/2
$config{'locale'} || 'en-US'
l!l
331
MC/DC100%1/1
$config{'locale'} || 'en-US'
$config{'locale'}
Truth table100%2/2
A
A= $config{'locale'}
Aresult
11
00
50  
51 34 $self;
52 }
53  
54 43 ▶sub format {
Subroutine format: called CC=6 SCAR=17.9
Pod documented
55 43 my ($self, $name, $package) = @_;
56  
57 43 ▶ if ( $package ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package) 5 38 43
58 5 ▶ if ( ref $package ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $package) { } 1 4 5
59 1 $self->{format}->{lc $name} = $package;
60 }
61 else {
62 4 ▶ unless ( $package =~ s/^\+// ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($package =~ s/^\+//) 3 1 4
63 3 $package =~ s/^DateTime::Format:://;
64 3 $package = "DateTime::Format\::$package";
65 }
66 4 eval "require $package;";
67 4 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
68 4 ▶ $self->{format}->{lc $name} =
Branch: 1 branch
BranchTrueFalseTotal
$package->can('new') ? : 3 1 4
69 ( $package->can('new') ) ? $package->new : $package;
70 }
71 }
72 43 $self->{format}->{lc $name};
73 }
74  
75 52 ▶sub time_zone {
Subroutine time_zone: called CC=4 SCAR=13.9
Pod documented
76 52 my ($self, $zone) = @_;
77  
78 52 ▶ if ( $zone ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zone) 40 12 52
79 $self->{config}->{time_zone} =
80 40 ▶ ( blessed $zone && $zone->isa('DateTime::TimeZone') )
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($zone) && $zone->isa('DateTime::TimeZone') ? : 2 38 40
Condition66%2/3
builtin::blessed($zone) and $zone->isa("DateTime::TimeZone")
!ll&&!rl&&r
3802
MC/DC50%1/2
builtin::blessed($zone) and $zone->isa("DateTime::TimeZone")
builtin::blessed($zone) $zone->isa("DateTime::TimeZone")
Truth table66%2/3
A and B
A= builtin::blessed($zone)B= $zone->isa("DateTime::TimeZone")
ABresult
0X0
100
111
81 ? $zone
82 : DateTime::TimeZone->new( name => $zone );
83 }
84 49 $self->{config}->{time_zone};
85 }
86  
87 45 ▶sub locale {
Subroutine locale: called CC=5 SCAR=16.6
Pod documented
88 45 my ($self, $locale) = @_;
89  
90 45 ▶ if ( $locale ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($locale) 39 6 45
91 $self->{config}->{locale} =
92 39 ▶ ( blessed $locale && ($locale->isa('DateTime::Locale::root') || $locale->isa('DateTime::Locale::FromData') ) )
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($locale) && ($locale->isa('DateTime::Locale::root') || $locale->isa('DateTime::Locale::FromData')) ? : 2 37 39
Condition50%3/6
builtin::blessed($locale) and $locale->isa("DateTime::Locale::root") || $locale->isa("DateTime::Locale::FromData")
!ll&&!rl&&r
3702
$locale->isa("DateTime::Locale::root") or $locale->isa("DateTime::Locale::FromData")
l!l&&r!l&&!r
020
MC/DC33%1/3
builtin::blessed($locale) and $locale->isa("DateTime::Locale::root") || $locale->isa("DateTime::Locale::FromData")
builtin::blessed($locale) $locale->isa("DateTime::Locale::root") $locale->isa("DateTime::Locale::FromData")
Truth table50%2/4
A and B or C
A= builtin::blessed($locale)B= $locale->isa("DateTime::Locale::root")C= $locale->isa("DateTime::Locale::FromData")
ABCresult
0XX0
1000
11X1
1011
93 ? $locale
94 : DateTime::Locale->load( $locale );
95 }
96 42 $self->{config}->{locale};
97 }
98  
99 {
100 11 my @constructors = qw(now today last_day_of_month from_day_of_year);
101 11 for my $method (@constructors) {
102 13 ▶ my $code = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
103 13 my $self = shift;
104  
105 13 my %options = $self->_parse_options(@_);
106  
107 13 $self->_merge_config( \%options );
108  
109 13 my $dt = eval { DateTime->$method( %options ) };
110 13 ▶ $self->_error( $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 13 13
111 13 return $dt;
112 44 };
113  
114 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
115 44 *{$method} = $code;
116 }
117 }
118  
119 18 ▶sub from {
Subroutine from: called CC=4 SCAR=13.9
Pod documented
120 18 my $self = shift;
121  
122 18 my %options = $self->_parse_options(@_);
123  
124 16 ▶ return $self->from_epoch( %options ) if $options{epoch};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"epoch"} 5 11 16
125 11 ▶ return $self->from_object( %options ) if $options{object};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"object"} 1 10 11
126  
127 10 $self->_merge_config( \%options );
128  
129 10 my $dt = eval { DateTime->new( %options ) };
130 10 ▶ $self->_error( $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 3 7 10
131 9 return $dt;
132 }
133  
134 7 ▶sub from_epoch {
Subroutine from_epoch: called CC=3 SCAR=11.0
Pod documented
135 7 my $self = shift;
136 7 my $epoch = shift;
137 7 ▶ $epoch = shift if $epoch eq 'epoch';
Branch: 1 branch
BranchTrueFalseTotal
if $epoch eq "epoch" 6 1 7
138 7 my %options = $self->_parse_options(@_);
139  
140 7 $self->_merge_config( \%options );
141  
142 7 my $dt = eval { DateTime->from_epoch( epoch => $epoch, %options ) };
143 7 ▶ $self->_error( $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
144  
145 7 return $dt;
146 }
147  
148 3 ▶sub from_object {
Subroutine from_object: called CC=6 SCAR=18.1
Pod documented
149 3 my $self = shift;
150 3 my $object = shift;
151 3 ▶ $object = shift if $object eq 'object';
Branch: 1 branch
BranchTrueFalseTotal
if $object eq "object" 2 1 3
152 3 my %options = $self->_parse_options(@_);
153  
154 3 $self->_merge_config( \%options );
155  
156 3 my $orig_time_zone;
157 3 ▶ if (my $time_zone = delete $options{time_zone}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $time_zone = delete $options{"time_zone"}) 3 0 3
158 3 ▶ if ($object->can('set_time_zone')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->can("set_time_zone")) 3 0 3
159 3 $orig_time_zone = $object->time_zone;
160 3 $object->set_time_zone($time_zone);
161 }
162 }
163  
164 3 my $dt = eval { DateTime->from_object( object => $object, %options ) };
165 3 ▶ $self->_error( $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
166  
167 3 ▶ if ($orig_time_zone) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orig_time_zone) 3 0 3
168 3 $object->set_time_zone($orig_time_zone);
169 }
170  
171 3 return $dt;
172 }
173  
174 3 ▶sub from_rss { shift->parse_as( wwwc => @_ ); }
Subroutine from_rss: called CC=1 SCAR=0.0
Pod documented
175 6 ▶sub from_mail { shift->parse_as( mail => @_ ); }
Subroutine from_mail: called CC=1 SCAR=0.0
Pod documented
176 2 ▶sub from_mysql { shift->parse_as( mysql => @_ ); }
Subroutine from_mysql: called CC=1 SCAR=0.0
Pod documented
177 2 ▶sub from_http { shift->parse_as( http => @_ ); }
Subroutine from_http: called CC=1 SCAR=0.0
Pod documented
178  
179 11 *from_wwwc = \&from_rss;
180 11 *from_rss20 = \&from_mail;
181  
182 13 ▶sub parse_as {
Subroutine parse_as: called CC=2 SCAR=6.9
Pod documented
183 13 my ($self, $formatter, $string, @args) = @_;
184  
185 13 my %options = $self->_parse_options(@args);
186  
187 13 $self->_load( $formatter );
188  
189 13 my $dt = eval { $self->format($formatter)->parse_datetime( $string ) };
190 13 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 4 9 13
191 4 $self->_error( $@ );
192 }
193 else {
194 9 $self->_merge_config( \%options );
195 9 $self->_set_config( $dt, \%options );
196 9 return $dt;
197 }
198 }
199  
200 1 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.1
Pod documented
201 1 my ($self, $pattern, $string, @args) = @_;
202  
203 1 my %options = $self->_parse_options(@args);
204  
205 1 ▶ unless ( $self->{parser}->{$pattern} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"parser"}{$pattern}) 1 0 1
206 1 $self->_merge_config( \%options );
207 1 $options{pattern} = $pattern;
208 1 my $parser = DateTime::Format::Strptime->new( %options );
209 1 $self->{parser}->{$pattern} = $parser;
210 }
211 1 my $dt = eval { $self->{parser}->{$pattern}->parse_datetime( $string ) };
212 1 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 1 1
213 0 $self->_error( $@ );
214 }
215 else {
216 1 $self->_set_config( $dt, \%options );
217 1 return $dt;
218 }
219 }
220  
221 11 *strptime = \&parse;
222  
223 1 ▶sub for_rss { shift->render_as( wwwc => @_ ); }
Subroutine for_rss: called CC=1 SCAR=0.0
Pod documented
224 1 ▶sub for_mail { shift->render_as( mail => @_ ); }
Subroutine for_mail: called CC=1 SCAR=0.0
Pod documented
225 1 ▶sub for_mysql { shift->render_as( mysql => @_ ); }
Subroutine for_mysql: called CC=1 SCAR=0.0
Pod documented
226 1 ▶sub for_http { shift->render_as( http => @_ ); }
Subroutine for_http: called CC=1 SCAR=0.0
Pod documented
227  
228 11 *for_wwwc = \&for_rss;
229 11 *for_rss20 = \&for_mail;
230  
231 4 ▶sub render_as {
Subroutine render_as: called CC=2 SCAR=7.0
Pod documented
232 4 my ($self, $formatter, @args) = @_;
233  
234 4 $self->_load( $formatter );
235  
236 4 my $dt = $self->_datetime( @args );
237  
238 4 my $str = eval { $self->format($formatter)->format_datetime( $dt ) };
239 4 ▶ $self->_error( $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
240 4 return $str;
241 }
242  
243 43 ▶sub _merge_config {
Subroutine _merge_config: called CC=4 SCAR=14.3
244 43 my ($self, $options) = @_;
245  
246 43 foreach my $key (qw( time_zone locale )) {
247 86 ▶ next unless defined $self->{config}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{$key} 0 86 86
248 86 ▶ next if defined $options->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $options->{$key} 0 86 86
249 86 $options->{$key} = $self->{config}->{$key};
250 }
251 }
252  
253 7 ▶sub _datetime {
Subroutine _datetime: called CC=5 SCAR=18.1
254 7 my $self = shift;
255  
256 7 ▶ return $self->now unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 7 7
257 7 ▶ return $_[0] if @_ == 1 && blessed $_[0] && $_[0]->isa('DateTime');
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and builtin::blessed($_[0]) and $_[0]->isa("DateTime") 0 7 7
Condition50%3/6
@_ == 1 and builtin::blessed($_[0]) and $_[0]->isa("DateTime")
!ll&&!rl&&r
700
@_ == 1 and builtin::blessed($_[0])
!ll&&!rl&&r
430
MC/DC0%0/3
@_ == 1 and builtin::blessed($_[0]) and $_[0]->isa("DateTime")
@_ == 1 builtin::blessed($_[0]) $_[0]->isa("DateTime")
Truth table50%2/4
A and B and C
A= @_ == 1B= builtin::blessed($_[0])C= $_[0]->isa("DateTime")
ABCresult
0XX0
10X0
1100
1111
258 7 return $self->from( @_ );
259 }
260  
261 17 ▶sub _load {
Subroutine _load: called CC=2 SCAR=8.1
262 17 my ($self, $formatter) = @_;
263  
264 17 ▶ unless ( $self->format($formatter) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->format($formatter)) 0 17 17
265 0 $self->format( $formatter => "DateTime::Format\::$formatter" );
266 }
267 }
268  
269 10 ▶sub _set_config {
Subroutine _set_config: called CC=4 SCAR=14.9
270 10 my ($self, $dt, $options) = @_;
271  
272 10 ▶ $options ||= $self->{config};
Condition33%1/3
$options ||= $self->{"config"}
l!l&&r!l&&!r
1000
MC/DC0%0/2
$options ||= $self->{"config"}
$options $self->{"config"}
Truth table33%1/3
A ||= B
A= $optionsB= $self->{"config"}
ABresult
1X1
011
000
273  
274 10 foreach my $key (qw( time_zone locale )) {
275 20 my $func = "set_$key";
276 20 ▶ $dt->$func( $options->{$key} ) if $options->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{$key} 20 0 20
277 }
278 }
279  
280 7 ▶sub _error {
Subroutine _error: called CC=3 SCAR=11.0
281 7 my ($self, $message) = @_;
282  
283 7 my $on_error = $self->{config}->{on_error};
284  
285 7 ▶ return if $on_error eq 'ignore';
Branch: 1 branch
BranchTrueFalseTotal
if $on_error eq "ignore" 5 2 7
286 2 ▶ return $on_error->( $message ) if ref $on_error eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $on_error eq "CODE" 1 1 2
287  
288 1 local $Carp::CarpLevel = 1;
289 1 croak $message;
290 }
291  
292 11 1;
293  
294 __END__
↑ Summary