File Coverage

blib/lib/DateTimeX/TO_JSON.pm
Criterion Covered Total %
statement 25 25 100.0
branch 5 6 83.3
condition 3 3 100.0
subroutine 8 8 100.0
pod n/a
total 41 42 97.6


line stmt bran cond sub pod time code
1             # ABSTRACT: Adds a TO_JSON method to DateTime
2              
3             package DateTimeX::TO_JSON;
4             $DateTimeX::TO_JSON::VERSION = '0.0.2';
5 3     3   2765 use strict;
  3         6  
  3         94  
6 3     3   13 use warnings;
  3         45  
  3         86  
7 3     3   54 use Class::Load;
  3         12  
  3         128  
8 3     3   17 use Carp;
  3         4  
  3         862  
9              
10             sub import {
11 3     3   27 my ($class, @args) = @_;
12              
13             ## Only deal with formatter just now but might deal with more later
14             ## such as importing DateTime itself
15 3         8 my %args;
16 3         13 while ($_ = shift @args) {
17 2 50       8 if ( $_ eq 'formatter' ) {
18 2         9 $args{$_} = shift @args;
19             }
20             }
21              
22 3 100 100     33 if ( $args{formatter} && ref($args{formatter}) ) {
    100          
23             *DateTime::TO_JSON = sub {
24 1     1   796 $args{formatter}->format_datetime($_[0]);
25             }
26 1         17 }
27             elsif ( $args{formatter} ) {
28 1         5 Class::Load::load_class $args{formatter};
29             *DateTime::TO_JSON = sub {
30 1     1   943 $args{formatter}->new->format_datetime($_[0]);
31             }
32 1         120 }
33             else {
34             *DateTime::TO_JSON = sub {
35 1     1   895 $_[0]->datetime;
36             }
37 1         24 }
38             }
39              
40             1;
41              
42             __END__