Bolts/Artifact.pm

filter: Statementstmt 97.8% 91 / 93 Branchbran 92.3% 24 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 94.3% 133 / 141 CC 19 SCAR 17.9
CC
19
Cov
95%
get
11.1
infer_injectors
7.0
such_that
3.0
CRAP 6.0
« Bolts/Role/RootLocator.pm ↑ Summary Bolts/Inference/Moose.pm »
1 package Bolts::Artifact;
2 11 $Bolts::Artifact::VERSION = '0.143171';
3 # ABSTRACT: Tools for resolving an artifact value
4  
5 11 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 with 'Bolts::Role::Artifact';
8  
9 11 ▶use Bolts::Util qw( locator_for meta_locator_for );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use List::MoreUtils qw( all );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Scalar::Util qw( weaken reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 11 has init_locator => (
18 is => 'ro',
19 does => 'Bolts::Role::Locator',
20 weak_ref => 1,
21 );
22  
23 11 with 'Bolts::Role::Initializer';
24  
25  
26 11 has name => (
27 is => 'ro',
28 isa => 'Str',
29 required => 1,
30 );
31  
32  
33 11 has blueprint => (
34 is => 'ro',
35 does => 'Bolts::Blueprint',
36 required => 1,
37 traits => [ 'Bolts::Initializer' ],
38 );
39  
40  
41 11 has scope => (
42 is => 'ro',
43 does => 'Bolts::Scope',
44 required => 1,
45 traits => [ 'Bolts::Initializer' ],
46 );
47  
48  
49 11 has infer => (
50 is => 'ro',
51 isa => enum([qw( none options acquisition )]),
52 required => 1,
53 default => 'none',
54 );
55  
56  
57 11 has inference_done => (
58 reader => 'is_inference_done',
59 writer => 'inference_is_done',
60 isa => 'Bool',
61 required => 1,
62 default => 0,
63 init_arg => undef,
64 );
65  
66  
67 subtype 'Bolts::Injector::List',
68 as 'ArrayRef',
69 140 ▶ where { all { $_->$_does('Bolts::Injector') } @$_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
70  
71 has injectors => (
72 is => 'ro',
73 isa => 'Bolts::Injector::List',
74 required => 1,
75 traits => [ 'Array', 'Bolts::Initializer' ],
76 handles => {
77 all_injectors => 'elements',
78 add_injector => 'push',
79 },
80 71 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
81 11 );
82  
83  
84 11 has does => (
85 accessor => 'does_type',
86 isa => 'Moose::Meta::TypeConstraint',
87 );
88  
89  
90 11 has isa => (
91 accessor => 'isa_type',
92 isa => 'Moose::Meta::TypeConstraint',
93 );
94  
95 11 ▶no Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97  
98 764 ▶sub infer_injectors {
Subroutine infer_injectors: called CC=7 SCAR=19.5
Pod documented
99 764 my ($self, $bag) = @_;
100  
101 # use Data::Dumper;
102 # warn Dumper($self);
103 # $self->inference_is_done(1);
104  
105 # Use inferences to collect the list of injectors
106 764 ▶ if ($self->infer ne 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->infer ne "none") 57 707 764
107 57 my $loc = locator_for($bag);
108 57 my $meta_loc = meta_locator_for($bag);
109  
110 57 my $inference_type = $self->infer;
111  
112 57 my $inferences = $meta_loc->acquire_all('inference');
113 57 my %injectors = map { $_->key => $_ } $self->all_injectors;
114  
115 57 my @inferred_parameters;
116 57 for my $inference (@$inferences) {
117 57 push @inferred_parameters,
118 $inference->infer($self->blueprint);
119 }
120  
121 # use Data::Dumper;
122 # warn 'INFERRED: ', Dumper(\@inferred_parameters);
123  
124 57 PARAMETER: for my $inferred (@inferred_parameters) {
125 325 my $key = $inferred->{key};
126  
127 325 ▶ next PARAMETER if defined $injectors{ $key };
Branch: 1 branch
BranchTrueFalseTotal
if defined $injectors{$key} 274 51 325
128  
129 51 my %params = %$inferred;
130 51 my $required = delete $params{required};
131 51 my $via = delete $params{inject_via};
132  
133 51 my $blueprint;
134 51 ▶ if ($inference_type eq 'options') {
Branch: 1 branch
BranchTrueFalseTotal
if ($inference_type eq 'options') { } 48 3 51
135 48 $blueprint = $meta_loc->acquire('blueprint', 'given', {
136 required => $required,
137 });
138 }
139 else {
140 3 $blueprint = $meta_loc->acquire('blueprint', 'acquired', {
141 locator => $loc,
142 path => [ $key ],
143 });
144 }
145  
146 51 $params{blueprint} = $blueprint;
147  
148 51 my $injector = $meta_loc->acquire(@$via, \%params);
149 51 ▶ unless (defined $injector) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $injector) 0 51 51
150 0 Carp::carp(qq[Unable to acquire an injector for "$via".]);
151 0 next PARAMETER;
152 }
153
154 51 $self->add_injector($injector);
155 }
156 }
157 }
158  
159  
160 90 ▶sub such_that {
Subroutine such_that: called CC=3 SCAR=11.0
Pod documented
161 90 my ($self, $such_that) = @_;
162  
163 # TODO Should probably do something special if on of the must_* are already
164 # set. Maybe make sure the new things are compatible with the old? Maybe
165 # setup a type union? Maybe croak? Maybe just carp? I don't know.
166  
167 90 ▶ $self->does_type($such_that->{does}) if defined $such_that->{does};
Branch: 1 branch
BranchTrueFalseTotal
if defined $such_that->{"does"} 87 3 90
168 90 ▶ $self->isa_type($such_that->{isa}) if defined $such_that->{isa};
Branch: 1 branch
BranchTrueFalseTotal
if defined $such_that->{"isa"} 3 87 90
169 }
170  
171 # sub init_meta {
172 # my ($self, $meta, $name) = @_;
173 #
174 # $self->blueprint->init_meta($meta, $name);
175 # $self->scope->init_meta($meta, $name);
176 #
177 # # Add the actual artifact factory method
178 # $meta->add_method($name => sub { $self });
179 #
180 # # # Add the actual artifact factory method
181 # # $meta->add_method($name => sub {
182 # # my ($bag, %params) = @_;
183 # # return $self->get($bag, %params);
184 # # });
185 # }
186  
187  
188 764 ▶sub get {
Subroutine get: called CC=11 SCAR=24.0
Pod documented
189 764 my ($self, $bag, %input_params) = @_;
190  
191 764 ▶ $self->infer_injectors($bag) unless $self->is_inference_done;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_inference_done 764 0 764
192  
193 764 my $name = $self->name;
194 764 my $blueprint = $self->blueprint;
195 764 my $scope = $self->scope;
196  
197 764 my $artifact;
198  
199 # Load the artifact from the scope unless the blueprint implies scope
200 764 ▶ $artifact = $scope->get($bag, $name)
Branch: 1 branch
BranchTrueFalseTotal
unless $blueprint->implied_scope 469 295 764
201 unless $blueprint->implied_scope;
202  
203 # The scope does not have it, so load it again from blueprints
204 764 ▶ if (not defined $artifact) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $artifact) 712 52 764
205  
206 712 my @bp_params;
207 712 for my $injector ($self->all_injectors) {
208 1051 $injector->pre_inject($bag, \%input_params, \@bp_params);
209 }
210  
211 711 $artifact = $blueprint->get($bag, $name, @bp_params);
212  
213 711 for my $injector ($self->all_injectors) {
214 1049 $injector->post_inject($bag, \%input_params, $artifact);
215 }
216  
217 # Carp::croak("unable to build artifact $name from blueprint")
218 # unless defined $artifact;
219  
220 # Add the item into the scope for possible reuse from cache
221 711 ▶ $scope->put($bag, $name, $artifact)
Branch: 1 branch
BranchTrueFalseTotal
unless $blueprint->implied_scope 416 295 711
222 unless $blueprint->implied_scope;
223 }
224  
225 # TODO This would be a much more helpful check to apply ahead of time in
226 # cases where we can. Possibly some sort of such_that check on the
227 # blueprints to be handled when such checks can be sensibly handled
228 # ahead of time.
229  
230 763 my $isa = $self->isa_type;
231 763 my $does = $self->does_type;
232  
233 763 my $msg;
234 763 ▶ $msg = $isa->validate($artifact) if defined $isa;
Branch: 1 branch
BranchTrueFalseTotal
if defined $isa 5 758 763
235 763 ▶ $msg //= $does->validate($artifact) if defined $does;
Branch: 1 branch
BranchTrueFalseTotal
if defined $does 286 477 763
Condition33%1/3
$msg //= $does->validate($artifact)
l!l&&r!l&&!r
00286
MC/DC0%0/2
$msg //= $does->validate($artifact)
$msg $does->validate($artifact)
Truth table33%1/3
A //= B
A= $msgB= $does->validate($artifact)
ABresult
1X1
011
000
236  
237 763 ▶ Carp::croak(qq[Constructed artifact named "$name" has the wrong type: $msg]) if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 1 762 763
238  
239 762 return $artifact;
240 }
241  
242 # sub inline_get {
243 # my $blueprint_inline = $self->blueprint->inline_get;
244 # my $scope_inline = $self->scope->inline_scope;
245 #
246 # return q[
247 # my ($self, $bag, %params) = @_;
248 # my $artifact;
249 #
250 # ].$scope_inline.q[
251 #
252 # if (not defined $artifact) {
253 # ].$blueprint_inline.q[
254 # }
255 #
256 # return $artifact;
257 # ];
258 # }
259  
260 11 __PACKAGE__->meta->make_immutable;
261  
262 __END__
« Bolts/Role/RootLocator.pm ↑ Summary Bolts/Inference/Moose.pm »