| 1 | package Bolts::Blueprint::Factory; | ||
| 2 | 10 | $Bolts::Blueprint::Factory::VERSION = '0.143170'; | |
| 3 | # ABSTRACT: Build an artifact by calling a class method on a class | ||
| 4 | |||
| 5 | 10 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 10 | with 'Bolts::Blueprint'; | |
| 8 | |||
| 9 | 10 | ▶ | use Class::Load (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | |||
| 12 | 10 | has class => ( | |
| 13 | is => 'ro', | ||
| 14 | isa => 'Str', | ||
| 15 | required => 1, | ||
| 16 | ); | ||
| 17 | |||
| 18 | |||
| 19 | 10 | has method => ( | |
| 20 | is => 'ro', | ||
| 21 | isa => 'Str', | ||
| 22 | required => 1, | ||
| 23 | default => 'new', | ||
| 24 | ); | ||
| 25 | |||
| 26 | |||
| 27 | sub builder { | ||
| 28 | 224 | ▶ | my ($self, $bag, $name, @params) = @_; |
|
Subroutine
builder: called CC=1 SCAR=0.0
Pod
documented
| |||
| 29 | |||
| 30 | 224 | my $class = $self->class; | |
| 31 | 224 | my $method = $self->method; | |
| 32 | |||
| 33 | 224 | Class::Load::load_class($class); | |
| 34 | |||
| 35 | 224 | return $class->$method(@params); | |
| 36 | } | ||
| 37 | |||
| 38 | 10 | __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.143170 | ||
| 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 | ||