Class.pm

filter: Statementstmt 82.8% 266 / 321 Branchbran 47.6% 60 / 126 Conditioncond 37.1% 29 / 78 MC/DCmcdc 2.2% 1 / 45 Subroutinesub 75.0% 48 / 64 Podpod 47.8% 11 / 23 totaltotal 63.1% 415 / 657 CC 106 SCAR 27.4
CC
106
Cov
68%
attribute
52.3
__ANON__
37.0
__ANON__
34.5
CRAP 15.5
« Class/Exporter.pm ↑ Summary Class/Prototype.pm »
1 # ABSTRACT: Self-Validating Object System and Data Validation Framework
2  
3 package Validation::Class;
4 {
5 56 $Validation::Class::VERSION = '7.10_01';
6 }
7  
8 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 56 our $VERSION = '7.10_01'; # VERSION
12  
13 56 ▶use Module::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 56 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 56 ▶use Hash::Merge 'merge';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 56 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 56 ▶use Validation::Class::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 56 ▶use Validation::Class::Fields;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 56 ▶use Validation::Class::Params;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 56 ▶use Validation::Class::Prototype;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 56 ▶use Validation::Class::Errors;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 {
25
26 56 my %CLASSES = ();
27
28 sub return_class_proto {
29
30 965 ▶ my $TARGET_CLASS = shift || caller(2);
Subroutine return_class_proto: called CC=7 SCAR=19.6
Pod undocumented
Condition66%2/3
shift() || caller 2
l!l&&r!l&&!r
8271380
MC/DC0%0/2
shift() || caller 2
shift() caller 2
Truth table66%2/3
A || B
A= shift()B= caller 2
ABresult
1X1
011
000
31
32 965 ▶ return $CLASSES{$TARGET_CLASS} ||= do {
Condition66%2/3
$CLASSES{$TARGET_CLASS} ||= do { no strict 'refs'; my $proto_class = "Validation::Class::Prototype"; my $proto = {"package", $TARGET_CLASS, "config", {}}; *{"${TARGET_CLASS}::new";} = sub { goto \&Validation::Class::new; } ; *{"${TARGET_CLASS}::proto";} = sub { goto \&Validation::Class::prototype; } ; *{"${TARGET_CLASS}::prototype";} = sub { goto \&Validation::Class::prototype; } ; my(@aliases) = $proto_class->proxy_methods; foreach my $alias (@aliases) { *{"${TARGET_CLASS}::$alias";} = sub { my $self = shift @_; my $proto = return_class_proto(ref $self); $proto->$alias(@_); } unless $TARGET_CLASS->can($alias); } ; my(@wrapped_aliases) = $proto_class->proxy_methods_wrapped; foreach my $alias (@wrapped_aliases) { *{"${TARGET_CLASS}::$alias";} = sub { my $self = shift @_; my $proto = return_class_proto(ref $self); $proto->$alias($self, @_); } unless $TARGET_CLASS->can($alias); } ; my $self = bless($proto, $proto_class); $self->{"config"} = merge($proto_class->configuration, $self->{"config"}); $self }
l!l&&r!l&&!r
879860
MC/DC0%0/2
$CLASSES{$TARGET_CLASS} ||= do { no strict 'refs'; my $proto_class = "Validation::Class::Prototype"; my $proto = {"package", $TARGET_CLASS, "config", {}}; *{"${TARGET_CLASS}::new";} = sub { goto \&Validation::Class::new; } ; *{"${TARGET_CLASS}::proto";} = sub { goto \&Validation::Class::prototype; } ; *{"${TARGET_CLASS}::prototype";} = sub { goto \&Validation::Class::prototype; } ; my(@aliases) = $proto_class->proxy_methods; foreach my $alias (@aliases) { *{"${TARGET_CLASS}::$alias";} = sub { my $self = shift @_; my $proto = return_class_proto(ref $self); $proto->$alias(@_); } unless $TARGET_CLASS->can($alias); } ; my(@wrapped_aliases) = $proto_class->proxy_methods_wrapped; foreach my $alias (@wrapped_aliases) { *{"${TARGET_CLASS}::$alias";} = sub { my $self = shift @_; my $proto = return_class_proto(ref $self); $proto->$alias($self, @_); } unless $TARGET_CLASS->can($alias); } ; my $self = bless($proto, $proto_class); $self->{"config"} = merge($proto_class->configuration, $self->{"config"}); $self }
$CLASSES{$TARGET_CLASS} do { no strict 'refs'; my $proto_class = "Validation::Class::Prototype"; my $proto = {"package", $TARGET_CLASS, "config", {}}; *{"${TARGET_CLASS}::new";} = sub { goto \&Validation::Class::new; } ; *{"${TARGET_CLASS}::proto";} = sub { goto \&Validation::Class::prototype; } ; *{"${TARGET_CLASS}::prototype";} = sub { goto \&Validation::Class::prototype; } ; my(@aliases) = $proto_class->proxy_methods; foreach my $alias (@aliases) { *{"${TARGET_CLASS}::$alias";} = sub { my $self = shift @_; my $proto = return_class_proto(ref $self); $proto->$alias(@_); } unless $TARGET_CLASS->can($alias); } ; my(@wrapped_aliases) = $proto_class->proxy_methods_wrapped; foreach my $alias (@wrapped_aliases) { *{"${TARGET_CLASS}::$alias";} = sub { my $self = shift @_; my $proto = return_class_proto(ref $self); $proto->$alias($self, @_); } unless $TARGET_CLASS->can($alias); } ; my $self = bless($proto, $proto_class); $self->{"config"} = merge($proto_class->configuration, $self->{"config"}); $self }
Truth table66%2/3
A ||= B
A= $CLASSES{$TARGET_CLASS}B= do { no strict 'refs'; my $proto_class = "Validation::Class::Prototype"; my $proto = {"package", $TARGET_CLASS, "config", {}}; *{"${TARGET_CLASS}::new";} = sub { goto \&Validation::Class::new; } ; *{"${TARGET_CLASS}::proto";} = sub { goto \&Validation::Class::prototype; } ; *{"${TARGET_CLASS}::prototype";} = sub { goto \&Validation::Class::prototype; } ; my(@aliases) = $proto_class->proxy_methods; foreach my $alias (@aliases) { *{"${TARGET_CLASS}::$alias";} = sub { my $self = shift @_; my $proto = return_class_proto(ref $self); $proto->$alias(@_); } unless $TARGET_CLASS->can($alias); } ; my(@wrapped_aliases) = $proto_class->proxy_methods_wrapped; foreach my $alias (@wrapped_aliases) { *{"${TARGET_CLASS}::$alias";} = sub { my $self = shift @_; my $proto = return_class_proto(ref $self); $proto->$alias($self, @_); } unless $TARGET_CLASS->can($alias); } ; my $self = bless($proto, $proto_class); $self->{"config"} = merge($proto_class->configuration, $self->{"config"}); $self }
ABresult
1X1
011
000
33
34 56 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
35
36 86 my $proto_class = 'Validation::Class::Prototype';
37
38 86 my $proto = {
39 package => $TARGET_CLASS,
40 config => {}
41 };
42
43 # injected into every derived class
44
45 86 ▶ *{"$TARGET_CLASS\::new"} = sub { goto \&new };
Subroutine __ANON__: called CC=1 SCAR=0.0
46 86 ▶ *{"$TARGET_CLASS\::proto"} = sub { goto \&prototype };
Subroutine __ANON__: called CC=1 SCAR=0.0
47 86 ▶ *{"$TARGET_CLASS\::prototype"} = sub { goto \&prototype };
Subroutine __ANON__: not called CC=1 SCAR=0.4
48
49 # inject prototype class aliases unless exist
50
51 86 my @aliases = $proto_class->proxy_methods;
52
53 86 foreach my $alias (@aliases) {
54
55 # slight-of-hand
56 1892 *{"$TARGET_CLASS\::$alias"} = sub {
57
58 432 ▶ my $self = shift @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
59
60 432 my $proto = return_class_proto(ref $self); # isnt recursive
61
62 432 $proto->$alias(@_);
63
64 1892 ▶ } unless $TARGET_CLASS->can($alias);
Branch: 1 branch
BranchTrueFalseTotal
unless $TARGET_CLASS->can($alias) 1892 0 1892
65
66 }
67
68 # inject wrapped prototype class aliases unless exist
69
70 86 my @wrapped_aliases = $proto_class->proxy_methods_wrapped;
71
72 86 foreach my $alias (@wrapped_aliases) {
73
74 # slight-of-hand
75 258 *{"$TARGET_CLASS\::$alias"} = sub {
76
77 145 ▶ my $self = shift @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
78
79 145 my $proto = return_class_proto(ref $self); # isnt recursive
80
81 145 $proto->$alias($self, @_);
82
83 258 ▶ } unless $TARGET_CLASS->can($alias);
Branch: 1 branch
BranchTrueFalseTotal
unless $TARGET_CLASS->can($alias) 258 0 258
84
85 }
86
87 86 my $self = bless $proto, $proto_class;
88
89 86 $self->{config} = merge $proto_class->configuration, $self->{config};
90
91 86 $self; # return-once
92
93 };
94
95 }
96
97 sub configure_class_proto {
98
99 138 ▶ my $configuration_routine = shift;
Subroutine configure_class_proto: called CC=2 SCAR=7.0
Pod undocumented
100
101 138 ▶ return undef unless "CODE" eq ref $configuration_routine;
Branch: 1 branch
BranchTrueFalseTotal
unless "CODE" eq ref $configuration_routine 0 138 138
102
103 56 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
104
105 138 my $proto = return_class_proto;
106
107 138 $configuration_routine->($proto);
108
109 138 return $proto;
110
111 }
112
113 }
114  
115 56 sub import {
116
117 85 ▶ my $caller = caller(0) || caller(1);
Subroutine import: called CC=3 SCAR=11.6
Condition33%1/3
caller 0 || caller 1
l!l&&r!l&&!r
8500
MC/DC0%0/2
caller 0 || caller 1
caller 0 caller 1
Truth table33%1/3
A || B
A= caller 0B= caller 1
ABresult
1X1
011
000
118
119 85 ▶ if ($caller) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caller) 85 0 85
120
121 85 return_class_proto $caller # create prototype instance when used
122
123 }
124
125 85 strict->import;
126 85 warnings->import;
127
128 85 __PACKAGE__->export_to_level(1, @_);
129
130 }
131  
132 56 our @ISA = qw(Exporter);
133 56 our @EXPORT = qw(
134  
135 attribute
136 bld
137 build
138 dir
139 directive
140 fld
141 field
142 flt
143 filter
144 has
145 load
146 mth
147 method
148 mxn
149 mixin
150 pro
151 profile
152 set
153  
154 );
155  
156  
157  
158 0 ▶sub has { goto &attribute }
Subroutine has: not called CC=1 SCAR=6.9
Pod undocumented
159 sub attribute {
160
161 0 ▶ my ($attrs, $default) = @_;
Subroutine attribute: not called CC=8 SCAR=39.6
Pod documented
162  
163 0 ▶ return unless $attrs;
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs 0 0 0
164  
165 0 ▶ confess "Error creating accessor, default must be a coderef or constant"
Branch: 1 branch
BranchTrueFalseTotal
if ref $default and ref $default ne "CODE" 0 0 0
Condition0%0/3
ref $default and ref $default ne "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $default and ref $default ne "CODE"
ref $default ref $default ne "CODE"
Truth table0%0/3
A and B
A= ref $defaultB= ref $default ne "CODE"
ABresult
0X0
100
111
166 if ref $default && ref $default ne 'CODE';
167  
168 0 ▶ $attrs = [$attrs] unless ref $attrs eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attrs eq "ARRAY" 0 0 0
169  
170 0 for my $attr (@$attrs) {
171  
172 0 ▶ confess "Error creating accessor '$attr', name has invalid characters"
Branch: 1 branch
BranchTrueFalseTotal
unless $attr =~ /^[a-zA-Z_]\w*$/ 0 0 0
173 unless $attr =~ /^[a-zA-Z_]\w*$/;
174
175 0 my $code ;
176
177 0 ▶ if (defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) { } 0 0 0
178
179 $code = sub {
180
181 0 ▶ if (@_ == 1) {
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 0 0 0
182 0 ▶ return $_[0]->{$attr} if exists $_[0]->{$attr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{$attr} 0 0 0
183 0 ▶ return $_[0]->{$attr} = ref $default eq 'CODE' ?
Branch: 1 branch
BranchTrueFalseTotal
ref $default eq 'CODE' ? : 0 0 0
184 $default->($_[0]) : $default;
185 }
186 0 $_[0]->{$attr} = $_[1]; $_[0];
187
188 0 };
189
190 }
191
192 else {
193
194 $code = sub {
195
196 0 ▶ return $_[0]->{$attr} if @_ == 1;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 0 0
197 0 $_[0]->{$attr} = $_[1]; $_[0];
198
199 0 };
200
201 }
202
203 return configure_class_proto sub {
204
205 0 ▶ my ($proto) = @_;
Subroutine __ANON__: not called CC=2 SCAR=9.2
206
207 0 ▶ my $accessors = $proto->{config}->{ATTRIBUTES} ||= {};
Condition0%0/2
$proto->{"config"}{"ATTRIBUTES"} ||= {}
l!l
00
MC/DC0%0/1
$proto->{"config"}{"ATTRIBUTES"} ||= {}
$proto->{"config"}{"ATTRIBUTES"}
Truth table0%0/2
A
A= $proto->{"config"}{"ATTRIBUTES"}
Aresult
11
00
208
209 56 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
210 56 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
211
212 0 *{"$proto->{package}\::$attr"} = $accessors->{$attr} = $code;
213
214 0 };
215  
216 }
217
218 }
219  
220  
221 1 ▶sub bld { goto &build }
Subroutine bld: called CC=1 SCAR=0.0
Pod undocumented
222 sub build {
223
224 1 ▶ my ($code) = @_;
Subroutine build: called CC=2 SCAR=7.1
Pod documented
225
226 1 ▶ return undef unless ("CODE" eq ref $code);
Branch: 1 branch
BranchTrueFalseTotal
unless "CODE" eq ref $code 0 1 1
227
228 return configure_class_proto sub {
229
230 1 ▶ my ($proto) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
231
232 1 ▶ $proto->{config}->{BUILDERS} ||= [];
Condition50%1/2
$proto->{"config"}{"BUILDERS"} ||= []
l!l
10
MC/DC0%0/1
$proto->{"config"}{"BUILDERS"} ||= []
$proto->{"config"}{"BUILDERS"}
Truth table50%1/2
A
A= $proto->{"config"}{"BUILDERS"}
Aresult
11
00
233
234 1 push @{$proto->{config}->{BUILDERS}}, $code;
235
236 1 };
237
238 }
239  
240  
241 0 ▶sub dir { goto &directive }
Subroutine dir: not called CC=1 SCAR=6.9
Pod undocumented
242 sub directive {
243
244 0 ▶ my ($name, $data) = @_;
Subroutine directive: not called CC=3 SCAR=24.8
Pod documented
245
246 0 ▶ return undef unless ($name && $data);
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $data 0 0 0
Condition0%0/3
$name and $data
!ll&&!rl&&r
000
MC/DC0%0/2
$name and $data
$name $data
Truth table0%0/3
A and B
A= $nameB= $data
ABresult
0X0
100
111
247
248 return configure_class_proto sub {
249
250 0 ▶ my ($proto) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
251
252 0 ▶ $proto->{config}->{DIRECTIVES} ||= {};
Condition0%0/2
$proto->{"config"}{"DIRECTIVES"} ||= {}
l!l
00
MC/DC0%0/1
$proto->{"config"}{"DIRECTIVES"} ||= {}
$proto->{"config"}{"DIRECTIVES"}
Truth table0%0/2
A
A= $proto->{"config"}{"DIRECTIVES"}
Aresult
11
00
253
254 0 $proto->{config}->{DIRECTIVES}->{$name} = {
255
256 mixin => 1,
257 field => 1,
258 validator => $data
259
260 };
261
262 0 };
263
264 }
265  
266  
267 9 ▶sub fld { goto &field }
Subroutine fld: called CC=1 SCAR=0.0
Pod undocumented
268 sub field {
269
270 101 ▶ my ($name, $data) = @_;
Subroutine field: called CC=5 SCAR=16.5
Pod documented
271
272 101 ▶ return undef unless ($name && $data);
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $data 0 101 101
Condition33%1/3
$name and $data
!ll&&!rl&&r
00101
MC/DC0%0/2
$name and $data
$name $data
Truth table33%1/3
A and B
A= $nameB= $data
ABresult
0X0
100
111
273
274 101 ▶ confess "Error creating field $name, name is using unconventional naming"
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^[a-zA-Z_](([\w\.]+)?\w)$/ xor $name =~ /^[a-zA-Z_](([\w\.]+)?\w)\:\d+$/ 0 101 101
Condition50%2/4
$name =~ /^[a-zA-Z_](([\w\.]+)?\w)$/ xor $name =~ /^[a-zA-Z_](([\w\.]+)?\w)\:\d+$/
l&&rl&&!r!l&&r!l&&!r
09830
MC/DC0%0/2
$name =~ /^[a-zA-Z_](([\w\.]+)?\w)$/ xor $name =~ /^[a-zA-Z_](([\w\.]+)?\w)\:\d+$/
$name =~ /^[a-zA-Z_](([\w\.]+)?\w)$/ $name =~ /^[a-zA-Z_](([\w\.]+)?\w)\:\d+$/
Truth table50%2/4
A xor B
A= $name =~ /^[a-zA-Z_](([\w\.]+)?\w)$/B= $name =~ /^[a-zA-Z_](([\w\.]+)?\w)\:\d+$/
ABresult
110
101
011
000
275 unless $name =~ /^[a-zA-Z_](([\w\.]+)?\w)$/
276 xor $name =~ /^[a-zA-Z_](([\w\.]+)?\w)\:\d+$/;
277
278 return configure_class_proto sub {
279
280 101 ▶ my ($proto) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
281
282 101 ▶ $proto->{config}->{FIELDS} ||= {};
Condition50%1/2
$proto->{"config"}{"FIELDS"} ||= {}
l!l
1010
MC/DC0%0/1
$proto->{"config"}{"FIELDS"} ||= {}
$proto->{"config"}{"FIELDS"}
Truth table50%1/2
A
A= $proto->{"config"}{"FIELDS"}
Aresult
11
00
283
284 confess "Error creating accessor $name on $proto->{package}, ".
285 101 ▶ "attribute collision" if exists $proto->{config}->{FIELDS}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $proto->{"config"}{"FIELDS"}{$name} 0 101 101
286
287 confess "Error creating accessor $name on $proto->{package}, ".
288 101 ▶ "method collision" if $proto->{package}->can($name);
Branch: 1 branch
BranchTrueFalseTotal
if $proto->{"package"}->can($name) 0 101 101
289
290 101 $data->{name} = $name;
291
292 101 $proto->{config}->{FIELDS}->{$name} = $data;
293
294 56 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
295
296 101 my $accessor = $name;
297
298 101 $accessor =~ s/[^a-zA-Z0-9_]/_/g;
299
300 my $accessor_routine = sub {
301
302 33 ▶ my ($self, $data) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
303
304 33 my $proto = $self->proto;
305 33 my $fields = $proto->fields;
306 33 my $result = undef;
307
308 33 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 31 2 33
309
310 31 $proto->set_params($name => $data);
311
312 }
313
314 33 $result = $proto->get_value($name);
315
316 33 return $result;
317
318 101 };
319
320 101 *{"$proto->{package}\::$accessor"} = $accessor_routine;
321
322 101 };
323
324 }
325  
326  
327 0 ▶sub flt { goto &filter }
Subroutine flt: not called CC=1 SCAR=6.9
Pod undocumented
328 sub filter {
329
330 0 ▶ my ($name, $data) = @_;
Subroutine filter: not called CC=3 SCAR=24.8
Pod documented
331
332 0 ▶ return undef unless ($name && $data);
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $data 0 0 0
Condition0%0/3
$name and $data
!ll&&!rl&&r
000
MC/DC0%0/2
$name and $data
$name $data
Truth table0%0/3
A and B
A= $nameB= $data
ABresult
0X0
100
111
333
334 return configure_class_proto sub {
335
336 0 ▶ my ($proto) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
337
338 0 ▶ $proto->{config}->{FILTERS} ||= {};
Condition0%0/2
$proto->{"config"}{"FILTERS"} ||= {}
l!l
00
MC/DC0%0/1
$proto->{"config"}{"FILTERS"} ||= {}
$proto->{"config"}{"FILTERS"}
Truth table0%0/2
A
A= $proto->{"config"}{"FILTERS"}
Aresult
11
00
339
340 0 $proto->{config}->{FILTERS}->{$name} = $data;
341
342 0 };
343
344 }
345  
346  
347 3 ▶sub set { goto &load }
Subroutine set: called CC=1 SCAR=0.0
Pod undocumented
348 sub load {
349
350 12 ▶ my $data = pop @_;
Subroutine load: called CC=1 SCAR=0.1
Pod documented
351 12 my $self = pop @_;
352
353 return configure_class_proto sub {
354
355 12 ▶ my $proto = $self ? $self->proto : $_[0];
Subroutine __ANON__: called CC=30 SCAR=36.1
Branch: 1 branch
BranchTrueFalseTotal
$self ? : 0 12 12
356
357 12 my $name = $proto->{package};
358
359 12 ▶ $proto->{config}->{BUILDERS} ||= [];
Condition50%1/2
$proto->{"config"}{"BUILDERS"} ||= []
l!l
120
MC/DC0%0/1
$proto->{"config"}{"BUILDERS"} ||= []
$proto->{"config"}{"BUILDERS"}
Truth table50%1/2
A
A= $proto->{"config"}{"BUILDERS"}
Aresult
11
00
360  
361 12 ▶ if ($data->{classes}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"classes"}) 4 8 12
362
363 4 my @parents ;
364
365 4 ▶ if (! ref $data->{classes} && $data->{classes} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $data->{'classes'} and $data->{'classes'} == 1) { } 3 1 4
Condition66%2/3
not ref $data->{"classes"} and $data->{"classes"} == 1
!ll&&!rl&&r
103
MC/DC50%1/2
not ref $data->{"classes"} and $data->{"classes"} == 1
not ref $data->{"classes"} $data->{"classes"} == 1
Truth table66%2/3
not(A) and B
A= not ref $data->{"classes"}B= $data->{"classes"} == 1
ABresult
0X0
100
111
366
367 3 push @parents, $name;
368
369 }
370
371 else {
372
373 push @parents, "ARRAY" eq ref $data->{classes} ?
374 1 ▶ @{$data->{classes}} : $data->{classes};
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $data->{'classes'} ? : 1 0 1
375
376 }
377
378 4 foreach my $parent (@parents) {
379
380 # load class children and create relationship map (hash)
381 4 foreach my $child (useall $parent) {
382
383 20 my $nickname = $child;
384 20 $nickname =~ s/^$name//;
385 20 $nickname =~ s/^:://;
386 20 $nickname =~ s/([a-z])([A-Z])/$1\_$2/g;
387 20 $nickname =~ s/::/-/g;
388
389 20 ▶ $proto->{config}->{RELATIVES} ||= {};
Condition50%1/2
$proto->{"config"}{"RELATIVES"} ||= {}
l!l
200
MC/DC0%0/1
$proto->{"config"}{"RELATIVES"} ||= {}
$proto->{"config"}{"RELATIVES"}
Truth table50%1/2
A
A= $proto->{"config"}{"RELATIVES"}
Aresult
11
00
390 20 $proto->{config}->{RELATIVES}->{lc $nickname} = $child;
391
392
393 }
394
395 }
396
397 }
398
399 12 ▶ if ($data->{plugins}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"plugins"}) 4 8 12
400
401 4 my @plugins ;
402
403 push @plugins, "ARRAY" eq ref $data->{plugins} ?
404 4 ▶ @{$data->{plugins}} : $data->{plugins};
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $data->{'plugins'} ? : 4 0 4
405
406 4 foreach my $plugin (@plugins) {
407
408 4 ▶ if ($plugin !~ /^\+/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($plugin =~ /^\+/) 0 4 4
409
410 0 $plugin = "Validation::Class::Plugin::$plugin";
411
412 }
413
414 4 $plugin =~ s/^\+//;
415
416 # require plugin
417 4 my $file = $plugin;
418 4 $file =~ s/::/\//g;
419 4 $file .= ".pm";
420
421 eval "require $plugin"
422 4 ▶ unless $INC{$file}; # unless already loaded
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{$file} 0 4 4
423
424 }
425
426 4 $proto->{config}->{PLUGINS}->{$_} = 1 for @plugins;
427
428 }
429
430 # attach roles
431 12 ▶ if (grep { $data->{$_} } qw/base bases role roles/) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$data->{$_};} "base", "bases", "role", "roles") 5 7 12
432
433 5 my @roles ;
434
435 my $alias =
436 $data->{base} || $data->{role} ||
437 5 ▶ $data->{roles} || $data->{bases};
Condition33%3/9
$data->{'base'} || $data->{'role'} || $data->{'roles'} || $data->{'bases'}
l!l&&r!l&&!r
500
$data->{"base"} or $data->{"role"} or $data->{"roles"}
l!l&&r!l&&!r
500
$data->{"base"} or $data->{"role"}
l!l&&r!l&&!r
500
MC/DC0%0/4
$data->{'base'} || $data->{'role'} || $data->{'roles'} || $data->{'bases'}
$data->{"base"} $data->{"role"} $data->{"roles"} $data->{'bases'}
Truth table20%1/5
A or B or C || D
A= $data->{"base"}B= $data->{"role"}C= $data->{"roles"}D= $data->{'bases'}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
438
439 5 ▶ if ($alias) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alias) 5 0 5
440
441 push @roles, "ARRAY" eq ref $alias ?
442 5 ▶ @{$alias} : $alias;
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $alias ? : 5 0 5
443
444 }
445
446 5 ▶ if (@roles) {
Branch: 1 branch
BranchTrueFalseTotal
if (@roles) 5 0 5
447
448 5 foreach my $role (@roles) {
449
450 # require plugin
451 5 my $file = $role;
452 5 $file =~ s/::/\//g;
453 5 $file .= ".pm";
454
455 56 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
456
457 eval "require $role"
458 5 ▶ unless $INC{$file}; # unless already loaded
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{$file} 5 0 5
459
460 250 my @routines = grep { defined &{"$role\::$_"} }
461 5 keys %{"$role\::"};
462
463 5 ▶ if (@routines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@routines) 5 0 5
464
465 # copy methods
466 5 foreach my $routine (@routines) {
467
468 eval {
469
470 10 *{"$proto->{package}\::$routine"} =
471 10 *{"$role\::$routine"}
472
473 240 ▶ } unless $proto->{package}->can($routine);
Branch: 1 branch
BranchTrueFalseTotal
unless $proto->{"package"}->can($routine) 10 230 240
474 # maybe I should issue a warning?
475
476 }
477
478 5 my $role_proto = return_class_proto $role;
479
480 5 ▶ $proto->{config} ||= {}; # good measure
Condition50%1/2
$proto->{"config"} ||= {}
l!l
50
MC/DC0%0/1
$proto->{"config"} ||= {}
$proto->{"config"}
Truth table50%1/2
A
A= $proto->{"config"}
Aresult
11
00
481 5 ▶ $role_proto->{config} ||= {}; # good measure
Condition50%1/2
$role_proto->{"config"} ||= {}
l!l
50
MC/DC0%0/1
$role_proto->{"config"} ||= {}
$role_proto->{"config"}
Truth table50%1/2
A
A= $role_proto->{"config"}
Aresult
11
00
482
483 # merge configs
484 $proto->{config} =
485 5 merge $proto->{config}, $role_proto->{config};
486
487 }
488
489 }
490
491 }
492
493 }
494
495 12 };
496
497 }
498  
499  
500 1 ▶sub mth { goto &method }
Subroutine mth: called CC=1 SCAR=0.0
Pod undocumented
501 sub method {
502  
503 1 ▶ my ($name, $data) = @_;
Subroutine method: called CC=3 SCAR=13.4
Pod documented
504  
505 1 ▶ return undef unless ($name && $data);
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $data 0 1 1
Condition33%1/3
$name and $data
!ll&&!rl&&r
001
MC/DC0%0/2
$name and $data
$name $data
Truth table33%1/3
A and B
A= $nameB= $data
ABresult
0X0
100
111
506
507 return configure_class_proto sub {
508
509 1 ▶ my ($proto) = @_;
Subroutine __ANON__: called CC=7 SCAR=24.5
510
511 1 ▶ $proto->{config}->{METHODS} ||= {};
Condition50%1/2
$proto->{"config"}{"METHODS"} ||= {}
l!l
10
MC/DC0%0/1
$proto->{"config"}{"METHODS"} ||= {}
$proto->{"config"}{"METHODS"}
Truth table50%1/2
A
A= $proto->{"config"}{"METHODS"}
Aresult
11
00
512 1 ▶ $proto->{config}->{ATTRIBUTES} ||= {};
Condition50%1/2
$proto->{"config"}{"ATTRIBUTES"} ||= {}
l!l
10
MC/DC0%0/1
$proto->{"config"}{"ATTRIBUTES"} ||= {}
$proto->{"config"}{"ATTRIBUTES"}
Truth table50%1/2
A
A= $proto->{"config"}{"ATTRIBUTES"}
Aresult
11
00
513
514 confess "Error creating method $name on $proto->{package}, ".
515 "collides with attribute $name"
516 1 ▶ if exists $proto->{config}->{ATTRIBUTES}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $proto->{"config"}{"ATTRIBUTES"}{$name} 0 1 1
517
518 confess "Error creating method $name on $proto->{package}, ".
519 "collides with method $name"
520 1 ▶ if $proto->{package}->can($name);
Branch: 1 branch
BranchTrueFalseTotal
if $proto->{"package"}->can($name) 0 1 1
521
522 # create method
523
524 confess "Error creating method $name, requires 'input' and 'using' options"
525 1 ▶ unless $data->{input} && $data->{using};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{"input"} and $data->{"using"} 0 1 1
Condition33%1/3
$data->{"input"} and $data->{"using"}
!ll&&!rl&&r
001
MC/DC0%0/2
$data->{"input"} and $data->{"using"}
$data->{"input"} $data->{"using"}
Truth table33%1/3
A and B
A= $data->{"input"}B= $data->{"using"}
ABresult
0X0
100
111
526
527 1 $proto->{config}->{METHODS}->{$name} = $data;
528
529 56 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
530
531 1 *{"$proto->{package}\::$name"} = sub {
532
533 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=12 SCAR=35.4
534 1 my @args = @_;
535
536 1 my $validator;
537
538 1 my $input = $data->{'input'};
539 1 my $using = $data->{'using'};
540 1 my $output = $data->{'output'};
541
542 1 ▶ if ($input) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input) 1 0 1
543
544 $validator = "ARRAY" eq ref $input ?
545
546 # validate fields
547 0 ▶ sub { $self->validate(@{$input}) } :
Subroutine __ANON__: not called CC=1 SCAR=6.9
548
549 # validate profile
550 1 ▶ sub { $self->validate_profile($input, @args) } ;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $input ? : 0 1 1
551
552 }
553
554 1 ▶ if ($using) {
Branch: 1 branch
BranchTrueFalseTotal
if ($using) 1 0 1
555
556 1 ▶ if ("CODE" eq ref $using) {
Branch: 1 branch
BranchTrueFalseTotal
if ('CODE' eq ref $using) { } 1 0 1
557
558 1 my $error = "Method $name failed to validate";
559
560 # run input validation
561 1 ▶ if ("CODE" eq ref $validator) {
Branch: 1 branch
BranchTrueFalseTotal
if ("CODE" eq ref $validator) 1 0 1
562
563 1 ▶ unless ($validator->(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$validator(@args)) 0 1 1
564
565 0 ▶ unshift @{$self->errors}, $error
Branch: 1 branch
BranchTrueFalseTotal
if $self->report_failure 0 0 0
566 if $self->report_failure;
567
568 0 ▶ confess $error. " input, ". $self->errors_to_string
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ignore_failure 0 0 0
569 if ! $self->ignore_failure;
570
571 0 return undef;
572
573 }
574
575 }
576
577 # execute routine
578 1 my $return = $data->{using}->($self, @args);
579
580 # run output validation
581 1 ▶ if ($output) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output) 0 1 1
582
583 $validator = "ARRAY" eq ref $output ?
584
585 # validate fields
586 0 ▶ sub { $self->validate(@{$output}) } :
Subroutine __ANON__: not called CC=1 SCAR=6.9
587
588 # validate profile
589 0 ▶ sub { $self->validate_profile($output, @args) } ;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $output ? : 0 0 0
590
591 0 ▶ confess $error. " output, ". $self->errors_to_string
Branch: 1 branch
BranchTrueFalseTotal
unless &$validator(@args) 0 0 0
592 unless $validator->(@args);
593
594 }
595
596 1 return $return;
597
598 }
599
600 else {
601
602 0 confess "Error executing $name, no associated coderef";
603
604 }
605
606 }
607
608 0 return undef;
609
610 1 };
611
612 1 };
613  
614 }
615  
616  
617 1 ▶sub mxn { goto &mixin }
Subroutine mxn: called CC=1 SCAR=0.0
Pod undocumented
618 sub mixin {
619  
620 19 ▶ my ($name, $data) = @_;
Subroutine mixin: called CC=3 SCAR=11.8
Pod documented
621
622 19 ▶ return undef unless ($name && $data);
Branch: 1 branch
BranchTrueFalseTotal
unless $name and $data 0 19 19
Condition33%1/3
$name and $data
!ll&&!rl&&r
0019
MC/DC0%0/2
$name and $data
$name $data
Truth table33%1/3
A and B
A= $nameB= $data
ABresult
0X0
100
111
623  
624 return configure_class_proto sub {
625
626 19 ▶ my $proto = shift;
Subroutine __ANON__: called CC=2 SCAR=7.1
627
628 19 ▶ $proto->{config}->{MIXINS} ||= {};
Condition50%1/2
$proto->{"config"}{"MIXINS"} ||= {}
l!l
190
MC/DC0%0/1
$proto->{"config"}{"MIXINS"} ||= {}
$proto->{"config"}{"MIXINS"}
Truth table50%1/2
A
A= $proto->{"config"}{"MIXINS"}
Aresult
11
00
629
630 19 $proto->{config}->{MIXINS}->{$name} = $data;
631
632 19 };
633  
634 }
635  
636  
637 sub new {
638  
639 93 ▶ my $class = shift;
Subroutine new: called CC=14 SCAR=26.6
Pod documented
640
641 93 my $proto = return_class_proto $class;
642
643 93 my $config = $proto->{config};
644
645 93 my $self = bless {}, $class;
646
647 # clone config values
648
649 93 my @clonables = qw(fields filters methods mixins profiles relatives) ;
650
651 93 $proto->{$_} = merge $proto->{config}->{uc $_}, $proto->{$_} for @clonables;
652
653 93 ▶ my %ARGS = @_ ? @_ > 1 ? @_ : "HASH" eq ref $_[0] ? %{$_[0]} : () : ();
Branch: 3 branches
BranchTrueFalseTotal
@_ ? : 84 9 93
@_ > 1 ? : 84 0 84
'HASH' eq ref $_[0] ? : 0 0 0
654
655 # bless special collections
656
657 93 $proto->{errors} = Validation::Class::Errors->new;
658 93 $proto->{params} = Validation::Class::Params->new;
659 93 $proto->{fields} = Validation::Class::Fields->new($proto->{fields}); #!!!
660
661 # process overridable attributes
662
663 93 ▶ my $fields = delete $ARGS{fields} if defined $ARGS{fields};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ARGS{"fields"} 45 48 93
664 93 ▶ my $params = delete $ARGS{params} if defined $ARGS{params};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ARGS{"params"} 73 20 93
665
666 93 ▶ $proto->set_fields($fields) if $fields;
Branch: 1 branch
BranchTrueFalseTotal
if $fields 45 48 93
667 93 ▶ $proto->set_params($params) if $params;
Branch: 1 branch
BranchTrueFalseTotal
if $params 73 20 93
668
669 # process attribute assignments
670
671 93 ▶ while (my($attr, $value) = each (%ARGS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %ARGS) 83 93 176
672
673 83 $self->$attr($value);
674
675 }
676
677 # process plugins
678
679 93 foreach my $plugin (keys %{$config->{PLUGINS}}) {
680
681 4 ▶ $plugin->new($self) if $plugin->can('new');
Branch: 1 branch
BranchTrueFalseTotal
if $plugin->can("new") 4 0 4
682
683 }
684
685 # process builders
686
687 93 foreach my $builder (@{$config->{BUILDERS}}) {
688
689 1 $builder->($self);
690
691 }
692
693 # initialize prototype
694
695 93 $proto->normalize;
696 93 ▶ $proto->apply_filters('pre') if $proto->filtering;
Branch: 1 branch
BranchTrueFalseTotal
if $proto->filtering 92 1 93
697
698 # ready-set-go !!!
699
700 93 return $self;
701  
702 }
703  
704  
705 1 ▶sub pro { goto &profile }
Subroutine pro: called CC=1 SCAR=0.0
Pod undocumented
706 sub profile {
707  
708 4 ▶ my ($name, $data) = @_;
Subroutine profile: called CC=3 SCAR=11.8
Pod documented
709  
710 4 ▶ return undef unless ($name && "CODE" eq ref $data);
Branch: 1 branch
BranchTrueFalseTotal
unless $name and "CODE" eq ref $data 0 4 4
Condition33%1/3
$name and "CODE" eq ref $data
!ll&&!rl&&r
004
MC/DC0%0/2
$name and "CODE" eq ref $data
$name "CODE" eq ref $data
Truth table33%1/3
A and B
A= $nameB= "CODE" eq ref $data
ABresult
0X0
100
111
711
712 return configure_class_proto sub {
713
714 4 ▶ my ($proto) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.1
715
716 4 ▶ $proto->{config}->{PROFILES} ||= {};
Condition50%1/2
$proto->{"config"}{"PROFILES"} ||= {}
l!l
40
MC/DC0%0/1
$proto->{"config"}{"PROFILES"} ||= {}
$proto->{"config"}{"PROFILES"}
Truth table50%1/2
A
A= $proto->{"config"}{"PROFILES"}
Aresult
11
00
717
718 4 $proto->{config}->{PROFILES}->{$name} = $data;
719
720 4 };
721  
722 }
723  
724  
725 0 ▶sub proto { goto &prototype }
Subroutine proto: not called CC=1 SCAR=6.9
Pod undocumented
726 sub prototype {
727
728 67 ▶ my ($self) = pop @_;
Subroutine prototype: called CC=2 SCAR=7.1
Pod documented
729
730 67 ▶ return_class_proto ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
6610
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
731
732 }
733  
734  
735  
736 56 1;
737 __END__
« Class/Exporter.pm ↑ Summary Class/Prototype.pm »