File Coverage

blib/lib/DateTime/Format/Builder/Parser/Quick.pm
Criterion Covered Total %
statement 15 28 53.5
branch 0 8 0.0
condition n/a
subroutine 5 7 71.4
pod 1 1 100.0
total 21 44 47.7


line stmt bran cond sub pod time code
1             package DateTime::Format::Builder::Parser::Quick;
2              
3 24     24   152 use strict;
  24         48  
  24         695  
4 24     24   118 use warnings;
  24         45  
  24         974  
5              
6             our $VERSION = '0.82';
7              
8 24     24   128 use vars qw( %dispatch_data );
  24         47  
  24         1029  
9              
10 24     24   138 use Params::Validate qw( SCALAR OBJECT CODEREF validate );
  24         46  
  24         1510  
11              
12 24     24   611 use parent qw( DateTime::Format::Builder::Parser );
  24         308  
  24         189  
13              
14             __PACKAGE__->valid_params(
15             Quick => {
16             type => SCALAR | OBJECT,
17             callbacks => {
18             good_classname => sub {
19             ( ref $_[0] ) or ( $_[0] =~ /^\w+[:'\w+]*\w+/ );
20             },
21             }
22             },
23             method => {
24             optional => 1,
25             type => SCALAR | CODEREF,
26             },
27             );
28              
29             sub create_parser {
30 0     0 1   my ( $self, %args ) = @_;
31 0           my $class = $args{Quick};
32 0           my $method = $args{method};
33 0 0         $method = 'parse_datetime' unless defined $method;
34 0           eval "use $class";
35 0 0         die $@ if $@;
36              
37             return sub {
38 0     0     my ( $self, $date ) = @_;
39 0 0         return unless defined $date;
40 0           my $rv = eval { $class->$method($date) };
  0            
41 0 0         return $rv if defined $rv;
42 0           return;
43 0           };
44             }
45              
46             1;
47              
48             # ABSTRACT: Use another formatter, simply
49              
50             __END__
51              
52             =pod
53              
54             =encoding UTF-8
55              
56             =head1 NAME
57              
58             DateTime::Format::Builder::Parser::Quick - Use another formatter, simply
59              
60             =head1 VERSION
61              
62             version 0.82
63              
64             =head1 SYNOPSIS
65              
66             use DateTime::Format::Builder (
67             parsers => { parse_datetime => [
68             { Quick => 'DateTime::Format::HTTP' },
69             { Quick => 'DateTime::Format::Mail' },
70             { Quick => 'DateTime::Format::IBeat' },
71             ]});
72              
73             is the same as:
74              
75             use DateTime::Format::HTTP;
76             use DateTime::Format::Mail;
77             use DateTime::Format::IBeat;
78              
79             use DateTime::Format::Builder (
80             parsers => { parse_datetime => [
81             sub { eval { DateTime::Format::HTTP->parse_datetime( $_[1] ) } },
82             sub { eval { DateTime::Format::Mail->parse_datetime( $_[1] ) } },
83             sub { eval { DateTime::Format::IBeat->parse_datetime( $_[1] ) } },
84             ]});
85              
86             (These two pieces of code can both be found in the test
87             suite; one as F<quick.t>, the other as F<fall.t>.)
88              
89             =head1 DESCRIPTION
90              
91             C<Quick> adds a parser that allows some shortcuts when
92             writing fairly standard and mundane calls to other
93             formatting modules.
94              
95             =head1 SPECIFICATION
96              
97             C<Quick> has two keys, one optional.
98              
99             The C<Quick> keyword should have an argument of either an
100             object or a class name. If it's a class name then the class
101             is C<use>d.
102              
103             The C<method> keyword is optional with a default of
104             C<parse_datetime>. It's either name of the method to invoke
105             on the object, or a reference to a piece of code.
106              
107             In any case, the resultant code ends up looking like:
108              
109             my $rv = $Quick->$method( $date );
110              
111             =head1 SEE ALSO
112              
113             C<datetime@perl.org> mailing list.
114              
115             http://datetime.perl.org/
116              
117             L<perl>, L<DateTime>,
118             L<DateTime::Format::Builder>
119              
120             =head1 SUPPORT
121              
122             Bugs may be submitted at L<http://rt.cpan.org/Public/Dist/Display.html?Name=DateTime-Format-Builder> or via email to L<bug-datetime-format-builder@rt.cpan.org|mailto:bug-datetime-format-builder@rt.cpan.org>.
123              
124             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
125              
126             =head1 SOURCE
127              
128             The source code repository for DateTime-Format-Builder can be found at L<https://github.com/houseabsolute/DateTime-Format-Builder>.
129              
130             =head1 AUTHORS
131              
132             =over 4
133              
134             =item *
135              
136             Dave Rolsky <autarch@urth.org>
137              
138             =item *
139              
140             Iain Truskett
141              
142             =back
143              
144             =head1 COPYRIGHT AND LICENSE
145              
146             This software is Copyright (c) 2019 by Dave Rolsky.
147              
148             This is free software, licensed under:
149              
150             The Artistic License 2.0 (GPL Compatible)
151              
152             The full text of the license can be found in the
153             F<LICENSE> file included with this distribution.
154              
155             =cut