blib/lib/Class/Container.pm

filter: Statementstmt 82.6% 214 / 259 Branchbran 60.4% 87 / 144 Conditioncond 60.0% 30 / 50 MC/DCmcdc 25.0% 8 / 32 Subroutinesub 90.9% 30 / 33 Podpod 70.5% 12 / 17 totaltotal 71.2% 381 / 535 CC 102 SCAR 41.0
CC
102
Cov
73%
all_specs
380.0
delayed_object_params
20.0
create_contained_objects
16.1
CRAP 60.5
↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Class::Container;
3 {
4 2 $Class::Container::VERSION = '0.13';
5 }
6  
7 2 my $HAVE_WEAKEN;
8 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
9 2 eval {
10 2 require Scalar::Util;
11 2 Scalar::Util->import('weaken');
12 2 $HAVE_WEAKEN = 1;
13 };
14
15 2 ▶ *weaken = sub {} unless defined &weaken;
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
unless defined &Class::Container::weaken 0 2 2
16 }
17  
18 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # The create_contained_objects() method lets one object
21 # (e.g. Compiler) transparently create another (e.g. Lexer) by passing
22 # creator parameters through to the created object.
23 #
24 # Any auto-created objects should be declared in a class's
25 # %CONTAINED_OBJECTS hash. The keys of this hash are objects which
26 # can be created and the values are the default classes to use.
27  
28 # For instance, the key 'lexer' indicates that a 'lexer' parameter
29 # should be silently passed through, and a 'lexer_class' parameter
30 # will trigger the creation of an object whose class is specified by
31 # the value. If no value is present there, the value of 'lexer' in
32 # the %CONTAINED_OBJECTS hash is used. If no value is present there,
33 # no contained object is created.
34 #
35 # We return the list of parameters for the creator. If contained
36 # objects were auto-created, their creation parameters aren't included
37 # in the return value. This lets the creator be totally ignorant of
38 # the creation parameters of any objects it creates.
39  
40 2 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶Params::Validate::validation_options( on_fail => sub { die @_ } );
Subroutine __ANON__: called CC=1 SCAR=0.0
42  
43 2 my %VALID_PARAMS = ();
44 2 my %CONTAINED_OBJECTS = ();
45 2 my %VALID_CACHE = ();
46 2 my %CONTAINED_CACHE = ();
47 2 my %DECORATEES = ();
48  
49 sub new
50 54 ▶{
Subroutine new: called CC=5 SCAR=16.4
Pod documented
51 54 my $proto = shift;
52 54 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0540
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
53 54 my $self = bless scalar validate_with
54 (
55 params => $class->create_contained_objects(@_),
56 spec => $class->validation_spec,
57 called => "$class->new()",
58 ), $class;
59 51 ▶ if ($HAVE_WEAKEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HAVE_WEAKEN) 51 0 51
60 51 my $c = $self->get_contained_object_spec;
61 51 foreach my $name (keys %$c) {
62 40 ▶ next if $c->{$name}{delayed};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{$name}{"delayed"} 19 21 40
63 21 $self->{$name}{container}{container} = $self;
64 21 weaken $self->{$name}{container}{container};
65 }
66 }
67 51 return $self;
68 }
69  
70 sub all_specs
71 0 ▶{
Subroutine all_specs: not called CC=19 SCAR=59.4
Pod undocumented
72 0 require B::Deparse;
73 0 my %out;
74  
75 0 foreach my $class (sort keys %VALID_PARAMS)
76 {
77 0 my $params = $VALID_PARAMS{$class};
78  
79 0 foreach my $name (sort keys %$params)
80 {
81 0 my $spec = $params->{$name};
82 0 my ($type, $default);
83 0 ▶ if ($spec->{isa}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{'isa'}) { } 0 0 0
84 0 my $obj_class;
85  
86 0 $type = 'object';
87  
88 0 ▶ if (exists $CONTAINED_OBJECTS{$class}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $CONTAINED_OBJECTS{$class}{$name}) 0 0 0
89 0 $default = "$CONTAINED_OBJECTS{$class}{$name}{class}->new";
90 }
91 } else {
92 0 ($type, $default) = ($spec->{parse}, $spec->{default});
93 }
94  
95 0 ▶ if (ref($default) eq 'CODE') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $default eq 'CODE') { } 0 0 0
elsif (ref $default eq 'ARRAY') { } 0 0 0
elsif (ref $default eq 'Regexp') { } 0 0 0
96 0 $default = 'sub ' . B::Deparse->new()->coderef2text($default);
97 0 $default =~ s/\s+/ /g;
98 } elsif (ref($default) eq 'ARRAY') {
99 0 $default = '[' . join(', ', map "'$_'", @$default) . ']';
100 } elsif (ref($default) eq 'Regexp') {
101 0 $type = 'regex';
102 0 $default =~ s,^\(\?(\w*)-\w*:(.*)\),/$2/$1,;
103 0 $default = "qr$default";
104 }
105 0 ▶ unless ($type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 0 0
106 # Guess from the validation spec
107 $type = ($spec->{type} & ARRAYREF ? 'list' :
108 $spec->{type} & SCALAR ? 'string' :
109 $spec->{type} & CODEREF ? 'code' :
110 0 ▶ $spec->{type} & HASHREF ? 'hash' :
Branch: 4 branches
BranchTrueFalseTotal
$spec->{'type'} & 2 ? : 0 0 0
$spec->{'type'} & 1 ? : 0 0 0
$spec->{'type'} & 8 ? : 0 0 0
$spec->{'type'} & 4 ? : 0 0 0
111 undef); # Oh well
112 }
113  
114 0 ▶ my $descr = $spec->{descr} || '(No description available)';
Condition0%0/2
$spec->{'descr'} || '(No description available)'
l!l
00
MC/DC0%0/1
$spec->{'descr'} || '(No description available)'
$spec->{'descr'}
Truth table0%0/2
A
A= $spec->{'descr'}
Aresult
11
00
115 $out{$class}{valid_params}{$name} = { type => $type,
116 pv_type => $spec->{type},
117 default => $default,
118 descr => $descr,
119 required => defined $default || $spec->{optional} ? 0 : 1,
120 0 ▶ public => exists $spec->{public} ? $spec->{public} : 1,
Branch: 2 branches
BranchTrueFalseTotal
defined $default || $spec->{'optional'} ? : 0 0 0
exists $spec->{'public'} ? : 0 0 0
Condition0%0/3
defined $default or $spec->{"optional"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $default or $spec->{"optional"}
defined $default $spec->{"optional"}
Truth table0%0/3
A or B
A= defined $defaultB= $spec->{"optional"}
ABresult
1X1
011
000
121 };
122 }
123  
124 0 $out{$class}{contained_objects} = {};
125 0 ▶ next unless exists $CONTAINED_OBJECTS{$class};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $CONTAINED_OBJECTS{$class} 0 0 0
126 0 my $contains = $CONTAINED_OBJECTS{$class};
127  
128 0 foreach my $name (sort keys %$contains)
129 {
130 $out{$class}{contained_objects}{$name}
131 0 = {map {$_, $contains->{$name}{$_}} qw(class delayed descr)};
132 }
133 }
134  
135 0 return %out;
136 }
137  
138 12 ▶sub dump_parameters {
Subroutine dump_parameters: called CC=13 SCAR=25.9
Pod documented
139 12 my $self = shift;
140 12 ▶ my $class = ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
1020
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
141
142 12 my %params;
143 12 foreach my $param (keys %{ $class->validation_spec }) {
144 30 ▶ next if $param eq 'container';
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "container" 12 18 30
145 18 my $spec = $class->validation_spec->{$param};
146 18 ▶ if (ref($self) and defined $self->{$param}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and defined $self->{$param}) { } 16 2 18
Condition66%2/3
ref $self and defined $self->{$param}
!ll&&!rl&&r
2016
MC/DC50%1/2
ref $self and defined $self->{$param}
ref $self defined $self->{$param}
Truth table66%2/3
A and B
A= ref $selfB= defined $self->{$param}
ABresult
0X0
100
111
147 16 $params{$param} = $self->{$param};
148 } else {
149 2 ▶ $params{$param} = $spec->{default} if exists $spec->{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{"default"} 2 0 2
150 }
151 }
152
153 12 foreach my $name (keys %{ $class->get_contained_object_spec }) {
154 6 ▶ next unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 6 6
155 my $contained = ($self->{container}{contained}{$name}{delayed} ?
156 $self->delayed_object_class($name) :
157 6 ▶ $params{$name});
Branch: 1 branch
BranchTrueFalseTotal
$self->{'container'}{'contained'}{$name}{'delayed'} ? : 2 4 6
158
159 6 ▶ my $subparams = UNIVERSAL::isa($contained, __PACKAGE__) ? $contained->dump_parameters : {};
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($contained, __PACKAGE__) ? : 6 0 6
160
161 6 ▶ my $more = $self->{container}{contained}{$name}{args} || {};
Condition100%2/2
$self->{'container'}{'contained'}{$name}{'args'} || {}
l!l
24
MC/DC100%1/1
$self->{'container'}{'contained'}{$name}{'args'} || {}
$self->{'container'}{'contained'}{$name}{'args'}
Truth table100%2/2
A
A= $self->{'container'}{'contained'}{$name}{'args'}
Aresult
11
00
162 6 $subparams->{$_} = $more->{$_} foreach keys %$more;
163
164 6 @params{ keys %$subparams } = values %$subparams;
165 6 delete $params{$name};
166 }
167 12 return \%params;
168 }
169  
170 23 ▶sub show_containers {
Subroutine show_containers: called CC=8 SCAR=21.0
Pod documented
171 23 my $self = shift;
172 23 my $name = shift;
173 23 my %args = (indent => '', @_);
174  
175 23 ▶ $name = defined($name) ? "$name -> " : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 18 5 23
176  
177 23 my $out = "$args{indent}$name$self";
178 23 ▶ $out .= " (delayed)" if $args{delayed};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"delayed"} 11 12 23
179 23 $out .= "\n";
180 23 ▶ return $out unless $self->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isa(__PACKAGE__) 0 23 23
181  
182 23 ▶ my $specs = ref($self) ? $self->{container}{contained} : $self->get_contained_object_spec;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 5 18 23
183  
184 23 ▶ while (my ($name, $spec) = each %$specs) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$specs) 18 23 41
185 18 ▶ my $class = $args{args}{"${name}_class"} || $spec->{class};
Condition66%2/3
$args{'args'}{"${name}_class"} || $spec->{'class'}
l!l&&r!l&&!r
4140
MC/DC0%0/2
$args{'args'}{"${name}_class"} || $spec->{'class'}
$args{'args'}{"${name}_class"} $spec->{'class'}
Truth table66%2/3
A || B
A= $args{'args'}{"${name}_class"}B= $spec->{'class'}
ABresult
1X1
011
000
186 18 $self->_load_module($class);
187  
188 18 ▶ if ($class->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->isa(__PACKAGE__)) { } 18 0 18
189 $out .= $class->show_containers($name,
190 indent => "$args{indent} ",
191 args => $spec->{args},
192 18 delayed => $spec->{delayed});
193 } else {
194 0 $out .= "$args{indent} $name -> $class\n";
195 }
196 }
197  
198 23 return $out;
199 }
200  
201 58 ▶sub _expire_caches {
Subroutine _expire_caches: called CC=1 SCAR=0.0
202 58 %VALID_CACHE = %CONTAINED_CACHE = ();
203 }
204  
205 38 ▶sub valid_params {
Subroutine valid_params: called CC=5 SCAR=16.1
Pod documented
206 38 my $class = shift;
207 38 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 33 5 38
208 33 $class->_expire_caches;
209 33 ▶ $VALID_PARAMS{$class} = @_ == 1 && !defined($_[0]) ? {} : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && !defined($_[0]) ? : 3 30 33
Condition66%2/3
@_ == 1 and not defined $_[0]
!ll&&!rl&&r
3003
MC/DC50%1/2
@_ == 1 and not defined $_[0]
@_ == 1 not defined $_[0]
Truth table66%2/3
A and not(B)
A= @_ == 1B= not defined $_[0]
ABresult
0X0
100
111
210 }
211 38 ▶ return $VALID_PARAMS{$class} ||= {};
Condition100%2/2
$VALID_PARAMS{$class} ||= {}
l!l
371
MC/DC100%1/1
$VALID_PARAMS{$class} ||= {}
$VALID_PARAMS{$class}
Truth table100%2/2
A
A= $VALID_PARAMS{$class}
Aresult
11
00
212 }
213  
214 sub contained_objects
215 24 ▶{
Subroutine contained_objects: called CC=3 SCAR=11.0
Pod documented
216 24 my $class = shift;
217 24 $class->_expire_caches;
218 24 $CONTAINED_OBJECTS{$class} = {};
219 24 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 24 24 48
220 24 my ($name, $spec) = (shift, shift);
221 24 ▶ $CONTAINED_OBJECTS{$class}{$name} = ref($spec) ? $spec : { class => $spec };
Branch: 1 branch
BranchTrueFalseTotal
ref $spec ? : 11 13 24
222 }
223 }
224  
225 9 ▶sub _decorator_AUTOLOAD {
Subroutine _decorator_AUTOLOAD: called CC=4 SCAR=13.9
226 9 my $self = shift;
227 2 ▶ use vars qw($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
228 9 my ($method) = $AUTOLOAD =~ /([^:]+)$/;
229 9 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 8 1 9
230 1 ▶ die qq{Can't locate object method "$method" via package $self} unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 1 1
231 1 ▶ my $subr = $self->{_decorates}->can($method)
Branch: 1 branch
BranchTrueFalseTotal
my $subr = $self->{"_decorates"}->can($method) or die qq[Can't locate object method "$method" via package ] . ref($self) 1 0 1
232 or die qq{Can't locate object method "$method" via package } . ref($self);
233 1 unshift @_, $self->{_decorates};
234 1 goto $subr;
235 }
236  
237 11 ▶sub _decorator_CAN {
Subroutine _decorator_CAN: called CC=4 SCAR=14.0
238 11 my ($self, $method) = @_;
239 11 ▶ return $self->SUPER::can($method) if $self->SUPER::can($method);
Branch: 1 branch
BranchTrueFalseTotal
if $self->SUPER::can($method) 9 2 11
240 2 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 2 0 2
241 2 ▶ return $self->{_decorates}->can($method) if $self->{_decorates};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_decorates"} 1 1 2
242 1 return;
243 } else {
244 0 return $DECORATEES{$self}->can($method);
245 }
246 }
247  
248 5 ▶sub decorates {
Subroutine decorates: called CC=2 SCAR=7.0
Pod documented
249 5 my ($class, $super) = @_;
250
251 2 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
252 5 ▶ $super ||= ${$class . '::ISA'}[0];
Condition33%1/3
$super ||= do { $class . '::ISA' }->[0]
l!l&&r!l&&!r
050
MC/DC0%0/2
$super ||= do { $class . '::ISA' }->[0]
$super do { $class . '::ISA' }->[0]
Truth table33%1/3
A ||= B
A= $superB= do { $class . '::ISA' }->[0]
ABresult
1X1
011
000
253
254 # Pass through unknown method invocations
255 5 *{$class . '::AUTOLOAD'} = \&_decorator_AUTOLOAD;
256 5 *{$class . '::can'} = \&_decorator_CAN;
257
258 5 $DECORATEES{$class} = $super;
259 5 $VALID_PARAMS{$class}{_decorates} = { isa => $super, optional => 1 };
260 }
261  
262 1 ▶sub container {
Subroutine container: called CC=2 SCAR=7.1
Pod documented
263 1 my $self = shift;
264 1 ▶ die "The ", ref($self), "->container() method requires installation of Scalar::Util" unless $HAVE_WEAKEN;
Branch: 1 branch
BranchTrueFalseTotal
unless $HAVE_WEAKEN 0 1 1
265 1 return $self->{container}{container};
266 }
267  
268 5 ▶sub call_method {
Subroutine call_method: called CC=2 SCAR=7.0
Pod undocumented
269 5 my ($self, $name, $method, @args) = @_;
270
271 5 ▶ my $class = $self->contained_class($name)
Branch: 1 branch
BranchTrueFalseTotal
my $class = $self->contained_class($name) or die "Unknown contained item '${name}'" 5 0 5
272 or die "Unknown contained item '$name'";
273  
274 5 $self->_load_module($class);
275 5 return $class->$method( %{ $self->{container}{contained}{$name}{args} }, @args );
276 }
277  
278 # Accepts a list of key-value pairs as parameters, representing all
279 # parameters taken by this object and its descendants. Returns a list
280 # of key-value pairs representing *only* this object's parameters.
281 sub create_contained_objects
282 54 ▶{
Subroutine create_contained_objects: called CC=16 SCAR=27.8
Pod undocumented
283 # Typically $self doesn't exist yet, $_[0] is a string classname
284 54 my $class = shift;
285  
286 54 my $c = $class->get_contained_object_spec;
287 54 ▶ return {@_, container => {}} unless %$c or $DECORATEES{$class};
Branch: 1 branch
BranchTrueFalseTotal
unless %$c or $DECORATEES{$class} 19 35 54
Condition100%3/3
%$c or $DECORATEES{$class}
l!l&&r!l&&!r
27819
MC/DC100%2/2
%$c or $DECORATEES{$class}
%$c $DECORATEES{$class}
Truth table100%3/3
A or B
A= %$cB= $DECORATEES{$class}
ABresult
1X1
011
000
288
289 35 my %args = @_;
290
291 35 ▶ if ($DECORATEES{$class}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DECORATEES{$class}) 8 27 35
292 # Fix format
293 $args{decorate_class} = [$args{decorate_class}]
294 8 ▶ if $args{decorate_class} and !ref($args{decorate_class});
Branch: 1 branch
BranchTrueFalseTotal
if $args{"decorate_class"} and not ref $args{"decorate_class"} 3 5 8
Condition66%2/3
$args{"decorate_class"} and not ref $args{"decorate_class"}
!ll&&!rl&&r
503
MC/DC50%1/2
$args{"decorate_class"} and not ref $args{"decorate_class"}
$args{"decorate_class"} not ref $args{"decorate_class"}
Truth table66%2/3
A and not(B)
A= $args{"decorate_class"}B= not ref $args{"decorate_class"}
ABresult
0X0
100
111
295
296 # Figure out which class to decorate
297 8 my $decorate;
298 8 ▶ if (my $c = $args{decorate_class}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $args{"decorate_class"}) 3 5 8
299 3 ▶ $decorate = @$c ? shift @$c : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$c ? : 3 0 3
300 3 ▶ delete $args{decorate_class} unless @$c;
Branch: 1 branch
BranchTrueFalseTotal
unless @$c 3 0 3
301 }
302 8 ▶ $c->{_decorates} = { class => $decorate } if $decorate;
Branch: 1 branch
BranchTrueFalseTotal
if $decorate 3 5 8
303 }
304  
305 # This one is special, don't pass to descendants
306 35 ▶ my $container_stuff = delete($args{container}) || {};
Condition100%2/2
delete $args{'container'} || {}
l!l
332
MC/DC100%1/1
delete $args{'container'} || {}
delete $args{'container'}
Truth table100%2/2
A
A= delete $args{'container'}
Aresult
11
00
307  
308 35 keys %$c; # Reset the iterator - why can't I do this in get_contained_object_spec??
309 35 my %contained_args;
310 my %to_create;
311
312 35 ▶ while (my ($name, $spec) = each %$c) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$c) 47 35 82
313 # Figure out exactly which class to make an object of
314 47 my ($contained_class, $c_args) = $class->_get_contained_args($name, \%args);
315 47 @contained_args{ keys %$c_args } = (); # Populate with keys
316 47 $to_create{$name} = { class => $contained_class,
317 args => $c_args };
318 }
319
320 35 ▶ while (my ($name, $spec) = each %$c) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$c) 46 34 80
321 # This delete() needs to be outside the previous loop, because
322 # multiple contained objects might need to see it
323 46 delete $args{"${name}_class"};
324  
325 46 ▶ if ($spec->{delayed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{'delayed'}) { } 20 26 46
326 20 $container_stuff->{contained}{$name} = $to_create{$name};
327 20 $container_stuff->{contained}{$name}{delayed} = 1;
328 } else {
329 26 ▶ $args{$name} ||= $to_create{$name}{class}->new(%{$to_create{$name}{args}});
Condition66%2/3
$args{$name} ||= $to_create{$name}{"class"}->new(%{$to_create{$name}{"args"};})
l!l&&r!l&&!r
1250
MC/DC0%0/2
$args{$name} ||= $to_create{$name}{"class"}->new(%{$to_create{$name}{"args"};})
$args{$name} $to_create{$name}{"class"}->new(%{$to_create{$name}{"args"};})
Truth table66%2/3
A ||= B
A= $args{$name}B= $to_create{$name}{"class"}->new(%{$to_create{$name}{"args"};})
ABresult
1X1
011
000
330 25 $container_stuff->{contained}{$name}{class} = ref $args{$name};
331 }
332 }
333  
334 # Delete things that we're not going to use - things that are in
335 # our contained object specs but not in ours.
336 34 my $my_spec = $class->validation_spec;
337 34 delete @args{ grep {!exists $my_spec->{$_}} keys %contained_args };
338 34 ▶ delete $c->{_decorates} if $DECORATEES{$class};
Branch: 1 branch
BranchTrueFalseTotal
if $DECORATEES{$class} 8 26 34
339  
340 34 $args{container} = $container_stuff;
341 34 return \%args;
342 }
343  
344 sub create_delayed_object
345 5 ▶{
Subroutine create_delayed_object: called CC=3 SCAR=11.2
Pod documented
346 5 my ($self, $name) = (shift, shift);
347 5 ▶ croak "Unknown delayed item '$name'" unless $self->{container}{contained}{$name}{delayed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"container"}{"contained"}{$name}{"delayed"} 0 5 5
348  
349 5 ▶ if ($HAVE_WEAKEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HAVE_WEAKEN) 5 0 5
350 5 push @_, container => {container => $self};
351 5 weaken $_[-1]->{container};
352 }
353 5 return $self->call_method($name, 'new', @_);
354 }
355  
356 sub delayed_object_class
357 4 ▶{
Subroutine delayed_object_class: called CC=2 SCAR=7.0
Pod documented
358 4 my $self = shift;
359 4 my $name = shift;
360 croak "Unknown delayed item '$name'"
361 4 ▶ unless $self->{container}{contained}{$name}{delayed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"container"}{"contained"}{$name}{"delayed"} 0 4 4
362  
363 4 return $self->{container}{contained}{$name}{class};
364 }
365  
366 sub contained_class
367 9 ▶{
Subroutine contained_class: called CC=2 SCAR=7.1
Pod undocumented
368 9 my ($self, $name) = @_;
369 croak "Unknown contained item '$name'"
370 9 ▶ unless my $spec = $self->{container}{contained}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless my $spec = $self->{"container"}{"contained"}{$name} 0 9 9
371 9 return $spec->{class};
372 }
373  
374 sub delayed_object_params
375 0 ▶{
Subroutine delayed_object_params: not called CC=4 SCAR=30.0
Pod documented
376 0 my ($self, $name) = (shift, shift);
377 croak "Unknown delayed object '$name'"
378 0 ▶ unless $self->{container}{contained}{$name}{delayed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"container"}{"contained"}{$name}{"delayed"} 0 0 0
379  
380 0 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 0 0 0
381 0 return $self->{container}{contained}{$name}{args}{$_[0]};
382 }
383  
384 0 my %args = @_;
385  
386 0 ▶ if (keys %args)
Branch: 1 branch
BranchTrueFalseTotal
if (keys %args) 0 0 0
387 {
388 0 @{ $self->{container}{contained}{$name}{args} }{ keys %args } = values %args;
389 }
390  
391 0 return %{ $self->{container}{contained}{$name}{args} };
392 }
393  
394 # Everything the specified contained object will accept, including
395 # parameters it will pass on to its own contained objects.
396 sub _get_contained_args
397 47 ▶{
Subroutine _get_contained_args: called CC=7 SCAR=19.6
398 47 my ($class, $name, $args) = @_;
399
400 47 ▶ my $spec = $class->get_contained_object_spec->{$name}
Branch: 1 branch
BranchTrueFalseTotal
my $spec = $class->get_contained_object_spec->{$name} or croak("Unknown contained object '${name}'") 47 0 47
401 or croak "Unknown contained object '$name'";
402  
403 47 ▶ my $contained_class = $args->{"${name}_class"} || $spec->{class};
Condition66%2/3
$$args{"${name}_class"} || $spec->{'class'}
l!l&&r!l&&!r
9380
MC/DC0%0/2
$$args{"${name}_class"} || $spec->{'class'}
$$args{"${name}_class"} $spec->{'class'}
Truth table66%2/3
A || B
A= $$args{"${name}_class"}B= $spec->{'class'}
ABresult
1X1
011
000
404 47 ▶ croak "Invalid class name '$contained_class'"
Branch: 1 branch
BranchTrueFalseTotal
unless $contained_class =~ /^[\w:]+$/ 0 47 47
405 unless $contained_class =~ /^[\w:]+$/;
406  
407 47 $class->_load_module($contained_class);
408 47 ▶ return ($contained_class, {}) unless $contained_class->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $contained_class->isa(__PACKAGE__) 1 46 47
409  
410 46 my $allowed = $contained_class->allowed_params($args);
411  
412 46 my %contained_args;
413 46 foreach (keys %$allowed) {
414 111 ▶ $contained_args{$_} = $args->{$_} if exists $args->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{$_} 20 91 111
415 }
416 46 return ($contained_class, \%contained_args);
417 }
418  
419 95 ▶sub _load_module {
Subroutine _load_module: called CC=3 SCAR=13.0
420 95 my ($self, $module) = @_;
421
422 95 ▶ unless ( eval { $module->can('new') } )
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $module->can("new") } }) 0 95 95
423 {
424 2 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
425 0 eval "use $module"; ## no critic
426 0 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
427 }
428 }
429  
430 sub allowed_params
431 75 ▶{
Subroutine allowed_params: called CC=8 SCAR=20.9
Pod documented
432 75 my $class = shift;
433 75 ▶ my $args = ref($_[0]) ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 71 4 75
434
435 # Strategy: the allowed_params of this class consists of the
436 # validation_spec of this class, merged with the allowed_params of
437 # all contained classes. The specific contained classes may be
438 # affected by arguments passed in, like 'interp' or
439 # 'interp_class'. A parameter like 'interp' doesn't add anything
440 # to our allowed_params (because it's already created) but
441 # 'interp_class' does.
442  
443 75 my $c = $class->get_contained_object_spec;
444 75 my %p = %{ $class->validation_spec };
445
446 75 foreach my $name (keys %$c)
447 {
448 # Can accept a 'foo' parameter - should already be in the validation_spec.
449 # Also, its creation parameters should already have been extracted from $args,
450 # so don't extract any parameters.
451 25 ▶ next if exists $args->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{$name} 0 25 25
452
453 # Figure out what class to use for this contained item
454 25 my $contained_class;
455 25 ▶ if ( exists $args->{"${name}_class"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$args{"${name}_class"}) { } 7 18 25
456 7 $contained_class = $args->{"${name}_class"};
457 7 $p{"${name}_class"} = { type => SCALAR }; # Add to spec
458 } else {
459 18 $contained_class = $c->{$name}{class};
460 }
461
462 # We have to make sure it is loaded before we try calling allowed_params()
463 25 $class->_load_module($contained_class);
464 25 ▶ next unless $contained_class->can('allowed_params');
Branch: 1 branch
BranchTrueFalseTotal
unless $contained_class->can("allowed_params") 0 25 25
465
466 25 my $subparams = $contained_class->allowed_params($args);
467
468 25 foreach (keys %$subparams) {
469 42 ▶ $p{$_} ||= $subparams->{$_};
Condition66%2/3
$p{$_} ||= $subparams->{$_}
l!l&&r!l&&!r
25170
MC/DC0%0/2
$p{$_} ||= $subparams->{$_}
$p{$_} $subparams->{$_}
Truth table66%2/3
A ||= B
A= $p{$_}B= $subparams->{$_}
ABresult
1X1
011
000
470 }
471 }
472  
473 75 return \%p;
474 }
475  
476 528 ▶sub _iterate_ISA {
Subroutine _iterate_ISA: called CC=6 SCAR=17.9
477 528 my ($class, $look_in, $cache_in, $add) = @_;
478  
479 528 ▶ return $cache_in->{$class} if $cache_in->{$class};
Branch: 1 branch
BranchTrueFalseTotal
if $cache_in->{$class} 428 100 528
480  
481 100 my %out;
482
483 2 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
484 100 foreach my $superclass (@{ "${class}::ISA" }) {
485 79 ▶ next unless $superclass->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $superclass->isa(__PACKAGE__) 2 77 79
486 77 my $superparams = $superclass->_iterate_ISA($look_in, $cache_in, $add);
487 77 @out{keys %$superparams} = values %$superparams;
488 }
489 100 ▶ if (my $x = $look_in->{$class}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $x = $look_in->{$class}) 52 48 100
490 52 @out{keys %$x} = values %$x;
491 }
492
493 100 ▶ @out{keys %$add} = values %$add if $add;
Branch: 1 branch
BranchTrueFalseTotal
if $add 49 51 100
494
495 100 return $cache_in->{$class} = \%out;
496 }
497  
498 258 ▶sub get_contained_object_spec {
Subroutine get_contained_object_spec: called CC=2 SCAR=7.2
Pod undocumented
499 258 ▶ return (ref($_[0]) || $_[0])->_iterate_ISA(\%CONTAINED_OBJECTS, \%CONTAINED_CACHE);
Condition66%2/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
512070
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
500 }
501  
502 193 ▶sub validation_spec {
Subroutine validation_spec: called CC=2 SCAR=9.2
Pod documented
503 193 ▶ return (ref($_[0]) || $_[0])->_iterate_ISA(\%VALID_PARAMS, \%VALID_CACHE, { container => {type => HASHREF} });
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
01930
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
504 }
505  
506 2 1;
507  
508 __END__
↑ Summary