File Coverage

blib/lib/Bolts/Blueprint/Factory.pm
Criterion Covered Total %
statement 11 11 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod 1 1 100.0
total 15 15 100.0


line stmt bran cond sub pod time code
1             package Bolts::Blueprint::Factory;
2             $Bolts::Blueprint::Factory::VERSION = '0.142930';
3             # ABSTRACT: Build an artifact by calling a class method on a class
4              
5 8     8   33 use Moose;
  8         11  
  8         41  
6              
7             with 'Bolts::Blueprint';
8              
9 8     8   39190 use Class::Load ();
  8         14  
  8         1014  
10              
11              
12             has class => (
13             is => 'ro',
14             isa => 'Str',
15             required => 1,
16             );
17              
18              
19             has method => (
20             is => 'ro',
21             isa => 'Str',
22             required => 1,
23             default => 'new',
24             );
25              
26              
27             sub builder {
28 117     117 1 216 my ($self, $bag, $name, @params) = @_;
29              
30 117         3092 my $class = $self->class;
31 117         2916 my $method = $self->method;
32              
33 117         298 Class::Load::load_class($class);
34              
35 117         106176 return $class->$method(@params);
36             }
37              
38             __PACKAGE__->meta->make_immutable;
39              
40             __END__
41              
42             =pod
43              
44             =encoding UTF-8
45              
46             =head1 NAME
47              
48             Bolts::Blueprint::Factory - Build an artifact by calling a class method on a class
49              
50             =head1 VERSION
51              
52             version 0.142930
53              
54             =head1 SYNOPSIS
55              
56             use Bolts;
57              
58             # Using the usual sugar...
59             artifact thing1 => ( # construct via MyApp::Thing->new(...)
60             class => 'MyApp::Thing',
61             );
62              
63             artifact thing2 => ( # construct via MyApp::Thing->load_standard_thing(...)
64             class => 'MyApp::Thing',
65             method => 'load_standard_thing',
66             );
67              
68             # Or directly...
69             my $meta = Bolts::Bag->start_bag;
70              
71             my $artifact = Bolts::Artifact->new(
72             name => 'thing',
73             blueprint => $meta->locator->acquire('blueprint', 'factory', {
74             class => 'MyApp::Thing',
75             method => 'new',
76             }),
77             scope => $meta->locator->acquire('scope', '_'),
78             );
79              
80             =head1 DESCRIPTION
81              
82             Most applications of Bolts will make a great deal of use of this. It allows you to use a class name and a class method name to construct an object. This is the most straightforward way of constructing things and the way most easily inferred from.
83              
84             =head1 ROLES
85              
86             =over
87              
88             =item *
89              
90             L<Bolts::Blueprint>
91              
92             =back
93              
94             =head1 ATTRIBUTES
95              
96             =head2 class
97              
98             B<Required.> This is the name of the class to call the L</method> upon. This class will also be automatically loaded if it hasn't been loaded into the current Perl interpreter yet.
99              
100             =head2 method
101              
102             This is the method to call on the L</class>. Defaults to "new".
103              
104             =head1 METHODS
105              
106             =head2 builder
107              
108             Loads the L</class> if it has not yet been loaded. Then calls the L</method> on it, passing through to the constructor whatever parameters were configured during pre-injection.
109              
110             =head1 AUTHOR
111              
112             Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
113              
114             =head1 COPYRIGHT AND LICENSE
115              
116             This software is copyright (c) 2014 by Qubling Software LLC.
117              
118             This is free software; you can redistribute it and/or modify it under
119             the same terms as the Perl 5 programming language system itself.
120              
121             =cut