File Coverage

blib/lib/DateTime/Format/Builder/Parser/generic.pm
Criterion Covered Total %
statement 42 42 100.0
branch 23 30 76.6
condition 2 3 66.6
subroutine 8 8 100.0
pod 2 2 100.0
total 77 85 90.5


line stmt bran cond sub pod time code
1             package DateTime::Format::Builder::Parser::generic;
2             {
3             $DateTime::Format::Builder::Parser::generic::VERSION = '0.81';
4             }
5 24     24   119 use strict;
  24         41  
  24         702  
6 24     24   138 use warnings;
  24         43  
  24         495  
7 24     24   115 use Carp;
  24         53  
  24         1725  
8 24         11795 use Params::Validate qw(
9             validate SCALAR CODEREF UNDEF
10 24     24   141 );
  24         49  
11              
12              
13              
14             sub new {
15 63     63 1 113 my $class = shift;
16 63         380 bless {@_}, $class;
17             }
18              
19              
20             sub generic_parser {
21 63     63 1 124 my $class = shift;
22 252         1956 my %args = validate(
23             @_,
24             {
25             (
26 63         155 map { $_ => { type => CODEREF, optional => 1 } }
27             qw(
28             on_match on_fail preprocess postprocess
29             )
30             ),
31             label => { type => SCALAR | UNDEF, optional => 1 },
32             }
33             );
34 63         426 my $label = $args{label};
35              
36 63 100 66     337 my $callback
37             = ( exists $args{on_match} or exists $args{on_fail} ) ? 1 : undef;
38              
39             return sub {
40 84     84   209 my ( $self, $date, $p, @args ) = @_;
41 84 50       191 return unless defined $date;
42 84         122 my %p;
43 84 50       434 %p = %$p if $p; # Look! A Copy!
44              
45 84 50       356 my %param = (
    100          
46             self => $self,
47             ( defined $label ? ( label => $label ) : () ),
48             ( @args ? ( args => \@args ) : () ),
49             );
50              
51             # Preprocess - can modify $date and fill %p
52 84 100       249 if ( $args{preprocess} ) {
53 8         37 $date = $args{preprocess}
54             ->( input => $date, parsed => \%p, %param );
55             }
56              
57 84 50       1317 my $rv = $class->do_match( $date, @args ) if $class->can('do_match');
58              
59             # Funky callback thing
60 84 100       230 if ($callback) {
61 6 100       13 my $type = defined $rv ? "on_match" : "on_fail";
62 6 50       33 $args{$type}->( input => $date, %param ) if $args{$type};
63             }
64 84 100       2822 return unless defined $rv;
65              
66 44         72 my $dt;
67 44 50       321 $dt = $class->post_match( $date, $rv, \%p )
68             if $class->can('post_match');
69              
70             # Allow post processing. Return undef if regarded as failure
71 44 100       248 if ( $args{postprocess} ) {
72 10         49 my $rv = $args{postprocess}->(
73             parsed => \%p,
74             input => $date,
75             post => $dt,
76             %param,
77             );
78 10 50       108 return unless $rv;
79             }
80              
81             # A successful match!
82 44 100       298 $dt = $class->make( $date, $dt, \%p ) if $class->can('make');
83 44         580201 return $dt;
84 63         777 };
85             }
86              
87              
88             {
89 24     24   135 no strict 'refs';
  24         51  
  24         1996  
90             for (qw( valid_params params )) {
91             *$_ = *{"DateTime::Format::Builder::Parser::$_"};
92             }
93             }
94              
95             1;
96              
97             # ABSTRACT: Useful routines
98              
99             __END__