Builder/Parser/Dispatch.pm

filter: Statementstmt 97.5% 39 / 40 Branchbran 60.0% 6 / 10 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 82.6% 57 / 69 SCAR 22.2
CC
9
Cov
86%
__ANON__
10.7
create_parser
1.0
BEGIN
1.0
CRAP 9.2
« Builder/Parser/Quick.pm ↑ Summary Builder/Parser/Regex.pm »
1 package DateTime::Format::Builder::Parser::Dispatch;
2  
3 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 24 our $VERSION = '0.82';
7  
8 24 ▶use vars qw( %dispatch_data );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use Params::Validate qw( CODEREF validate );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use DateTime::Format::Builder::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 {
13 24 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 24 *dispatch_data = *DateTime::Format::Builder::dispatch_data;
15 24 *params = *DateTime::Format::Builder::Parser::params;
16 }
17  
18 DateTime::Format::Builder::Parser->valid_params(
19 24 Dispatch => {
20 type => CODEREF,
21 }
22 );
23  
24 sub create_parser {
25 2 ▶ my ( $self, %args ) = @_;
Subroutine create_parser: called CC=1 SCAR=0.2
Pod undocumented
26 2 my $coderef = $args{Dispatch};
27  
28 return sub {
29 3 ▶ my ( $self, $date, $p, @args ) = @_;
Subroutine __ANON__: called CC=9 SCAR=23.7
30 3 ▶ return unless defined $date;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $date 0 3 3
31 3 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
300
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
32  
33 3 my @results = $coderef->($date);
34 3 ▶ return unless @results;
Branch: 1 branch
BranchTrueFalseTotal
unless @results 0 3 3
35 3 ▶ return unless defined $results[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $results[0] 0 3 3
36  
37 3 for my $group (@results) {
38 4 my $parser = $dispatch_data{$class}{$group};
39 4 ▶ die "Unknown parsing group: $class\n" unless defined $parser;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parser 0 4 4
40 4 my $rv = eval { $parser->parse( $self, $date, $p, @args ) };
41 4 ▶ return $rv unless $@ or not defined $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ or not defined $rv 3 1 4
Condition66%2/3
$@ or not defined $rv
l!l&&r!l&&!r
103
MC/DC50%1/2
$@ or not defined $rv
$@ not defined $rv
Truth table66%2/3
A or not(B)
A= $@B= not defined $rv
ABresult
1X1
011
000
42 }
43 0 return;
44 2 };
45 }
46  
47 24 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
« Builder/Parser/Quick.pm ↑ Summary Builder/Parser/Regex.pm »