Board/Service/Inferred.pm

filter: Statementstmt 98.7% 78 / 79 Branchbran 86.8% 33 / 38 Conditioncond 80.0% 12 / 15 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 91.3% 138 / 151 CC 27 SCAR 30.6
CC
27
Cov
93%
infer_service
27.5
BEGIN
1.0
BEGIN
1.0
CRAP 21.4
« Board/Dumper.pm ↑ Summary Board/Traversable.pm »
1 package Bread::Board::Service::Inferred;
2 55 our $AUTHORITY = 'cpan:STEVAN';
3 # ABSTRACT: Helper for inferring a service from a Moose object
4 55 $Bread::Board::Service::Inferred::VERSION = '0.36';
5 55 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 55 ▶use Moose::Util::TypeConstraints 'find_type_constraint';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 55 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 55 ▶use Bread::Board::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 55 ▶use Bread::Board::ConstructorInjection;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 55 has 'current_container' => (
13 is => 'ro',
14 isa => 'Bread::Board::Container',
15 required => 1,
16 );
17  
18 55 has 'service' => (
19 is => 'ro',
20 isa => 'Bread::Board::ConstructorInjection',
21 predicate => 'has_service',
22 );
23  
24 has 'service_args' => (
25 is => 'ro',
26 isa => 'HashRef',
27 lazy => 1,
28 11 ▶ default => sub { +{} }
Subroutine __ANON__: called CC=1 SCAR=0.0
29 55 );
30  
31 has 'infer_params' => (
32 is => 'ro',
33 isa => 'Bool',
34 11 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 55 );
36  
37 sub infer_service {
38 35 ▶ my $self = shift;
Subroutine infer_service: called CC=27 SCAR=33.1
Pod documented
39 35 my $type = shift;
40 35 ▶ my $seen = shift || {};
Condition100%2/2
shift() || {}
l!l
1124
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
41 35 my $type_constraint = find_type_constraint( $type );
42 35 my $current_container = $self->current_container;
43  
44 # the type must exist ...
45 35 ▶ (defined $type_constraint)
Branch: 1 branch
BranchTrueFalseTotal
defined $type_constraint or confess("$type is not an existing valid Moose type") 0 35 35
46 || confess "$type is not an existing valid Moose type";
47  
48 # the type must be either
49 # a class type, or a subtype
50 # of object.
51 35 ▶ ($type_constraint->isa('Moose::Meta::TypeConstraint::Class')
Branch: 1 branch
BranchTrueFalseTotal
$type_constraint->isa("Moose::Meta::TypeConstraint::Class") or $type_constraint->is_subtype_of("Object") or confess(q[Only class types, role types, or subtypes of Object can be inferred. I don't know what to do with type (] . $type_constraint->name . ')') 0 35 35
Condition66%2/3
$type_constraint->isa("Moose::Meta::TypeConstraint::Class") or $type_constraint->is_subtype_of("Object")
l!l&&r!l&&!r
2870
MC/DC0%0/2
$type_constraint->isa("Moose::Meta::TypeConstraint::Class") or $type_constraint->is_subtype_of("Object")
$type_constraint->isa("Moose::Meta::TypeConstraint::Class") $type_constraint->is_subtype_of("Object")
Truth table66%2/3
A or B
A= $type_constraint->isa("Moose::Meta::TypeConstraint::Class")B= $type_constraint->is_subtype_of("Object")
ABresult
1X1
011
000
52 ||
53 $type_constraint->is_subtype_of('Object'))
54 || confess 'Only class types, role types, or subtypes of Object can be inferred. '
55 . 'I don\'t know what to do with type (' . $type_constraint->name . ')';
56  
57 35 my %params = (
58 name => 'type:' . $type,
59 );
60  
61 35 ▶ if ($self->has_service) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_service) { } 1 34 35
62 1 my $service = $self->service;
63 1 %params = (
64 %params,
65 name => $service->name,
66 class => $service->class,
67 dependencies => $service->dependencies,
68 parameters => $service->parameters,
69 );
70 }
71 else {
72 %params = (
73 %params,
74 34 %{ $self->service_args }
75 );
76 }
77  
78 # if the class is specified, then
79 # we can use that reliably, otherwise
80 # we need to try and figure out the
81 # class name ...
82 35 ▶ unless ( exists $params{'class'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params{"class"}) 28 7 35
83 # if it is a class type, it is easy
84 28 ▶ if ($type_constraint->isa('Moose::Meta::TypeConstraint::Class')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_constraint->isa('Moose::Meta::TypeConstraint::Class')) { } 27 1 28
85 27 $params{'class'} = $type_constraint->class;
86 }
87 # if it is not a class type, then
88 # we will make the assumption that
89 # the name of the type constraint
90 # is also the name of the class.
91 else {
92 1 $params{'class'} = $type_constraint->name;
93 }
94 }
95  
96 my $meta = Class::MOP::class_of($params{'class'})
97 35 ▶ || confess "Could not get the meta object for class(" . $params{'class'} . ")";
Condition33%1/3
Class::MOP::class_of($params{'class'}) || confess('Could not get the meta object for class(' . $params{'class'} . ')')
l!l&&r!l&&!r
3500
MC/DC0%0/2
Class::MOP::class_of($params{'class'}) || confess('Could not get the meta object for class(' . $params{'class'} . ')')
Class::MOP::class_of($params{'class'}) confess('Could not get the meta object for class(' . $params{'class'} . ')')
Truth table33%1/3
A || B
A= Class::MOP::class_of($params{'class'})B= confess('Could not get the meta object for class(' . $params{'class'} . ')')
ABresult
1X1
011
000
98  
99 35 ▶ ($meta->isa('Moose::Meta::Class'))
Branch: 2 branches
BranchTrueFalseTotal
$meta->isa("Moose::Meta::Class") or confess('We can only infer Moose classes' . ($meta->isa("Moose::Meta::Role") ? ', ' . $meta->name . ' is a role and therefore not concrete enough' : "")) 1 34 35
$meta->isa('Moose::Meta::Role') ? : 1 0 1
100 || confess "We can only infer Moose classes"
101 . ($meta->isa('Moose::Meta::Role')
102 ? (', ' . $meta->name . ' is a role and therefore not concrete enough')
103 : '');
104  
105 my @required_attributes = grep {
106 34 ▶ $_->is_required && $_->has_type_constraint
Branch: 1 branch
BranchTrueFalseTotal
$_->is_required and $_->has_type_constraint 34 2 36
107 } $meta->get_all_attributes;
108  
109 34 ▶ $params{'dependencies'} ||= {};
Condition100%2/2
$params{"dependencies"} ||= {}
l!l
331
MC/DC100%1/1
$params{"dependencies"} ||= {}
$params{"dependencies"}
Truth table100%2/2
A
A= $params{"dependencies"}
Aresult
11
00
110 34 ▶ $params{'parameters'} ||= {};
Condition100%2/2
$params{"parameters"} ||= {}
l!l
133
MC/DC100%1/1
$params{"parameters"} ||= {}
$params{"parameters"}
Truth table100%2/2
A
A= $params{"parameters"}
Aresult
11
00
111  
112 # defer this for now ...
113 34 $seen->{ $type } = $params{'name'};
114  
115 34 foreach my $attribute (@required_attributes) {
116 33 my $name = $attribute->name;
117  
118 33 ▶ next if exists $params{'dependencies'}->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"dependencies"}{$name} 2 31 33
119  
120 31 my $type_constraint = $attribute->type_constraint;
121 31 ▶ my $type_name = $type_constraint->isa('Moose::Meta::TypeConstraint::Class')
Branch: 1 branch
BranchTrueFalseTotal
$type_constraint->isa('Moose::Meta::TypeConstraint::Class') ? : 16 15 31
122 ? $type_constraint->class
123 : $type_constraint->name;
124  
125 31 my $service;
126 31 ▶ if ($current_container->has_type_mapping_for( $type_name )) {
Branch: 2 branches
BranchTrueFalseTotal
if ($current_container->has_type_mapping_for($type_name)) { } 11 20 31
elsif (exists $seen->{$type_name}) { } 1 19 20
127 11 $service = $current_container->get_type_mapping_for( $type_name )
128 }
129 elsif ( exists $seen->{ $type_name } ) {
130 1 ▶ if ( blessed($seen->{ $type_name }) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($seen->{$type_name})) { } 0 1 1
131 # if the type has already been
132 # inferred, then we use it
133 0 $service = $seen->{ $type_name };
134 }
135 else {
136 # if not, then we have to use
137 # the built in laziness and
138 # make it a dependency
139 $service = Bread::Board::Dependency->new(
140 1 service_path => $seen->{ $type_name }
141 );
142 }
143 }
144 else {
145 19 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($type_constraint->isa('Moose::Meta::TypeConstraint::Class') or $type_constraint->is_subtype_of('Object')) { } 11 8 19
Condition100%3/3
$type_constraint->isa("Moose::Meta::TypeConstraint::Class") or $type_constraint->is_subtype_of("Object")
l!l&&r!l&&!r
1018
MC/DC100%2/2
$type_constraint->isa("Moose::Meta::TypeConstraint::Class") or $type_constraint->is_subtype_of("Object")
$type_constraint->isa("Moose::Meta::TypeConstraint::Class") $type_constraint->is_subtype_of("Object")
Truth table100%3/3
A or B
A= $type_constraint->isa("Moose::Meta::TypeConstraint::Class")B= $type_constraint->is_subtype_of("Object")
ABresult
1X1
011
000
146 $type_constraint->isa('Moose::Meta::TypeConstraint::Class')
147 ||
148 $type_constraint->is_subtype_of('Object')
149 ) {
150 11 $service = Bread::Board::Service::Inferred->new(
151 current_container => $self->current_container
152 )->infer_service(
153 $type_name,
154 $seen
155 );
156 } else {
157 8 ▶ if ($self->infer_params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->infer_params) { } 7 1 8
158 7 $params{'parameters'}->{ $name } = { isa => $type_name };
159 }
160 else {
161 1 confess 'Only class types, role types, or subtypes of Object can be inferred. '
162 . 'I don\'t know what to do with type (' . $type_name . ')';
163 }
164 }
165 }
166  
167 27 ▶ $params{'dependencies'}->{ $name } = $service
Branch: 1 branch
BranchTrueFalseTotal
if defined $service 20 7 27
168 if defined $service;
169 }
170  
171 30 ▶ if ( $self->infer_params ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->infer_params) 22 8 30
172 map {
173 2 ▶ $params{'parameters'}->{ $_->name } = {
Branch: 1 branch
BranchTrueFalseTotal
$_->has_type_constraint ? : 2 0 2
174 optional => 1,
175 ($_->has_type_constraint
176 ? ( isa => $_->type_constraint )
177 : ())
178 };
179 } grep {
180 22 ( not $_->is_required )
181 } $meta->get_all_attributes
182 }
183  
184 # NOTE:
185 # this is always going to be
186 # constructor injection because
187 # that is what we do when we
188 # infer. No other type of
189 # injection makes sense here.
190 # - SL
191 30 my $service;
192 30 ▶ if ($self->has_service) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_service) { } 1 29 30
193 1 $service = $self->service->clone(%params);
194 }
195 else {
196 29 $service = Bread::Board::ConstructorInjection->new(%params);
197 }
198  
199 # NOTE:
200 # We need to do this so that
201 # anything created by a typemap
202 # can still also refer back to
203 # an actual service in the parent
204 # container.
205 # - SL
206 30 $self->current_container->add_service( $service );
207  
208 30 $service;
209 }
210  
211 55 __PACKAGE__->meta->make_immutable;
212  
213 55 ▶no Moose; 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
214  
215 __END__
216  
217 =pod
218  
219 =encoding UTF-8
220  
221 =head1 NAME
222  
223 Bread::Board::Service::Inferred - Helper for inferring a service from a Moose object
224  
225 =head1 VERSION
226  
227 version 0.36
228  
229 =head1 DESCRIPTION
230  
231 CAUTION, EXPERIMENTAL FEATURE.
232  
233 Docs to come, as well as refactoring.
234  
235 =head1 METHODS
236  
237 =head2 C<infer_service>
238  
239 =head1 AUTHOR
240  
241 Stevan Little <stevan@iinteractive.com>
242  
243 =head1 BUGS
244  
245 Please report any bugs or feature requests on the bugtracker website
246 https://github.com/stevan/BreadBoard/issues
247  
248 When submitting a bug or request, please include a test-file or a
249 patch to an existing test-file that illustrates the bug or desired
250 feature.
251  
252 =head1 COPYRIGHT AND LICENSE
253  
254 This software is copyright (c) 2017, 2016, 2015, 2014, 2013, 2011, 2009 by Infinity Interactive.
255  
256 This is free software; you can redistribute it and/or modify it under
257 the same terms as the Perl 5 programming language system itself.
258  
259 =cut
« Board/Dumper.pm ↑ Summary Board/Traversable.pm »