File Coverage

blib/lib/DateTime/Format/Builder/Parser/Dispatch.pm
Criterion Covered Total %
statement 33 34 97.0
branch 6 10 60.0
condition 3 6 50.0
subroutine 8 8 100.0
pod 0 1 0.0
total 50 59 84.7


line stmt bran cond sub pod time code
1             package DateTime::Format::Builder::Parser::Dispatch;
2              
3 24     24   200 use strict;
  24         55  
  24         734  
4 24     24   124 use warnings;
  24         50  
  24         1322  
5              
6             our $VERSION = '0.82';
7              
8 24     24   142 use vars qw( %dispatch_data );
  24         50  
  24         1144  
9 24     24   432 use Params::Validate qw( CODEREF validate );
  24         66  
  24         1309  
10 24     24   151 use DateTime::Format::Builder::Parser;
  24         56  
  24         829  
11              
12             {
13 24     24   371 no strict 'refs';
  24         68  
  24         6673  
14             *dispatch_data = *DateTime::Format::Builder::dispatch_data;
15             *params = *DateTime::Format::Builder::Parser::params;
16             }
17              
18             DateTime::Format::Builder::Parser->valid_params(
19             Dispatch => {
20             type => CODEREF,
21             }
22             );
23              
24             sub create_parser {
25 2     2 0 6 my ( $self, %args ) = @_;
26 2         4 my $coderef = $args{Dispatch};
27              
28             return sub {
29 3     3   8 my ( $self, $date, $p, @args ) = @_;
30 3 50       8 return unless defined $date;
31 3   33     9 my $class = ref($self) || $self;
32              
33 3         52 my @results = $coderef->($date);
34 3 50       19 return unless @results;
35 3 50       8 return unless defined $results[0];
36              
37 3         6 for my $group (@results) {
38 4         41 my $parser = $dispatch_data{$class}{$group};
39 4 50       14 die "Unknown parsing group: $class\n" unless defined $parser;
40 4         6 my $rv = eval { $parser->parse( $self, $date, $p, @args ) };
  4         17  
41 4 100 66     79 return $rv unless $@ or not defined $rv;
42             }
43 0           return;
44 2         16 };
45             }
46              
47             1;
48              
49             # ABSTRACT: Dispatch parsers by group
50              
51             __END__
52              
53             =pod
54              
55             =encoding UTF-8
56              
57             =head1 NAME
58              
59             DateTime::Format::Builder::Parser::Dispatch - Dispatch parsers by group
60              
61             =head1 VERSION
62              
63             version 0.82
64              
65             =head1 SYNOPSIS
66              
67             package SampleDispatch;
68             use DateTime::Format::Builder
69             (
70             parsers => {
71             parse_datetime => [
72             {
73             Dispatch => sub {
74             return 'fnerk';
75             }
76             }
77             ]
78             },
79             groups => {
80             fnerk => [
81             {
82             regex => qr/^(\d{4})(\d\d)(\d\d)$/,
83             params => [qw( year month day )],
84             },
85             ]
86             }
87             );
88              
89             =head1 DESCRIPTION
90              
91             C<Dispatch> adds another parser type to C<Builder> permitting
92             dispatch of parsing according to group names.
93              
94             =head1 SPECIFICATION
95              
96             C<Dispatch> has just one key: C<Dispatch>. The value should be a
97             reference to a subroutine that returns one of:
98              
99             =over 4
100              
101             =item *
102              
103             C<undef>, meaning no groups could be found.
104              
105             =item *
106              
107             An empty list, meaning no groups could be found.
108              
109             =item *
110              
111             A single string, meaning: use this group
112              
113             =item *
114              
115             A list of strings, meaning: use these groups in this order.
116              
117             =back
118              
119             Groups are specified much like the example in the L<SYNOPSIS>.
120             They follow the same format as when you specify them for methods.
121              
122             =head1 SIDE EFFECTS
123              
124             Your group parser can also be a Dispatch parser. Thus you could
125             potentially end up with an infinitely recursive parser.
126              
127             =head1 SEE ALSO
128              
129             C<datetime@perl.org> mailing list.
130              
131             http://datetime.perl.org/
132              
133             L<perl>, L<DateTime>,
134             L<DateTime::Format::Builder>
135              
136             =head1 SUPPORT
137              
138             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>.
139              
140             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
141              
142             =head1 SOURCE
143              
144             The source code repository for DateTime-Format-Builder can be found at L<https://github.com/houseabsolute/DateTime-Format-Builder>.
145              
146             =head1 AUTHORS
147              
148             =over 4
149              
150             =item *
151              
152             Dave Rolsky <autarch@urth.org>
153              
154             =item *
155              
156             Iain Truskett
157              
158             =back
159              
160             =head1 COPYRIGHT AND LICENSE
161              
162             This software is Copyright (c) 2019 by Dave Rolsky.
163              
164             This is free software, licensed under:
165              
166             The Artistic License 2.0 (GPL Compatible)
167              
168             The full text of the license can be found in the
169             F<LICENSE> file included with this distribution.
170              
171             =cut