Board/Service/Inferred.pm

filter: Statementstmt 98.7% 78 / 79 Branchbran 86.1% 31 / 36 Conditioncond 82.3% 14 / 17 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 91.4% 139 / 152 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.37';
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 10 ▶ default => sub { +{} }
Subroutine __ANON__: called CC=1 SCAR=0.0
29 55 );
30  
31 has 'infer_params' => (
32 is => 'ro',
33 isa => 'Bool',
34 10 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
35 55 );
36  
37 34 ▶sub infer_service {
Subroutine infer_service: called CC=27 SCAR=33.1
Pod documented
38 34 my $self = shift;
39 34 my $type = shift;
40 34 ▶ my $seen = shift || {};
Condition100%2/2
shift() || {}
l!l
1024
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
41 34 my $type_constraint = find_type_constraint( $type );
42 34 my $current_container = $self->current_container;
43  
44 # the type must exist ...
45 34 ▶ (defined $type_constraint)
Branch: 1 branch
BranchTrueFalseTotal
defined $type_constraint or confess("$type is not an existing valid Moose type") 34 0 34
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 34 ▶ ($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 . ')') 34 0 34
Condition66%2/3
$type_constraint->isa("Moose::Meta::TypeConstraint::Class") or $type_constraint->is_subtype_of("Object")
l!l&&r!l&&!r
2770
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 34 my %params = (
58 name => 'type:' . $type,
59 );
60  
61 34 ▶ if ($self->has_service) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_service) { } 1 33 34
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 33 %{ $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 34 ▶ unless ( exists $params{'class'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params{"class"}) 27 7 34
83 # if it is a class type, it is easy
84 27 ▶ if ($type_constraint->isa('Moose::Meta::TypeConstraint::Class')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_constraint->isa('Moose::Meta::TypeConstraint::Class')) { } 26 1 27
85 26 $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 34 ▶ || confess "Could not get the meta object for class(" . $params{'class'} . ")";
Condition50%1/2
Class::MOP::class_of($params{'class'}) || confess('Could not get the meta object for class(' . $params{'class'} . ')')
l!l
340
MC/DC0%0/1
Class::MOP::class_of($params{'class'}) || confess('Could not get the meta object for class(' . $params{'class'} . ')')
Class::MOP::class_of($params{'class'})
Truth table50%1/2
A
A= Class::MOP::class_of($params{'class'})
Aresult
11
00
98  
99 34 ▶ ($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' : "")) 33 1 34
$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 33 ▶ $_->is_required && $_->has_type_constraint
Condition66%2/3
$_->is_required && $_->has_type_constraint
!ll&&!rl&&r
2034
MC/DC50%1/2
$_->is_required && $_->has_type_constraint
$_->is_required $_->has_type_constraint
Truth table66%2/3
A && B
A= $_->is_requiredB= $_->has_type_constraint
ABresult
0X0
100
111
107 } $meta->get_all_attributes;
108  
109 33 ▶ $params{'dependencies'} ||= {};
Condition100%2/2
$params{"dependencies"} ||= {}
l!l
330
MC/DC100%1/1
$params{"dependencies"} ||= {}
$params{"dependencies"}
Truth table100%2/2
A
A= $params{"dependencies"}
Aresult
11
00
110 33 ▶ $params{'parameters'} ||= {};
Condition100%2/2
$params{"parameters"} ||= {}
l!l
132
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 33 $seen->{ $type } = $params{'name'};
114  
115 33 foreach my $attribute (@required_attributes) {
116 31 my $name = $attribute->name;
117  
118 31 ▶ next if exists $params{'dependencies'}->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"dependencies"}{$name} 2 29 31
119  
120 29 my $type_constraint = $attribute->type_constraint;
121 29 ▶ my $type_name = $type_constraint->isa('Moose::Meta::TypeConstraint::Class')
Branch: 1 branch
BranchTrueFalseTotal
$type_constraint->isa('Moose::Meta::TypeConstraint::Class') ? : 15 14 29
122 ? $type_constraint->class
123 : $type_constraint->name;
124  
125 29 my $service;
126 29 ▶ if ($current_container->has_type_mapping_for( $type_name )) {
Branch: 2 branches
BranchTrueFalseTotal
if ($current_container->has_type_mapping_for($type_name)) { } 11 18 29
elsif (exists $seen->{$type_name}) { } 1 17 18
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 17 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($type_constraint->isa('Moose::Meta::TypeConstraint::Class') or $type_constraint->is_subtype_of('Object')) { } 10 7 17
Condition100%3/3
$type_constraint->isa("Moose::Meta::TypeConstraint::Class") or $type_constraint->is_subtype_of("Object")
l!l&&r!l&&!r
917
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 10 $service = Bread::Board::Service::Inferred->new(
151 current_container => $self->current_container
152 )->infer_service(
153 $type_name,
154 $seen
155 );
156 } else {
157 7 ▶ if ($self->infer_params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->infer_params) { } 6 1 7
158 6 $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 25 ▶ $params{'dependencies'}->{ $name } = $service
Branch: 1 branch
BranchTrueFalseTotal
if defined $service 19 6 25
168 if defined $service;
169 }
170  
171 29 ▶ if ( $self->infer_params ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->infer_params) 22 7 29
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 29 my $service;
192 29 ▶ if ($self->has_service) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_service) { } 1 28 29
193 1 $service = $self->service->clone(%params);
194 }
195 else {
196 28 $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 29 $self->current_container->add_service( $service );
207  
208 29 $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__
« Board/Dumper.pm ↑ Summary Board/Traversable.pm »