File Coverage

blib/lib/DateTime/Format/Builder/Parser/Quick.pm
Criterion Covered Total %
statement 12 25 48.0
branch 0 8 0.0
condition n/a
subroutine 4 6 66.6
pod 1 1 100.0
total 17 40 42.5


line stmt bran cond sub pod time code
1             package DateTime::Format::Builder::Parser::Quick;
2              
3 24     24   147 use strict;
  24         43  
  24         664  
4 24     24   102 use warnings;
  24         45  
  24         1097  
5              
6             our $VERSION = '0.83';
7              
8             our %dispatch_data;
9              
10 24     24   125 use Params::Validate qw( SCALAR OBJECT CODEREF validate );
  24         44  
  24         1251  
11              
12 24     24   502 use parent qw( DateTime::Format::Builder::Parser );
  24         343  
  24         171  
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.83
63              
64             =head1 SYNOPSIS
65              
66             use DateTime::Format::Builder (
67             parsers => {
68             parse_datetime => [
69             { Quick => 'DateTime::Format::HTTP' },
70             { Quick => 'DateTime::Format::Mail' },
71             { Quick => 'DateTime::Format::IBeat' },
72             ]
73             }
74             );
75              
76             # is the same as
77              
78             use DateTime::Format::HTTP;
79             use DateTime::Format::Mail;
80             use DateTime::Format::IBeat;
81              
82             use DateTime::Format::Builder (
83             parsers => {
84             parse_datetime => [
85             sub {
86             eval { DateTime::Format::HTTP->parse_datetime( $_[1] ) }
87             },
88             sub {
89             eval { DateTime::Format::Mail->parse_datetime( $_[1] ) }
90             },
91             sub {
92             eval { DateTime::Format::IBeat->parse_datetime( $_[1] ) }
93             },
94             ]
95             }
96             );
97              
98             (These two pieces of code can both be found in the test suite; one as
99             F<quick.t>, the other as F<fall.t>.)
100              
101             =head1 DESCRIPTION
102              
103             C<Quick> adds a parser that allows some shortcuts when writing fairly standard
104             and mundane calls to other formatting modules.
105              
106             =head1 SPECIFICATION
107              
108             C<Quick> has two keys, one optional.
109              
110             The C<Quick> keyword should have an argument of either an object or a class
111             name. If it's a class name then the class is C<use>d.
112              
113             The C<method> keyword is optional with a default of C<parse_datetime>. It's
114             either name of the method to invoke on the object, or a reference to a piece
115             of code.
116              
117             In any case, the resultant code ends up looking like:
118              
119             my $rv = $Quick->$method($date);
120              
121             =head1 SEE ALSO
122              
123             C<datetime@perl.org> mailing list.
124              
125             http://datetime.perl.org/
126              
127             L<perl>, L<DateTime>,
128             L<DateTime::Format::Builder>
129              
130             =head1 SUPPORT
131              
132             Bugs may be submitted at L<https://github.com/houseabsolute/DateTime-Format-Builder/issues>.
133              
134             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
135              
136             =head1 SOURCE
137              
138             The source code repository for DateTime-Format-Builder can be found at L<https://github.com/houseabsolute/DateTime-Format-Builder>.
139              
140             =head1 AUTHORS
141              
142             =over 4
143              
144             =item *
145              
146             Dave Rolsky <autarch@urth.org>
147              
148             =item *
149              
150             Iain Truskett <spoon@cpan.org>
151              
152             =back
153              
154             =head1 COPYRIGHT AND LICENSE
155              
156             This software is Copyright (c) 2020 by Dave Rolsky.
157              
158             This is free software, licensed under:
159              
160             The Artistic License 2.0 (GPL Compatible)
161              
162             The full text of the license can be found in the
163             F<LICENSE> file included with this distribution.
164              
165             =cut