| 1 | package Bread::Board::ConstructorInjection; | ||
| 2 | 63 | our $AUTHORITY = 'cpan:STEVAN'; | |
| 3 | # ABSTRACT: service instantiating objects via a constructor | ||
| 4 | 63 | $Bread::Board::ConstructorInjection::VERSION = '0.35'; | |
| 5 | 63 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 63 | ▶ | use Try::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 63 | ▶ | use Bread::Board::Types; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 63 | with 'Bread::Board::Service::WithConstructor', | |
| 12 | 'Bread::Board::Service::WithParameters', | ||
| 13 | 'Bread::Board::Service::WithDependencies'; | ||
| 14 | |||
| 15 | 63 | has '+class' => (required => 1); | |
| 16 | |||
| 17 | sub get { | ||
| 18 | 161 | ▶ | my $self = shift; |
|
Subroutine
get: called CC=1 SCAR=0.0
| |||
| 19 | |||
| 20 | 161 | my $constructor = $self->constructor_name; | |
| 21 | 161 | $self->class->$constructor( %{ $self->params } ); | |
| 22 | } | ||
| 23 | |||
| 24 | 63 | __PACKAGE__->meta->make_immutable; | |
| 25 | |||
| 26 | 63 | ▶ | no Moose; 1; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | |||
| 28 | __END__ | ||
| 29 | |||
| 30 | =pod | ||
| 31 | |||
| 32 | =encoding UTF-8 | ||
| 33 | |||
| 34 | =head1 NAME | ||
| 35 | |||
| 36 | Bread::Board::ConstructorInjection - service instantiating objects via a constructor | ||
| 37 | |||
| 38 | =head1 VERSION | ||
| 39 | |||
| 40 | version 0.35 | ||
| 41 | |||
| 42 | =head1 DESCRIPTION | ||
| 43 | |||
| 44 | This L<service|Bread::Board::Service> class instantiates objects by | ||
| 45 | calling the constructor on a class. | ||
| 46 | |||
| 47 | This class consumes L<Bread::Board::Service::WithClass>, | ||
| 48 | L<Bread::Board::Service::WithParameters>, | ||
| 49 | L<Bread::Board::Service::WithDependencies>. | ||
| 50 | |||
| 51 | =head1 ATTRIBUTES | ||
| 52 | |||
| 53 | =head2 C<class> | ||
| 54 | |||
| 55 | Attribute provided by L<Bread::Board::Service::WithClass>. This | ||
| 56 | service makes it a required attribute: you can't call a constructor if | ||
| 57 | you don't have a class. | ||
| 58 | |||
| 59 | =head2 C<constructor_name> | ||
| 60 | |||
| 61 | Optional string, indicates the name of the class method to invoke to | ||
| 62 | construct the object. If not provided, defaults to the constructor | ||
| 63 | name obtained via L<Class::MOP::Class>, or C<new> if introspection | ||
| 64 | does not work. | ||
| 65 | |||
| 66 | =head1 METHODS | ||
| 67 | |||
| 68 | =head2 C<get> | ||
| 69 | |||
| 70 | Calls the constructor (as indicated by L</constructor_name>) on the | ||
| 71 | L</class>, passing all the L<service | ||
| 72 | parameters|Bread::Board::Service/params> as a B<hash>. Returns | ||
| 73 | whatever the constructor returned (hopefully a correctly-constructed | ||
| 74 | object of the right class). | ||
| 75 | |||
| 76 | =head1 AUTHOR | ||
| 77 | |||
| 78 | Stevan Little <stevan@iinteractive.com> | ||
| 79 | |||
| 80 | =head1 BUGS | ||
| 81 | |||
| 82 | Please report any bugs or feature requests on the bugtracker website | ||
| 83 | https://github.com/stevan/BreadBoard/issues | ||
| 84 | |||
| 85 | When submitting a bug or request, please include a test-file or a | ||
| 86 | patch to an existing test-file that illustrates the bug or desired | ||
| 87 | feature. | ||
| 88 | |||
| 89 | =head1 COPYRIGHT AND LICENSE | ||
| 90 | |||
| 91 | This software is copyright (c) 2017, 2016, 2015, 2014, 2013, 2011, 2009 by Infinity Interactive. | ||
| 92 | |||
| 93 | This is free software; you can redistribute it and/or modify it under | ||
| 94 | the same terms as the Perl 5 programming language system itself. | ||
| 95 | |||
| 96 | =cut | ||