Bolts/Util.pm

filter: Statementstmt 90.7% 98 / 108 Branchbran 68.5% 37 / 54 Conditioncond 52.0% 13 / 25 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 76.7% 168 / 219 SCAR 40.6
CC
42
Cov
79%
artifact
33.9
_injector
8.8
meta_locator_for
7.6
CRAP 58.0
↑ Summary Bolts/Role/RootLocator.pm »
1 package Bolts::Util;
2 10 $Bolts::Util::VERSION = '0.143170';
3 # ABSTRACT: Utilities helpful for use with Bolts
4  
5 10 ▶use Moose ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶use Bolts::Locator;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Moose::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Hash::Util::FieldHash 'fieldhash';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 ▶use Bolts::Meta::Initializer;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 10 Moose::Exporter->setup_import_methods(
16 as_is => [ qw( bolts_init locator_for meta_locator_for ) ],
17 );
18  
19 10 fieldhash my %locator;
20 10 fieldhash my %meta_locator;
21  
22  
23 sub _injector {
24 38 ▶ my ($meta, $where, $type, $key, $params) = @_;
Subroutine _injector: called CC=7 SCAR=21.8
25  
26 38 my %params;
27  
28 38 ▶ if ($params->$_can('does') and $params->$_does('Bolts::Blueprint')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->$Bolts::Util::_can('does') and $params->$Bolts::Util::_does('Bolts::Blueprint')) { } 0 38 38
Condition33%1/3
$params->$Bolts::Util::_can("does") and $params->$Bolts::Util::_does("Bolts::Blueprint")
!ll&&!rl&&r
3800
MC/DC0%0/2
$params->$Bolts::Util::_can("does") and $params->$Bolts::Util::_does("Bolts::Blueprint")
$params->$Bolts::Util::_can("does") $params->$Bolts::Util::_does("Bolts::Blueprint")
Truth table33%1/3
A and B
A= $params->$Bolts::Util::_can("does")B= $params->$Bolts::Util::_does("Bolts::Blueprint")
ABresult
0X0
100
111
29 0 %params = { blueprint => $params };
30 }
31 else {
32 38 %params = %$params;
33 }
34  
35 Carp::croak("invalid blueprint in $where $key")
36 unless $params{blueprint}->$_can('does')
37 38 ▶ and $params{blueprint}->$_does('Bolts::Blueprint::Role::Injector');
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"blueprint"}->$Bolts::Util::_can("does") and $params{"blueprint"}->$Bolts::Util::_does("Bolts::Blueprint::Role::Injector") 0 38 38
Condition33%1/3
$params{"blueprint"}->$Bolts::Util::_can("does") and $params{"blueprint"}->$Bolts::Util::_does("Bolts::Blueprint::Role::Injector")
!ll&&!rl&&r
0038
MC/DC0%0/2
$params{"blueprint"}->$Bolts::Util::_can("does") and $params{"blueprint"}->$Bolts::Util::_does("Bolts::Blueprint::Role::Injector")
$params{"blueprint"}->$Bolts::Util::_can("does") $params{"blueprint"}->$Bolts::Util::_does("Bolts::Blueprint::Role::Injector")
Truth table33%1/3
A and B
A= $params{"blueprint"}->$Bolts::Util::_can("does")B= $params{"blueprint"}->$Bolts::Util::_does("Bolts::Blueprint::Role::Injector")
ABresult
0X0
100
111
38  
39 $params{isa} = Moose::Util::TypeConstraints::find_or_create_isa_type_constraint($params{isa})
40 38 ▶ if defined $params{isa};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"isa"} 8 30 38
41 $params{does} = Moose::Util::TypeConstraints::find_or_create_does_type_constraint($params{does})
42 38 ▶ if defined $params{does};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params{"does"} 0 38 38
43  
44 38 $params{key} = $key;
45  
46 38 return $meta->acquire('injector', $type, \%params);
47 }
48  
49 # TODO This sugar requires special knowledge of the built-in blueprint
50 # types. It would be slick if this was not required. On the other hand, that
51 # sounds like very deep magic and that might just be taking the magic too far.
52 sub artifact {
53 39 ▶ my $meta = shift;
Subroutine artifact: called CC=30 SCAR=35.2
Pod documented
54 39 my $name = shift;
55  
56 # No arguments means it's acquired with given parameters
57 39 my $blueprint_name;
58 my %params;
59 39 ▶ if (@_ == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 0) { } 2 37 39
elsif (@_ == 1) { } 3 34 37
60 2 $blueprint_name = 'acquired';
61 2 $params{path} = [ "__auto_$name" ];
62 2 $meta->add_attribute("__auto_$name" =>
63 is => 'ro',
64 init_arg => $name,
65 );
66 }
67  
68 # One argument means it's a literal
69 elsif (@_ == 1) {
70 3 $blueprint_name = 'literal';
71 3 $params{value} = $_[0];
72 }
73  
74 # Otherwise, we gotta figure out what it is...
75 else {
76 34 %params = @_;
77  
78 # Is the service class named?
79 34 ▶ if (defined $params{blueprint}) {
Branch: 5 branches
BranchTrueFalseTotal
if (defined $params{'blueprint'}) { } 0 34 34
elsif (defined $params{'path'} and $params{'path'}) { } 2 32 34
elsif (exists $params{'value'}) { } 0 32 32
elsif (defined $params{'class'}) { } 12 20 32
elsif (defined $params{'builder'}) { } 20 0 20
80 0 $blueprint_name = delete $params{blueprint};
81 }
82  
83 # Is it an acquired?
84 ▶ elsif (defined $params{path} && $params{path}) {
Condition66%2/3
defined $params{"path"} and $params{"path"}
!ll&&!rl&&r
3202
MC/DC50%1/2
defined $params{"path"} and $params{"path"}
defined $params{"path"} $params{"path"}
Truth table66%2/3
A and B
A= defined $params{"path"}B= $params{"path"}
ABresult
0X0
100
111
85 2 $blueprint_name = 'acquired';
86  
87 2 ▶ $params{path} = [ $params{path} ] unless ref $params{path} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params{"path"} eq "ARRAY" 0 2 2
88  
89 2 my @path = ('__top', @{ $params{path} });
90  
91 2 $params{path} = \@path;
92 }
93  
94 # Is it a literal?
95 elsif (exists $params{value}) {
96 0 $blueprint_name = 'literal';
97 }
98  
99 # Is it a factory blueprint?
100 elsif (defined $params{class}) {
101 12 $blueprint_name = 'factory';
102 }
103  
104 # Is it a builder blueprint?
105 elsif (defined $params{builder}) {
106 20 $blueprint_name = 'built';
107 }
108  
109 else {
110 0 Carp::croak("unable to determine what kind of service $name is in ", $meta->name);
111 }
112 }
113  
114 39 my @injectors;
115 39 ▶ if (defined $params{parameters}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"parameters"}) 6 33 39
116 6 my $parameters = delete $params{parameters};
117  
118 6 ▶ if ($parameters->$_does('Bolts::Blueprint')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($parameters->$Bolts::Util::_does('Bolts::Blueprint')) { } 0 6 6
elsif (ref $parameters eq 'HASH') { } 5 1 6
elsif (ref $parameters eq 'ARRAY') { } 1 0 1
119 0 push @injectors, _injector(
120 $meta, 'parameters', 'parameter_position',
121 '0', { blueprint => $parameters },
122 );
123 }
124 elsif (ref $parameters eq 'HASH') {
125 5 for my $key (keys %$parameters) {
126 push @injectors, _injector(
127 $meta, 'parameters', 'parameter_name',
128 12 $key, $parameters->{$key},
129 );
130 }
131 }
132 elsif (ref $parameters eq 'ARRAY') {
133 1 my $key = 0;
134 1 for my $params (@$parameters) {
135 3 push @injectors, _injector(
136 $meta, 'parameters', 'parameter_position',
137 $key++, $params,
138 );
139 }
140 }
141 else {
142 0 Carp::croak("parameters must be a blueprint, an array of blueprints, or a hash with blueprint values");
143 }
144 }
145  
146 39 ▶ if (defined $params{setters}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"setters"}) 1 38 39
147 1 my $setters = delete $params{setters};
148  
149 1 for my $key (keys %$setters) {
150 push @injectors, _injector(
151 $meta, 'setters', 'setter',
152 1 $key, $setters->{$key},
153 );
154 }
155 }
156  
157 39 ▶ if (defined $params{indexes}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"indexes"}) 1 38 39
158 1 my $indexes = delete $params{indexes};
159  
160 1 ▶ while (my ($index, $def) = splice @$indexes, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@$indexes, 0, 2)) 4 1 5
161 4 ▶ if (!Scalar::Util::blessed($def) && Scalar::Util::reftype($def) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($def) and builtin::reftype($def) eq "HASH") 4 0 4
Condition33%1/3
not builtin::blessed($def) and builtin::reftype($def) eq "HASH"
!ll&&!rl&&r
004
MC/DC0%0/2
not builtin::blessed($def) and builtin::reftype($def) eq "HASH"
not builtin::blessed($def) builtin::reftype($def) eq "HASH"
Truth table33%1/3
not(A) and B
A= not builtin::blessed($def)B= builtin::reftype($def) eq "HASH"
ABresult
0X0
100
111
162 4 ▶ $def->{position} //= $index;
Condition33%1/3
$def->{"position"} //= $index
l!l&&r!l&&!r
040
MC/DC0%0/2
$def->{"position"} //= $index
$def->{"position"} $index
Truth table33%1/3
A //= B
A= $def->{"position"}B= $index
ABresult
1X1
011
000
163 }
164  
165 4 push @injectors, _injector(
166 $meta, 'indexes', 'store_array',
167 $index, $def,
168 );
169 }
170 }
171  
172 39 ▶ if (defined $params{push}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"push"}) 9 30 39
173 9 my $push = delete $params{push};
174  
175 9 my $i = 0;
176 9 for my $def (@$push) {
177 14 ▶ my $key = $def->{key} // $i;
Condition66%2/3
$def->{"key"} // $i
l!l&&r!l&&!r
059
MC/DC50%1/2
$def->{"key"} // $i
$def->{"key"} $i
Truth table66%2/3
A // B
A= $def->{"key"}B= $i
ABresult
1X1
011
000
178  
179 14 push @injectors, _injector(
180 $meta, 'push', 'store_array',
181 $key, $def,
182 );
183  
184 14 $i++;
185 }
186 }
187  
188 39 ▶ if (defined $params{keys}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"keys"}) 1 38 39
189 1 my $keys = delete $params{keys};
190  
191 1 for my $key (keys %$keys) {
192 push @injectors, _injector(
193 $meta, 'keys', 'store_hash',
194 4 $key, $keys->{$key},
195 );
196 }
197 }
198  
199 # TODO Remember the service for introspection
200  
201 39 ▶ my $scope_name = delete $params{scope} // '_';
Condition100%2/2
delete $params{"scope"} // "_"
l!l
138
MC/DC100%1/1
delete $params{"scope"} // "_"
delete $params{"scope"}
Truth table100%2/2
A
A= delete $params{"scope"}
Aresult
11
00
202 39 ▶ my $infer = delete $params{infer} // 'none';
Condition100%2/2
delete $params{"infer"} // "none"
l!l
336
MC/DC100%1/1
delete $params{"infer"} // "none"
delete $params{"infer"}
Truth table100%2/2
A
A= delete $params{"infer"}
Aresult
11
00
203  
204 39 my $scope = $meta->acquire('scope', $scope_name);
205  
206 39 my $blueprint = $meta->acquire('blueprint', $blueprint_name, \%params);
207  
208 39 return Bolts::Artifact->new(
209 meta_locator => $meta,
210 name => $name,
211 blueprint => $blueprint,
212 scope => $scope,
213 infer => $infer,
214 injectors => \@injectors,
215 );
216 }
217  
218  
219 sub locator_for {
220 232 ▶ my ($bag) = @_;
Subroutine locator_for: called CC=3 SCAR=15.1
Pod documented
221  
222 232 ▶ if ($bag->$_does('Bolts::Role::Locator')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bag->$Bolts::Util::_does('Bolts::Role::Locator')) { } 232 0 232
elsif (defined $locator{$bag}) { } 0 0 0
223 232 return $bag;
224 }
225 elsif (defined $locator{ $bag }) {
226 0 return $locator{ $bag };
227 }
228 else {
229 0 return $locator{ $bag } = Bolts::Locator->new($bag);
230 }
231 }
232  
233  
234 sub meta_locator_for {
235 57 ▶ my ($bag) = @_;
Subroutine meta_locator_for: called CC=5 SCAR=20.3
Pod documented
236  
237 57 my $meta = Moose::Util::find_meta($bag);
238 57 ▶ if (defined $meta) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $meta) { } 57 0 57
elsif (defined $meta_locator{$bag}) { } 0 0 0
239 57 my $meta_meta = Moose::Util::find_meta($meta);
240 57 ▶ if ($meta_meta->$_can('does_role') && $meta_meta->does_role('Bolts::Meta::Class::Trait::Locator')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta_meta->$Bolts::Util::_can("does_role") and $meta_meta->does_role("Bolts::Meta::Class::Trait::Locator")) 57 0 57
Condition33%1/3
$meta_meta->$Bolts::Util::_can("does_role") and $meta_meta->does_role("Bolts::Meta::Class::Trait::Locator")
!ll&&!rl&&r
0057
MC/DC0%0/2
$meta_meta->$Bolts::Util::_can("does_role") and $meta_meta->does_role("Bolts::Meta::Class::Trait::Locator")
$meta_meta->$Bolts::Util::_can("does_role") $meta_meta->does_role("Bolts::Meta::Class::Trait::Locator")
Truth table33%1/3
A and B
A= $meta_meta->$Bolts::Util::_can("does_role")B= $meta_meta->does_role("Bolts::Meta::Class::Trait::Locator")
ABresult
0X0
100
111
241 57 return $meta->locator;
242 }
243 }
244  
245 elsif (defined $meta_locator{ $bag }) {
246 0 return $meta_locator{ $bag };
247 }
248  
249 0 return $meta_locator{ $bag } = $Bolts::GLOBAL_FALLBACK_META_LOCATOR->new;
250 }
251  
252  
253 2 ▶sub bolts_init { Bolts::Meta::Initializer->new(@_) }
Subroutine bolts_init: called CC=1 SCAR=0.0
Pod documented
254  
255 10 1;
256  
257 __END__
258  
259 =pod
260  
261 =encoding UTF-8
262  
263 =head1 NAME
264  
265 Bolts::Util - Utilities helpful for use with Bolts
266  
267 =head1 VERSION
268  
269 version 0.143170
270  
271 =head1 SYNOPSIS
272  
273 use Bolts::Util qw( bolts_init locator_for meta_locator_for );
274  
275 my $loc = locator_for($bag);
276 my $thing = $loc->acquire('path', 'to', 'thing');
277  
278 my $metaloc = meta_locator_for($bag);
279 my $blueprint = $metaloc->acquire('blueprint', 'factory', {
280 class => 'MyApp::Thing',
281 method => 'fetch',
282 });
283  
284 # See Bolts::Role::Initializer for a better synopsis
285 my $obj = MyApp::Thing->new(
286 foo => bolts_init('path', 'to', 'foo'),
287 );
288  
289 =head1 DESCRIPTION
290  
291 This provides some helpful utility methods for use with Bolts.
292  
293 =head1 EXPORTED FUNCTIONS
294  
295 =head2 artifact
296  
297 my $artifact = artifact($bag, $name, %definition);
298  
299 # For example:
300 my $artifact = artifact($bag, thing => ( class => 'MyApp::Thing' ) );
301  
302 This contains the internal implementation for building L<Bolt::Artifact> objects used by the sugar methods in L<Bolts> and L<Bolts::Role>. See the documentation L<there|Bolts/artifact> for more details on how to call it.
303  
304 The C<$bag> must be the metaclass or reference to which the artifact is being attached. The C<$name> is the name to give the artifact and teh C<%definition> is the remaineder of the definition.
305  
306 =head2 locator_for
307  
308 my $loc = locator_for($bag);
309  
310 Given a bag, it will return a L<Bolts::Role::Locator> for acquiring artifacts from it. If the bag provides it's own locator, the bag will be returned. If it doesn't (e.g., if it's a hash or an array or just some other object that doesn't have a locator built-in), then a new locator will be built to locate within the bag and returned on the first call. Subsequent calls using the same reference will return the same locator object.
311  
312 =head2 meta_locator_for
313  
314 my $metaloc = meta_locator_for($bag);
315  
316 Attempts to find the meta locator for the bag. It returns a L<Bolts::Role::Locator> that is able to return artifacts used to manage a collection of bolts bags and artifacts. If the bag itself does not have such a locator associated with it, one is constructed using the L<Bolts/$Bolts::GLOBAL_FALLBACK_META_LOCATOR> class, which is L<Bolts::Meta::Locator> by default. After the first call, the object created the first time for each reference will be reused.
317  
318 =head2 bolts_init
319  
320 my $init = bolts_init(@path, \%params);
321  
322 This is shorthand for:
323  
324 my $init = Bolts::Meta::Initializer->new(@path, \%params);
325  
326 This returns an initializer object that may be used with L<Bolts::Role::Initializer> to automatically initialize attributes from a built-in locator.
327  
328 =head1 AUTHOR
329  
330 Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
331  
332 =head1 COPYRIGHT AND LICENSE
333  
334 This software is copyright (c) 2014 by Qubling Software LLC.
335  
336 This is free software; you can redistribute it and/or modify it under
337 the same terms as the Perl 5 programming language system itself.
338  
339 =cut
↑ Summary Bolts/Role/RootLocator.pm »