File Coverage

blib/lib/DateTime/Format/Builder/Parser/Dispatch.pm
Criterion Covered Total %
statement 30 31 96.7
branch 6 10 60.0
condition 3 6 50.0
subroutine 7 7 100.0
pod 0 1 0.0
total 46 55 83.6


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