Bolts.pm

filter: Statementstmt 99.0% 100 / 101 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 96.5% 28 / 29 Podpod 80.0% 8 / 10 totaltotal 97.3% 144 / 148 SCAR 17.9
CC
6
Cov
99%
option
3.0
__ANON__
2.0
_bag_meta
2.0
CRAP 6.0
« Bolts/Inference/Moose.pm ↑ Summary Bolts/Blueprint/Given.pm »
1 package Bolts;
2 10 $Bolts::VERSION = '0.143170';
3 # ABSTRACT: An Inversion of Control framework for Perl
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 # Register attribute traits
9 10 ▶use Bolts::Meta::Attribute::Trait::Initializer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 ▶use Class::Load ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Moose::Util::MetaRole ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Moose::Util::TypeConstraints ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 10 ▶use Bolts::Util qw( locator_for );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use Bolts::Blueprint::Given;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use Bolts::Blueprint::Literal;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶use Bolts::Blueprint::Built;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 10 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 10 our @CARP_NOT = qw( Moose::Exporter );
26  
27 # Ugly, but so far... necessary...
28 10 our $GLOBAL_FALLBACK_META_LOCATOR = 'Bolts::Meta::Locator';
29  
30 10 my @BAG_META;
31  
32 10 Moose::Exporter->setup_import_methods(
33 class_metaroles => {
34 class => [
35 'Bolts::Meta::Class::Trait::Locator',
36 'Bolts::Meta::Class::Trait::Bag',
37 ],
38 },
39 base_class_roles => [ 'Bolts::Role::SelfLocator' ],
40 with_meta => [ qw(
41 artifact bag builder contains dep option self such_that_each value
42 ) ],
43 also => 'Moose',
44 );
45  
46 sub init_meta {
47 11 ▶ my $class = shift;
Subroutine init_meta: called CC=1 SCAR=0.0
Pod undocumented
48 11 my $meta = Moose->init_meta(@_);
49  
50 $meta->add_attribute(__top => (
51 reader => '__top',
52 required => 1,
53 22 ▶ default => sub { shift },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 11 lazy => 1,
55 weak_ref => 1,
56 ));
57  
58 11 return $meta;
59 }
60  
61 sub _bag_meta {
62 54 ▶ my ($meta) = @_;
Subroutine _bag_meta: called CC=2 SCAR=6.9
63  
64 54 ▶ $meta = $BAG_META[-1] if @BAG_META;
Branch: 1 branch
BranchTrueFalseTotal
if @BAG_META 9 45 54
65  
66 54 return $meta;
67 }
68  
69  
70 sub artifact {
71 39 ▶ my $meta = _bag_meta(shift);
Subroutine artifact: called CC=1 SCAR=0.0
Pod documented
72 39 my $artifact = Bolts::Util::artifact($meta, @_);
73 39 $meta->add_artifact($artifact->name, $artifact);
74 39 return;
75 }
76  
77  
78 10 our @BAG_OF_BUILDING;
79 sub bag {
80 2 ▶ my ($meta, $name, $partial_def) = @_;
Subroutine bag: called CC=1 SCAR=0.0
Pod documented
81  
82 2 $meta = _bag_meta($meta);
83  
84 2 my $def = $partial_def->($name);
85 $meta->add_artifact(
86 $name => Bolts::Artifact::Thunk->new(
87 thunk => sub {
88 16 ▶ my ($self, $bag, $name, %params) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
89 16 return $def->name->new(
90 __parent => $bag,
91 %params,
92 );
93 },
94 )
95 2 );
96 }
97  
98 sub contains(&;$) {
99 2 ▶ my ($parent_meta, $code, $such_that_each) = @_;
Subroutine contains: called CC=1 SCAR=0.0
Pod undocumented
100  
101 2 my $meta = _bag_meta($parent_meta);
102  
103 return sub {
104 2 ▶ my ($name) = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
105  
106 2 my $parent = $meta->name;
107  
108 2 ▶ my $bag_meta = Bolts::Bag->start_bag(
Branch: 1 branch
BranchTrueFalseTotal
$such_that_each ? : 1 1 2
109 package => "${parent}::$name",
110 ($such_that_each ? (such_that_each => $such_that_each) : ()),
111 );
112 2 push @BAG_META, $bag_meta;
113  
114 $bag_meta->add_attribute(__parent => (
115 reader => '__parent',
116 required => 1,
117 0 ▶ default => sub { Carp::confess('why are we here?') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
118 2 weak_ref => 1,
119 ));
120  
121 2 $bag_meta->add_artifact(
122 __top => Bolts::Artifact->new(
123 meta_locator => $bag_meta,
124 name => '__top',
125 blueprint => $bag_meta->acquire('blueprint', 'acquired', {
126 path => [ '__parent', '__top' ],
127 }),
128 scope => $bag_meta->acquire('scope', 'prototype'),
129 )
130 );
131  
132 2 $code->($bag_meta);
133  
134 2 pop @BAG_META;
135  
136 2 $bag_meta->finish_bag;
137  
138 2 return $bag_meta;
139 2 };
140 }
141  
142  
143 sub such_that_each($) {
144 1 ▶ my ($meta, $params) = @_;
Subroutine such_that_each: called CC=1 SCAR=0.0
Pod documented
145 1 return $params;
146 }
147  
148  
149 sub builder(&) {
150 2 ▶ my ($meta, $code) = @_;
Subroutine builder: called CC=1 SCAR=0.0
Pod documented
151 2 $meta = _bag_meta($meta);
152  
153 return {
154 2 blueprint => $meta->acquire('blueprint', 'built_injector', {
155 builder => $code,
156 }),
157 };
158 }
159  
160  
161 sub dep($) {
162 9 ▶ my ($meta, $path) = @_;
Subroutine dep: called CC=2 SCAR=6.9
Pod documented
163 9 $meta = _bag_meta($meta);
164  
165 9 ▶ $path = [ $path ] unless ref $path eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path eq "ARRAY" 4 5 9
166  
167 9 my @path = ('__top', @$path);
168  
169 return {
170 9 blueprint => $meta->acquire('blueprint', 'acquired', {
171 path => \@path,
172 }),
173 };
174 }
175  
176  
177 sub option($) {
178 8 ▶ my ($meta, $p) = @_;
Subroutine option: called CC=3 SCAR=11.0
Pod documented
179  
180 8 my %bp = %$p;
181 8 my %ip;
182 8 for my $k (qw( isa does )) {
183 16 ▶ $ip{$k} = delete $bp{$k} if exists $bp{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $bp{$k} 8 8 16
184 }
185  
186 return {
187 8 %ip,
188 blueprint => $meta->acquire('blueprint', 'given', \%bp),
189 },
190 }
191  
192  
193 sub value($) {
194 18 ▶ my ($meta, $value) = @_;
Subroutine value: called CC=1 SCAR=0.0
Pod documented
195  
196 return {
197 18 blueprint => $meta->acquire('blueprint', 'literal', {
198 value => $value,
199 }),
200 };
201 }
202  
203  
204 sub self() {
205 1 ▶ my ($meta, $value) = @_;
Subroutine self: called CC=1 SCAR=0.0
Pod documented
206  
207 return {
208 1 blueprint => $meta->acquire('blueprint', 'parent_bag'),
209 };
210 }
211  
212  
213 10 1;
214  
215 __END__
216  
217 =pod
218  
219 =encoding UTF-8
220  
221 =head1 NAME
222  
223 Bolts - An Inversion of Control framework for Perl
224  
225 =head1 VERSION
226  
227 version 0.143170
228  
229 =head1 SYNOPSIS
230  
231 package MyApp;
232 use Bolts;
233  
234 artifcat log_file => 'var/messages.log';
235 artifact logger => (
236 class => 'MyApp::Logger',
237 scope => 'singleton',
238 infer => 'acquisition',
239 );
240  
241 # Later...
242 my $log = $app->acquire('logger');
243 $log->error("Bad stuff.");
244  
245 =head1 DESCRIPTION
246  
247 B<Caution:> I<< This is an B<experimental> API. Some aspects of the API may change, possibly drastically, from version to version. That probably won't happen, but please contact me via email if you plan to use this in something and want to know what might change. Pay close attention to any B<Caution> remarks in the documentation. >>
248  
249 This is yet another Inversion of Control framework for Perl. This one is based upon a combination of L<Bread::Board>, concepts from the Spring framework, and a good mix of my own ideas and modifications after spending a few years using L<Moose> and Bread::Board.
250  
251 =head2 Inversion of Control
252  
253 For those who might not know what Inversion of Control (IOC) is, it is a design pattern aimed at helping you decouple your code, automate parts of the configuration assembly, and manage the life cycle of your objects.
254  
255 By using an IOC framework, the objects in your program need to know less about the other objects in your application. Your objects can focus on knowing what it needs from other objects without knowing where to find objects that do that or how they are configured.
256  
257 For example, early in a program's lifetime, the logger might be a local object that writes directly to a file. Later, it might be an object with the same interface, but it writes to syslog. Further on, it might be some sort of logging service that is accessed over the network through a stub provided by a service locator. If your program uses an IOC framework, the configuration files for your IOC will change to pass a different object to the application during each phase, but the program itself might not change at all.
258  
259 An IOC framework also helps you assemble complex configuration related to your application. It can join various configurations together in interesting and complex ways automatically.
260  
261 An IOC framework can make sure that your objects live only as long as they should or longer than they would normally. It can manage the list of objects that should be created each time (prototypes), objects that should last as long as a user session, objects that should last as long as some request, and objects that last for the duration of the process (singletons).
262  
263 The next sections will introduce the concepts and terminology used by this framework.
264  
265 =head2 Artifacts
266  
267 The basic building block of the Bolts IOC framework is the B<artifact>. At the simplest level, an artifact is any kind of thing your program might use. It might be a value, it might be a reference to something, it might be an object, or it might be something more complex.
268  
269 For simple values and direct references to things, you can treat any thing as an artifact. However, the real power starts when you use an implementation of L<Bolts::Role::Artifact>, usually L<Bolts::Artifact> to manage that thing. These provide utilities for constructing an object or other value according to some set of instructions and directions for managing the lifecycle of the artifact in question.
270  
271 =head2 Bags
272  
273 Artifacts are grouped into bags. A B<bag> can be any object, hash reference, or array reference. Artifacts are associated with the bag as indexes in the array, keys on the hash, or methods on the object. Literally, any object can be used as a bag, which differs from frameworks like L<Bread::Board>, which requires that its services be put inside a special container object. Bolts just uses hashes, arrays, and objects in the usual Perl way to locate artifacts.
274  
275 =head2 Locators
276  
277 A B<locator> is an object that finds things in a bag (these are things related to L<Bolts::Role::Locator>. The finding process is called, B<acquisition>. (If you are familiar with Harry Potter, this process is similar to Harry Potter using a wand to extract dittany from Hermione's handbag by saying "Accio Dittany.") After finding the object, the locator performs B<resolution>, which checks to see if the returned artifact needs to be resolved further. (To continue the analogy, this is like unbottling the dittany and pouring it out, there may be another step before the artifact is completely ready for use.)
278  
279 =head2 Blueprints
280  
281 Attached to L<Bolts::Artifact> definitions are a set of blueprints (some object that implements L<Bolts::Blueprint>). A B<blueprint> describes how an artifact is located or constructed, which is part of resolution. The system provides standard blueprints that can cover all possible needs, but you can create your own to extend the framework as necessary. The built-in blueprints can locate an object by acquring it from a bag, the result of a subroutine, by use of a factory method or constructor on an object, by directly handing the value in to the bag when the bag is constructed, or set as a constant.
282  
283 =head2 Injectors
284  
285 Another step in resolution is injection. An B<injector> associates additional artifacts with the artifact being resolved. This might be values that need to be passed to the artifact during construction, methods that need to be called to configure the object following construction, or even keys on a hash that need to be set on the artifact.
286  
287 Injectors come in two flavors, injection by automatic acquisition and by given options. With acquisition, the framework will acquire or other provide addition artifacts to the artifact being resolved automatically, this is where much of the power of IOC comes from. Sometimes, however, an object just requires some configuration state to let it know how it will be used. In those cases, options can be directly passed to C<acquire> to be used for injection.
288  
289 =head2 Scope
290  
291 The B<scope> of an artifact determines during what period an artifact is valid. Bolts provides two built-in scopes, prototype and singleton. A prototype represents an artifact that must be resolved every time it is acquired. A singleton represents an artifact that is resolved only the first time it is acquired and is then reused for all following acquisitions for the lifetime of the bag.
292  
293 =head2 Infererer
294  
295 It is usually considered a bad thing in computer science if you have to configure something twice in the same program. Such duplication is tedious and leads to technical debt. This, unfortunately, is often the case when using some IOC frameworks. You configure the object once using Moose and then a second time to let the IOC framework know how to inject configuration into the artifact. This is where the inferers come in.
296  
297 An B<inferer> is a tool that can inspect an object and automatically decide how that object should be configured. Bolts provides an inferer for L<Moose> that can use the metadata about a L<Moose> object to determine how to inject into that object automatically.
298  
299 =head2 Meta Locator and Extension
300  
301 One of the goals of this system is to have the system rely on the IOC internally as much as possible and to decouple the components as much as possible. This goal has not been totally achieved, but it is something strived for. The framework itself depends on L<Bolts::Meta::Locator>, which provides all the standard definitions internally. This can be extended to provide additional or even alternate features.
302  
303 All the various components: artifact, bag, locator, blueprint, injector, scope, and inferer are completely extensible. You can create new versions of L<Bolts::Role::Artifact>. You can create bags from almost anything. You can create new locators via L<Bolts::Role::Locator>. You can create new blueprints via L<Bolts::Blueprint>. You can create new scopes via L<Bolts::Scope>. You can create new inferers via L<Bolts::Inferer>. You can then associate these components with the internals using L<Bolts::Meta::Locator>.
304  
305 =head1 THIS CLASS
306  
307 The purpose of the Bolts module itself is to provide some nice syntactic sugar for turning the class that uses it into a bag and locator.
308  
309 =head1 FUNCTIONS
310  
311 =head2 artifact
312  
313 artifact 'name';
314 artifact name => $value;
315 artifact name => %options;
316  
317 This defines an artifact in the current class. This will create a method on the current object with the given "name". If only the name is given, then the artifact to use must be passed when the bag is constructed.
318  
319 # for example, if you bag is named "MyApp"
320 my $bag = MyApp->new( name => 42 );
321 my $value = $bag->acquire('name');
322 say $value; # 42
323  
324 If a scalar or reference is passed in as a single argument in addition to the name, the artifact will be set to that literal value.
325  
326 Otherwise, you may pass in a list of pairs, which will be interpreted depending on the keys present. Here is a list of keys and their meanings:
327  
328 =over
329  
330 =item path
331  
332 This is like an alias to an artifact elsewhere within this bag or in another bag (if "locator" is passed as well). It is set to a reference to an array of names, naming the path within the bag to acquire. See L<Bolts::Blueprint::Acquired> for details.
333  
334 =item value
335  
336 This sets the artifact to a literal value, similar to passing C<$value> in the example above. See L<Bolts::Blueprint::Literal> for details.
337  
338 =item class
339  
340 This should be set to a package name. This causes the artifact to construct and return the value from a factory method or constructor. See L<Bolts::Blueprint::Factory> for details.
341  
342 =item builder
343  
344 This should be set to a subroutine. The subroutine will be called to attain this artifact and the return value used as the artifact. See L<Bolts::Blueprint::Builder> for details.
345  
346 =item blueprint
347  
348 This is set to the name of a L<Bolts::Blueprint> definition and allows you to specify the blueprint you wish to use directly.
349  
350 =item scope
351  
352 In addition to the options above, you may also specify the scope. This is usually either "prototype" or "singleton" and the default is generally "prototype".
353  
354 =back
355  
356 =head2 bag
357  
358 bag 'name' => contains {
359 artifact 'child_name' => 42;
360 };
361  
362 Attaches a bag at the named location. This provides tools for assembling complex IOC configurations.
363  
364 =head2 such_that_each
365  
366 bag 'name' => contains {
367 artifact 'child_name' => 'value';
368  
369 } such_that_each {
370 isa => 'Str',
371 };
372  
373 Causes every artifact within the bag to have the same type constraints, which is handy in some cases. The first argument is a hash that may contain an C<isa> key and a C<does> key, which will be applid to each of the artifacts within. The second argument is the bag definition, which should be built using C<contains> as shown in the description of L</bag>.
374  
375 =head2 builder
376  
377 artifact name => (
378 ...
379 parameters => {
380 thing => builder {
381 return MyApp::Thing->new,
382 },
383 },
384 );
385  
386 This is a helper for setting a L<Bolts::Blueprint::BuiltInjector> for use in passing in a dependency that is wired directly to the builder function given.
387  
388 =head2 dep
389  
390 artifact other => ( ... );
391  
392 artifact name => (
393 ...
394 parameters => {
395 thing => dep('other'),
396 },
397 );
398  
399 This is a helper for laoding dependencies from a path in the current bag (or a bag within it).
400  
401 =head2 option
402  
403 artifact name => (
404 ...
405 parameters => {
406 thing => option {
407 isa => 'MyApp::Thing',
408 required => 1,
409 },
410 other_thing => option {
411 does => 'MyApp::OtherThing',
412 },
413 },
414 );
415  
416 Helper to allow a dependency to be passed as a given option to the call to L<Bolts::Role::Locator/acquire>. To provide validators for the values pass, you may set the C<isa> and C<does> options to a L<Moose> type constraint. To make the option required, set the C<required> option.
417  
418 =head2 value
419  
420 artifact name => (
421 ...
422 parameters => {
423 thing => value 42,
424 },
425 );
426  
427 Helper that passes a literal value through as a dependency to the artifact
428 during injection.
429  
430 =head2 self
431  
432 artifact thing => (
433 ...
434 parameters => {
435 parent => self,
436 },
437 );
438  
439 Sets up a blueprint to return the artifact's parent.
440  
441 =head1 GLOBALS
442  
443 =head2 $Bolts::GLOBAL_FALLBACK_META_LOCATOR
444  
445 B<Subject to Change:> This is the name of the locator to use for locating the meta objects needed to configure within Bolts. The default is L<Bolts::Meta::Locator>, which defines the standard set of scopes, blueprints, etc.
446  
447 This variable likely to change or disappear in the future.
448  
449 =for Pod::Coverage contains
450 init_meta
451  
452 =head1 AUTHOR
453  
454 Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
455  
456 =head1 COPYRIGHT AND LICENSE
457  
458 This software is copyright (c) 2014 by Qubling Software LLC.
459  
460 This is free software; you can redistribute it and/or modify it under
461 the same terms as the Perl 5 programming language system itself.
462  
463 =cut
« Bolts/Inference/Moose.pm ↑ Summary Bolts/Blueprint/Given.pm »