Builder.pm

filter: Statementstmt 97.3% 111 / 114 Branchbran 84.7% 39 / 46 Conditioncond 66.6% 12 / 18 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 92.0% 23 / 25 Podpod 78.5% 11 / 14 totaltotal 87.3% 200 / 229 CC 31 SCAR 12.9
CC
31
Cov
91%
create_constructor
7.0
create_class
6.0
parse_datetime
4.8
CRAP 3.6
« Builder/Parser.pm ↑ Summary Builder/Parser/Regex.pm »
1 package DateTime::Format::Builder;
2  
3 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 23 our $VERSION = '0.83';
7  
8 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use DateTime 1.00;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 ▶use Params::Validate 0.72 qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 validate SCALAR ARRAYREF HASHREF SCALARREF CODEREF GLOB GLOBREF UNDEF
12 23 );
13 23 our %dispatch_data;
14  
15 23 my $parser = 'DateTime::Format::Builder::Parser';
16  
17 0 ▶sub verbose {
Subroutine verbose: not called CC=1 SCAR=6.9
Pod documented
18 0 warn "Use of verbose() deprecated for the interim.";
19 0 1;
20 }
21  
22 36 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
23 36 my $class = shift;
24 36 ▶ $class->create_class( @_, class => (caller)[0] ) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 13 23 36
25 }
26  
27 21 ▶sub create_class {
Subroutine create_class: called CC=6 SCAR=17.9
Pod documented
28 21 my $class = shift;
29 21 my %args = validate(
30 @_,
31 {
32 class => { type => SCALAR, default => (caller)[0] },
33 version => { type => SCALAR, optional => 1 },
34 verbose => { type => SCALAR | GLOBREF | GLOB, optional => 1 },
35 parsers => { type => HASHREF },
36 groups => { type => HASHREF, optional => 1 },
37 constructor =>
38 { type => UNDEF | SCALAR | CODEREF, optional => 1 },
39 }
40 );
41  
42 21 ▶ verbose( $args{verbose} ) if exists $args{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"verbose"} 0 21 21
43  
44 21 my $target = $args{class}; # where we're writing our methods and such.
45  
46 # Create own lovely new package
47 {
48 23 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 21 ▶ ${"${target}::VERSION"} = $args{version} if exists $args{version};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"version"} 4 17 21
51  
52 $class->create_constructor(
53 $target, exists $args{constructor},
54 $args{constructor}
55 21 );
56  
57 # Turn groups of parser specs in to groups of parsers
58 {
59 19 my $specs = $args{groups};
60 19 my %groups;
61  
62 19 for my $label ( keys %$specs ) {
63 3 my $parsers = $specs->{$label};
64 3 my $code = $class->create_parser($parsers);
65 3 $groups{$label} = $code;
66 }
67  
68 19 $dispatch_data{$target} = \%groups;
69 }
70  
71 # Write all our parser methods, creating parsers as we go.
72 19 ▶ while ( my ( $method, $parsers ) = each %{ $args{parsers} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$args{"parsers"};}) 19 17 36
73 19 my $globname = $target . "::$method";
74 croak "Will not override a preexisting method $method()"
75 19 ▶ if defined &{$globname};
Branch: 1 branch
BranchTrueFalseTotal
if defined &{$globname;} 2 17 19
76 17 *$globname = $class->create_end_parser($parsers);
77 }
78 }
79  
80 }
81  
82 21 ▶sub create_constructor {
Subroutine create_constructor: called CC=7 SCAR=19.5
Pod undocumented
83 21 shift;
84 21 my ( $target, $intended, $value ) = @_;
85  
86 21 my $new = $target . "::new";
87 21 ▶ $value = 1 unless $intended;
Branch: 1 branch
BranchTrueFalseTotal
unless $intended 13 8 21
88  
89 21 ▶ return unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 3 18 21
90 18 ▶ return if not $intended and defined &$new;
Branch: 1 branch
BranchTrueFalseTotal
if not $intended and defined &$new 2 16 18
Condition100%3/3
not $intended and defined &$new
!ll&&!rl&&r
5112
MC/DC100%2/2
not $intended and defined &$new
not $intended defined &$new
Truth table100%3/3
not(A) and B
A= not $intendedB= defined &$new
ABresult
0X0
100
111
91 16 ▶ croak "Will not override a preexisting constructor new()"
Branch: 1 branch
BranchTrueFalseTotal
if defined &$new 2 14 16
92 if defined &$new;
93  
94 23 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 14 ▶ return *$new = $value if ref $value eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "CODE" 2 12 14
97 16 ▶ return *$new = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
98 16 my $class = shift;
99 16 ▶ croak "${class}->new takes no parameters." if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 15 16
100  
101 15 ▶ my $self = bless {}, ref($class) || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
1140
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
102  
103 # If called on an object, clone, but we've nothing to
104 # clone
105  
106 15 $self;
107 12 };
108 }
109  
110 42 ▶sub create_parser {
Subroutine create_parser: called CC=4 SCAR=13.9
Pod undocumented
111 42 my $class = shift;
112 42 my @common = ( maker => $class );
113 42 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 29 13 42
114 29 my $parsers = shift;
115 29 ▶ my @parsers = (
Branch: 2 branches
BranchTrueFalseTotal
ref $parsers eq 'HASH' ? : 7 22 29
ref $parsers eq 'ARRAY' ? : 22 0 22
116 ( ref $parsers eq 'HASH' )
117 ? %$parsers
118 : ( ( ref $parsers eq 'ARRAY' ) ? @$parsers : $parsers )
119 );
120 29 $parser->create_parser( \@common, @parsers );
121 }
122 else {
123 13 $parser->create_parser( \@common, @_ );
124 }
125 }
126  
127 # This creates the end methods. Coderefs die on bad parses, return C<DateTime>
128 # objects on good parse.
129 25 ▶sub create_end_parser {
Subroutine create_end_parser: called CC=1 SCAR=0.0
Pod undocumented
130 25 my ( $class, $parsers ) = @_;
131 25 $class->create_method( $class->create_parser($parsers) );
132 }
133  
134 25 ▶sub create_method {
Subroutine create_method: called CC=1 SCAR=0.0
Pod documented
135 25 shift;
136 25 my ($parser) = @_;
137  
138 31 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
139 31 my $self = shift;
140 31 $parser->parse( $self, @_ );
141 25 };
142 }
143  
144 3 ▶sub on_fail {
Subroutine on_fail: called CC=4 SCAR=13.9
Pod documented
145 3 shift;
146 3 my ($input) = @_;
147  
148 3 my $pkg;
149 3 my $i = 0;
150 3 ▶ while ( ($pkg) = caller( $i++ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($pkg) = caller $i++) 15 0 15
151 last
152 15 ▶ if ( !UNIVERSAL::isa( $pkg, 'DateTime::Format::Builder' )
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($pkg, "DateTime::Format::Builder") or UNIVERSAL::isa($pkg, "DateTime::Format::Builder::Parser") 3 12 15
Condition100%3/3
UNIVERSAL::isa($pkg, "DateTime::Format::Builder") or UNIVERSAL::isa($pkg, "DateTime::Format::Builder::Parser")
l!l&&r!l&&!r
393
MC/DC100%2/2
UNIVERSAL::isa($pkg, "DateTime::Format::Builder") or UNIVERSAL::isa($pkg, "DateTime::Format::Builder::Parser")
UNIVERSAL::isa($pkg, "DateTime::Format::Builder") UNIVERSAL::isa($pkg, "DateTime::Format::Builder::Parser")
Truth table100%3/3
A or B
A= UNIVERSAL::isa($pkg, "DateTime::Format::Builder")B= UNIVERSAL::isa($pkg, "DateTime::Format::Builder::Parser")
ABresult
1X1
011
000
153 && !UNIVERSAL::isa( $pkg, 'DateTime::Format::Builder::Parser' ) );
154 }
155 3 local $Carp::CarpLevel = $i;
156 3 croak "Invalid date format: $input";
157 }
158  
159 13 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
160 13 my $class = shift;
161 13 ▶ croak "Constructor 'new' takes no parameters" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 12 13
162 my $self = bless {
163 1 ▶ parser => sub { croak "No parser set." }
Subroutine __ANON__: called CC=1 SCAR=0.0
164 },
165 12 ▶ ref($class) || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
2100
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
166 12 ▶ if ( ref $class ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 2 10 12
167  
168 # If called on an object, clone
169 2 $self->set_parser( $class->get_parser );
170  
171 # and that's it. we don't store that much info per object
172 }
173 12 return $self;
174 }
175  
176 8 ▶sub parser {
Subroutine parser: called CC=2 SCAR=6.9
Pod documented
177 8 my $class = shift;
178 8 my $parser = $class->create_end_parser( \@_ );
179  
180 # Do we need to instantiate a new object for return,
181 # or are we modifying an existing object?
182 8 my $self;
183 8 ▶ $self = ref $class ? $class : $class->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 2 6 8
184  
185 8 $self->set_parser($parser);
186  
187 8 $self;
188 }
189  
190 1 ▶sub clone {
Subroutine clone: called CC=2 SCAR=7.1
Pod documented
191 1 my $self = shift;
192 1 ▶ croak "Calling object method as class method!" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 1 1
193 1 return $self->new;
194 }
195  
196 10 ▶sub set_parser {
Subroutine set_parser: called CC=3 SCAR=12.0
Pod documented
197 10 my ( $self, $parser ) = @_;
198 10 ▶ croak "set_parser given something other than a coderef"
Branch: 1 branch
BranchTrueFalseTotal
unless $parser and ref $parser eq "CODE" 0 10 10
Condition33%1/3
$parser and ref $parser eq "CODE"
!ll&&!rl&&r
0010
MC/DC0%0/2
$parser and ref $parser eq "CODE"
$parser ref $parser eq "CODE"
Truth table33%1/3
A and B
A= $parserB= ref $parser eq "CODE"
ABresult
0X0
100
111
199 unless $parser
200 and ref $parser eq 'CODE';
201 10 $self->{parser} = $parser;
202 10 $self;
203 }
204  
205 6 ▶sub get_parser {
Subroutine get_parser: called CC=1 SCAR=0.0
Pod documented
206 6 my ($self) = @_;
207 6 return $self->{parser};
208 }
209  
210 14 ▶sub parse_datetime {
Subroutine parse_datetime: called CC=4 SCAR=15.6
Pod documented
211 14 my $self = shift;
212 14 ▶ croak "parse_datetime is an object method, not a class method."
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self and $self->isa(__PACKAGE__) 0 14 14
Condition33%1/3
ref $self and $self->isa(__PACKAGE__)
!ll&&!rl&&r
0014
MC/DC0%0/2
ref $self and $self->isa(__PACKAGE__)
ref $self $self->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= ref $selfB= $self->isa(__PACKAGE__)
ABresult
0X0
100
111
213 unless ref $self and $self->isa(__PACKAGE__);
214 14 ▶ croak "No date specified." unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 14 14
215 14 return $self->{parser}->( $self, @_ );
216 }
217  
218 0 ▶sub format_datetime {
Subroutine format_datetime: not called CC=1 SCAR=6.9
Pod documented
219 0 croak __PACKAGE__ . "::format_datetime not implemented.";
220 }
221  
222 23 require DateTime::Format::Builder::Parser;
223  
224 23 1;
225  
226 # ABSTRACT: Create DateTime parser classes and objects.
227  
228 __END__
« Builder/Parser.pm ↑ Summary Builder/Parser/Regex.pm »