Board/Container.pm

filter: Statementstmt 97.3% 72 / 74 Branchbran 63.3% 19 / 30 Conditioncond 46.6% 7 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 17 / 17 Podpod 100% 6 / 6 totaltotal 80.1% 121 / 151 SCAR 33.5
CC
25
Cov
82%
resolve
11.2
add_type_mapping_for
4.6
add_sub_container
4.2
CRAP 28.4
« Board.pm ↑ Summary Board/Service/Inferred.pm »
1 package Bread::Board::Container;
2 61 our $AUTHORITY = 'cpan:STEVAN';
3 # ABSTRACT: A container for services and other containers
4 61 $Bread::Board::Container::VERSION = '0.35';
5 61 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 61 ▶use Moose::Util::TypeConstraints 'find_type_constraint';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 61 ▶use MooseX::Params::Validate 0.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 61 ▶use Bread::Board::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 61 with 'Bread::Board::Traversable';
12  
13 61 has 'name' => (
14 is => 'rw',
15 isa => 'Str',
16 required => 1
17 );
18  
19 has 'services' => (
20 traits => [ 'Hash', 'Clone' ],
21 is => 'rw',
22 isa => 'Bread::Board::Container::ServiceList',
23 coerce => 1,
24 lazy => 1,
25 96 ▶ default => sub{ +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 trigger => sub {
27 51 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
28 51 $_->parent($self) foreach values %{$self->services};
29 },
30 61 handles => {
31 'get_service' => 'get',
32 'has_service' => 'exists',
33 'get_service_list' => 'keys',
34 'has_services' => 'count',
35 }
36 );
37  
38 has 'sub_containers' => (
39 traits => [ 'Hash', 'Clone' ],
40 is => 'rw',
41 isa => 'Bread::Board::Container::SubContainerList',
42 coerce => 1,
43 lazy => 1,
44 105 ▶ default => sub{ +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 trigger => sub {
46 23 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
47 23 $_->parent($self) foreach values %{$self->sub_containers};
48 },
49 61 handles => {
50 'get_sub_container' => 'get',
51 'has_sub_container' => 'exists',
52 'get_sub_container_list' => 'keys',
53 'has_sub_containers' => 'count',
54 }
55 );
56  
57 has 'type_mappings' => (
58 traits => [ 'Hash' ],
59 is => 'rw',
60 isa => 'Bread::Board::Container::ServiceList',
61 lazy => 1,
62 19 ▶ default => sub{ +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
63 61 handles => {
64 '_get_type_mapping_for' => 'get',
65 '_has_type_mapping_for' => 'exists',
66 '_mapped_types' => 'keys',
67 }
68 );
69  
70 sub get_type_mapping_for {
71 49 ▶ my $self = shift;
Subroutine get_type_mapping_for: called CC=4 SCAR=14.2
Pod documented
72 49 my ($type) = @_;
73  
74 49 ▶ return $self->_get_type_mapping_for($type)
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_type_mapping_for($type) 48 1 49
75 if $self->_has_type_mapping_for($type);
76  
77 1 for my $possible ($self->_mapped_types) {
78 1 ▶ return $self->_get_type_mapping_for($possible)
Branch: 1 branch
BranchTrueFalseTotal
if $possible->isa($type) 1 0 1
79 if $possible->isa($type);
80 }
81  
82 0 return;
83 }
84  
85 sub has_type_mapping_for {
86 66 ▶ my $self = shift;
Subroutine has_type_mapping_for: called CC=4 SCAR=13.9
Pod documented
87 66 my ($type) = @_;
88  
89 66 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_type_mapping_for($type) 45 21 66
90 if $self->_has_type_mapping_for($type);
91  
92 21 for my $possible ($self->_mapped_types) {
93 26 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $possible->isa($type) 1 25 26
94 if $possible->isa($type);
95 }
96  
97 20 return;
98 }
99  
100 sub add_service {
101 227 ▶ my ($self, $service) = @_;
Subroutine add_service: called CC=3 SCAR=12.0
Pod documented
102 227 ▶ (blessed $service && $service->does('Bread::Board::Service'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($service) and $service->does("Bread::Board::Service") or confess("You must pass in a Bread::Board::Service instance, not $service") 0 227 227
Condition33%1/3
builtin::blessed($service) and $service->does("Bread::Board::Service")
!ll&&!rl&&r
00227
MC/DC0%0/2
builtin::blessed($service) and $service->does("Bread::Board::Service")
builtin::blessed($service) $service->does("Bread::Board::Service")
Truth table33%1/3
A and B
A= builtin::blessed($service)B= $service->does("Bread::Board::Service")
ABresult
0X0
100
111
103 || confess "You must pass in a Bread::Board::Service instance, not $service";
104 227 $service->parent($self);
105 227 $self->services->{$service->name} = $service;
106 }
107  
108 sub add_sub_container {
109 33 ▶ my ($self, $container) = @_;
Subroutine add_sub_container: called CC=4 SCAR=14.5
Pod documented
110 (
111 33 ▶ blessed $container &&
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($container) and $container->isa("Bread::Board::Container") || $container->isa("Bread::Board::Container::Parameterized") or confess("You must pass in a Bread::Board::Container instance, not $container") 0 33 33
Condition66%4/6
builtin::blessed($container) and $container->isa("Bread::Board::Container") || $container->isa("Bread::Board::Container::Parameterized")
!ll&&!rl&&r
0285
$container->isa("Bread::Board::Container") or $container->isa("Bread::Board::Container::Parameterized")
l!l&&r!l&&!r
2940
MC/DC0%0/3
builtin::blessed($container) and $container->isa("Bread::Board::Container") || $container->isa("Bread::Board::Container::Parameterized")
builtin::blessed($container) $container->isa("Bread::Board::Container") $container->isa("Bread::Board::Container::Parameterized")
Truth table25%1/4
A and B or C
A= builtin::blessed($container)B= $container->isa("Bread::Board::Container")C= $container->isa("Bread::Board::Container::Parameterized")
ABCresult
0XX0
1000
11X1
1011
112 (
113 $container->isa('Bread::Board::Container')
114 ||
115 $container->isa('Bread::Board::Container::Parameterized')
116 )
117 ) || confess "You must pass in a Bread::Board::Container instance, not $container";
118 33 $container->parent($self);
119 33 $self->sub_containers->{$container->name} = $container;
120 }
121  
122 sub add_type_mapping_for {
123 29 ▶ my ($self, $type, $service) = @_;
Subroutine add_type_mapping_for: called CC=4 SCAR=15.2
Pod documented
124  
125 29 my $type_constraint = find_type_constraint( $type );
126  
127 29 ▶ (defined $type_constraint)
Branch: 1 branch
BranchTrueFalseTotal
defined $type_constraint or confess("You must pass a valid Moose type, and it must exist already") 0 29 29
128 || confess "You must pass a valid Moose type, and it must exist already";
129  
130 29 ▶ (blessed $service && $service->does('Bread::Board::Service'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($service) and $service->does("Bread::Board::Service") or confess("You must pass in a Bread::Board::Service instance, not $service") 0 29 29
Condition33%1/3
builtin::blessed($service) and $service->does("Bread::Board::Service")
!ll&&!rl&&r
0029
MC/DC0%0/2
builtin::blessed($service) and $service->does("Bread::Board::Service")
builtin::blessed($service) $service->does("Bread::Board::Service")
Truth table33%1/3
A and B
A= builtin::blessed($service)B= $service->does("Bread::Board::Service")
ABresult
0X0
100
111
131 || confess "You must pass in a Bread::Board::Service instance, not $service";
132  
133 29 $self->type_mappings->{ $type_constraint->name } = $service;
134 }
135  
136 sub resolve {
137 135 ▶ my ($self, %params) = validated_hash(\@_,
Subroutine resolve: called CC=9 SCAR=24.1
Pod documented
138 service => { isa => 'Str', optional => 1 },
139 type => { isa => 'Str', optional => 1 },
140 parameters => { isa => 'HashRef', optional => 1 },
141 );
142  
143 my %parameters = exists $params{'parameters'}
144 135 ▶ ? %{ $params{'parameters'} }
Branch: 1 branch
BranchTrueFalseTotal
exists $params{'parameters'} ? : 24 111 135
145 : ();
146  
147 135 ▶ if (my $service_path = $params{'service'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $service_path = $params{'service'}) { } 113 22 135
elsif (my $type = $params{'type'}) { } 22 0 22
148 113 my $service = $self->fetch( $service_path );
149 # NOTE:
150 # we might want to allow Bread::Board::Service::Deferred::Thunk
151 # objects as well, but I am not sure that is a valid use case
152 # for this, so for now we just don't go there.
153 # - SL
154 109 ▶ (blessed $service && $service->does('Bread::Board::Service'))
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($service) and $service->does("Bread::Board::Service") or confess('You can only resolve services, ' . (defined $service ? $service : "undef") . ' is not a Bread::Board::Service') 0 109 109
defined $service ? : 0 0 0
Condition33%1/3
builtin::blessed($service) and $service->does("Bread::Board::Service")
!ll&&!rl&&r
00109
MC/DC0%0/2
builtin::blessed($service) and $service->does("Bread::Board::Service")
builtin::blessed($service) $service->does("Bread::Board::Service")
Truth table33%1/3
A and B
A= builtin::blessed($service)B= $service->does("Bread::Board::Service")
ABresult
0X0
100
111
155 || confess "You can only resolve services, "
156 . (defined $service ? $service : 'undef')
157 . " is not a Bread::Board::Service";
158 109 return $service->get( %parameters );
159 }
160 elsif (my $type = $params{'type'}) {
161  
162 22 ▶ ($self->has_type_mapping_for( $type ))
Branch: 1 branch
BranchTrueFalseTotal
$self->has_type_mapping_for($type) or confess("Could not find a mapped service for type ($type)") 0 22 22
163 || confess "Could not find a mapped service for type ($type)";
164  
165 22 my $service = $self->get_type_mapping_for( $type );
166 22 my $result = $service->get( %parameters );
167  
168 22 ▶ (find_type_constraint( $type )->check( $result ))
Branch: 1 branch
BranchTrueFalseTotal
find_type_constraint($type)->check($result) or confess("The result of the service for type ($type) did not" . " pass the type constraint with $result") 0 22 22
169 || confess "The result of the service for type ($type) did not"
170 . " pass the type constraint with $result";
171  
172 22 return $result;
173 }
174 else {
175 0 confess "Cannot call resolve without telling it what to resolve.";
176 }
177  
178 }
179  
180 61 __PACKAGE__->meta->make_immutable;
181  
182 61 ▶no Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
183 61 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
184  
185 61 1;
186  
187 __END__
188  
189 =pod
190  
191 =encoding UTF-8
192  
193 =head1 NAME
194  
195 Bread::Board::Container - A container for services and other containers
196  
197 =head1 VERSION
198  
199 version 0.35
200  
201 =head1 SYNOPSIS
202  
203 use Bread::Board;
204 my $c = container MCP => as {
205 container Users => as {
206 service flynn => ...;
207 service bradley => ...;
208 service dillinger => ...;
209 };
210  
211 container Programs => as {
212 container Rebels => as {
213 service tron => ...;
214 service yori => ...;
215 alias flynn => '/Users/flynn';
216 };
217  
218 # nested container
219 container Slaves => as {
220 service sark => ...;
221 service crom => ...;
222 };
223 };
224 };
225  
226 # OR directly...
227 my $guardians => Bread::Board::Container->new( name => 'Guardians' );
228 $guardians->add_service(
229 Bread::Board::ConstructorInjection->new(
230 name => 'dumont',
231 ...,
232 )
233 );
234 $c->get_sub_container('Programs')->add_sub_container($guardians);
235  
236 =head1 DESCRIPTION
237  
238 This class implements the container for L<Bread::Board>: a container
239 is a thing that contains services and other containers. Each container
240 and service has a name, so you end up with a tree of named nodes, just
241 like files and directories in a filesystem: each item can be
242 referenced using a path (see L<Bread::Board::Traversable> for the
243 details).
244  
245 =head1 ATTRIBUTES
246  
247 =head2 C<name>
248  
249 Read/write string, required. Every container needs a name, by which it
250 can be referenced when L<fetching it|Bread::Board::Traversable/fetch>.
251  
252 =head2 C<services>
253  
254 Hashref, constrained by L<<
255 C<Bread::Board::Container::ServiceList>|Bread::Board::Types/Bread::Board::Container::ServiceList
256 >>, mapping names to services directly contained in this
257 container. Every service added here will have its L<<
258 C<parent>|Bread::Board::Traversable/parent >> set to this container.
259  
260 You can pass an arrayref of services instead of a hashref, the keys
261 will be the names of the services.
262  
263 You should probably use L</add_service> and L</get_service> to
264 manipulate this attribute, instead of modifying it directly.
265  
266 =head2 C<sub_containers>
267  
268 Hashref, constrained by L<<
269 C<Bread::Board::Container::SubContainerList>|Bread::Board::Types/Bread::Board::Container::SubContainerList
270 >>, mapping names to containers directly contained in this
271 container. Every container added here will have its L<<
272 C<parent>|Bread::Board::Traversable/parent >> set to this container.
273  
274 You can pass an arrayref of containers instead of a hashref, the keys
275 will be the names of the containers.
276  
277 You should probably use L</add_sub_container> and
278 L</get_sub_container> to manipulate this attribute, instead of
279 modifying it directly.
280  
281 Containers added here can either be normal L<Bread::Board::Container>
282 or L<Bread::Board::Container::Parameterized>.
283  
284 =head1 METHODS
285  
286 =head2 C<add_service>
287  
288 $container->add_service($service);
289  
290 Adds a service into the L</services> map, using its name as the key.
291  
292 =head2 C<get_service>
293  
294 my $service = $container->get_service($name);
295  
296 Returns a service by name, or C<undef> if there's no such service in
297 the L</services> map.
298  
299 =head2 C<has_service>
300  
301 if ($container->has_service($name)) { ... }
302  
303 Returns true if a service with the given name name exists in the
304 L</services> map, false otherwise.
305  
306 =head2 C<has_services>
307  
308 if ($container->has_services) { ... }
309  
310 Returns true if the L</services> map contains any services, false if
311 it's empty.
312  
313 =head2 C<get_service_list>
314  
315 my @service_names = $container->get_service_list();
316  
317 Returns the names off all services present in the L</services> map.
318  
319 =head2 C<add_sub_container>
320  
321 $container->add_sub_container($container);
322  
323 Adds a container into the L</sub_containers> map, using its name as
324 the key.
325  
326 =head2 C<get_sub_container>
327  
328 my $container = $container->get_sub_container($name);
329  
330 Returns a container by name, or C<undef> if there's no such container
331 in the L</sub_containers> map.
332  
333 =head2 C<has_sub_container>
334  
335 if ($container->has_sub_container($name)) { ... }
336  
337 Returns true if a container with the given name name exists in the
338 L</sub_containers> map, false otherwise.
339  
340 =head2 C<has_sub_containers>
341  
342 if ($container->has_sub_containers) { ... }
343  
344 Returns true if the L</sub_containers> map contains any contains,
345 false if it's empty.
346  
347 =head2 C<get_sub_container_list>
348  
349 my @container_names = $container->get_sub_container_list();
350  
351 Returns the names off all containers present in the L</sub_containers>
352 map.
353  
354 =head2 C<add_type_mapping_for>
355  
356 $containers->add_type_mapping_for( $type_name, $service );
357  
358 Adds a mapping from a L<Moose type|Moose::Util::TypeConstraints> to a
359 service: whenever we try to L<< resolve|/resolve ( ?service =>
360 $service_name, ?type => $type, ?parameters => { ... } ) >> that type,
361 we'll use that service to instantiate it.
362  
363 =head2 C<get_type_mapping_for>
364  
365 my $service = $container->get_type_mapping_for( $type_name );
366  
367 Returns the service to use to instantiate the given type name.
368  
369 Important: if a mapping for the exact type can't be found, but a
370 mapping for a I<subtype> of it can, you'll get the latter instead:
371  
372 package Superclass { use Moose };
373 package Subclass { use Moose; exends 'Superclass' };
374  
375 $c->add_type_mapping_for(
376 'Subclass',
377 Bread::Board::ConstructorInjection->new(name=>'sc',class=>'Subclass'),
378 );
379 my $o = $c->get_type_mapping_for('Superclass')->get;
380  
381 C<$o> is an instance of C<Subclass>. If there are more than one
382 sub-type mapped, you get a random one. This is probably a bad idea.
383  
384 =head2 C<has_type_mapping_for>
385  
386 if ($container->has_type_mapping_for( $type_name )) { ... }
387  
388 Returns true if we have a service defined to instantiate the given
389 type name, but see the note on
390 L<get_type_mapping_for|/get_type_mapping_for ( $type_name )> about
391 subtype mapping.
392  
393 =head2 C<resolve>
394  
395 my $object = $container->resolve(service=>$service_name);
396 my $object = $container->resolve(service=>$service_name,parameters=>\%p);
397  
398 When given a service name, this method will
399 L<fetch|Bread::Board::Traversable/fetch> the service, then call L<<
400 C<get>|Bread::Board::Service/get >> on it, optionally passing the
401 given parameters.
402  
403 my $object = $container->resolve(type=>$type);
404 my $object = $container->resolve(type=>$type,parameters=>\%p);
405  
406 When given a type name, this method will use
407 L<get_type_mapping_for|/get_type_mapping_for ( $type_name )> to get
408 the service, then call L<< C<get>|Bread::Board::Service/get >> on it,
409 optionally passing the given parameters. If the instance is not of the
410 expected type, the method will die.
411  
412 =head1 AUTHOR
413  
414 Stevan Little <stevan@iinteractive.com>
415  
416 =head1 BUGS
417  
418 Please report any bugs or feature requests on the bugtracker website
419 https://github.com/stevan/BreadBoard/issues
420  
421 When submitting a bug or request, please include a test-file or a
422 patch to an existing test-file that illustrates the bug or desired
423 feature.
424  
425 =head1 COPYRIGHT AND LICENSE
426  
427 This software is copyright (c) 2017, 2016, 2015, 2014, 2013, 2011, 2009 by Infinity Interactive.
428  
429 This is free software; you can redistribute it and/or modify it under
430 the same terms as the Perl 5 programming language system itself.
431  
432 =cut
« Board.pm ↑ Summary Board/Service/Inferred.pm »