| 1 | package Bolts::Inference::Moose; | ||||||||||||||
| 2 | 6 | $Bolts::Inference::Moose::VERSION = '0.143170'; | |||||||||||||
| 3 | # ABSTRACT: Inference engine for Moose classes | ||||||||||||||
| 4 | |||||||||||||||
| 5 | 6 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 6 | with 'Bolts::Inference'; | |||||||||||||
| 8 | |||||||||||||||
| 9 | 6 | ▶ | use Class::Load (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 6 | ▶ | use Moose::Util (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | |||||||||||||||
| 13 | sub infer { | ||||||||||||||
| 14 | 57 | ▶ | my ($self, $blueprint) = @_; | ||||||||||||
|
Subroutine
infer: called CC=7 SCAR=19.8
Pod
documented
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 57 | ▶ | return unless $blueprint->isa('Bolts::Blueprint::Factory'); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | 57 | my $class = $blueprint->class; | |||||||||||||
| 19 | 57 | Class::Load::load_class($class); | |||||||||||||
| 20 | |||||||||||||||
| 21 | 57 | my $meta = Moose::Util::find_meta($class); | |||||||||||||
| 22 | |||||||||||||||
| 23 | 57 | ▶ | return unless defined $meta; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 24 | |||||||||||||||
| 25 | 57 | my @parameters; | |||||||||||||
| 26 | 57 | ATTR: for my $attr ($meta->get_all_attributes) { | |||||||||||||
| 27 | 377 | my ($preferred_injector, $key); | |||||||||||||
| 28 | 377 | ▶ | if (defined $attr->init_arg) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 29 | 325 | $preferred_injector = 'parameter_name'; | |||||||||||||
| 30 | 325 | $key = $attr->init_arg; | |||||||||||||
| 31 | } | ||||||||||||||
| 32 | elsif ($attr->has_write_method) { | ||||||||||||||
| 33 | 0 | $preferred_injector = 'setter'; | |||||||||||||
| 34 | 0 | $key = $attr->get_write_method; | |||||||||||||
| 35 | } | ||||||||||||||
| 36 | else { | ||||||||||||||
| 37 | 52 | next ATTR; | |||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | 325 | my $isa = $attr->type_constraint; | |||||||||||||
| 41 | 325 | ▶ | push @parameters, { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | key => $key, | ||||||||||||||
| 43 | inject_via => [ 'injector', $preferred_injector ], | ||||||||||||||
| 44 | (defined $isa ? (isa => $isa) : ()), | ||||||||||||||
| 45 | required => $attr->is_required, | ||||||||||||||
| 46 | }; | ||||||||||||||
| 47 | } | ||||||||||||||
| 48 | |||||||||||||||
| 49 | 57 | return @parameters; | |||||||||||||
| 50 | } | ||||||||||||||
| 51 | |||||||||||||||
| 52 | 6 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 53 | |||||||||||||||
| 54 | __END__ | ||||||||||||||
| 55 | |||||||||||||||
| 56 | =pod | ||||||||||||||
| 57 | |||||||||||||||
| 58 | =encoding UTF-8 | ||||||||||||||
| 59 | |||||||||||||||
| 60 | =head1 NAME | ||||||||||||||
| 61 | |||||||||||||||
| 62 | Bolts::Inference::Moose - Inference engine for Moose classes | ||||||||||||||
| 63 | |||||||||||||||
| 64 | =head1 VERSION | ||||||||||||||
| 65 | |||||||||||||||
| 66 | version 0.143170 | ||||||||||||||
| 67 | |||||||||||||||
| 68 | =head1 SYNOPSIS | ||||||||||||||
| 69 | |||||||||||||||
| 70 | package MyApp::Thing; | ||||||||||||||
| 71 | use Moose; | ||||||||||||||
| 72 | |||||||||||||||
| 73 | has id => ( is => 'ro' ); | ||||||||||||||
| 74 | has name => ( is => 'ro' ); | ||||||||||||||
| 75 | |||||||||||||||
| 76 | package MyApp::Settings; | ||||||||||||||
| 77 | use Bolts; | ||||||||||||||
| 78 | |||||||||||||||
| 79 | artifact thing => ( | ||||||||||||||
| 80 | class => 'MyApp::Thing', | ||||||||||||||
| 81 | infer => 'options', | ||||||||||||||
| 82 | ); | ||||||||||||||
| 83 | |||||||||||||||
| 84 | package MyApp; | ||||||||||||||
| 85 | |||||||||||||||
| 86 | my $settings = MyApp::Settings->new; | ||||||||||||||
| 87 | my $thing = $settings->acquire('thing', { | ||||||||||||||
| 88 | id => 1, | ||||||||||||||
| 89 | name => 'something', | ||||||||||||||
| 90 | }); # works! | ||||||||||||||
| 91 | |||||||||||||||
| 92 | =head1 DESCRIPTION | ||||||||||||||
| 93 | |||||||||||||||
| 94 | Performs inferrence for L<Moose> object constructor injection on | ||||||||||||||
| 95 | L<Bolts::Blueprint::Factory>. That is, it is the way in which Bolts will | ||||||||||||||
| 96 | automatically guess how to build your Moose objects, provided you construct them | ||||||||||||||
| 97 | with L<Bolts::Blueprint::Factory> (see the L</SYNOPSIS> for an example). | ||||||||||||||
| 98 | |||||||||||||||
| 99 | This works by iterating through the attributes on the metaclass for the Moose | ||||||||||||||
| 100 | object set on the L<Bolts::Blueprint::Factory/class> of the blueprint. If the | ||||||||||||||
| 101 | attribute has an C<init_arg> set (which all do by default to a name matching the | ||||||||||||||
| 102 | attribute name), then the dependency will be passed to the constructor as a | ||||||||||||||
| 103 | parameter. If the C<init_arg> is undefined, but a setter is provided (i.e., you | ||||||||||||||
| 104 | use C<< isa => 'rw' >> or use C<< writer => 'set_attr' >> or C<< accessor => | ||||||||||||||
| 105 | 'attr' >> when setting up the attribute), the setter will be used for that | ||||||||||||||
| 106 | dependency instead. If neither a setter or an C<init_arg> is defined, then the | ||||||||||||||
| 107 | attribute will be skipped for injection. | ||||||||||||||
| 108 | |||||||||||||||
| 109 | =head1 ROLES | ||||||||||||||
| 110 | |||||||||||||||
| 111 | =over | ||||||||||||||
| 112 | |||||||||||||||
| 113 | =item * | ||||||||||||||
| 114 | |||||||||||||||
| 115 | L<Bolts::Inferrence> | ||||||||||||||
| 116 | |||||||||||||||
| 117 | =back | ||||||||||||||
| 118 | |||||||||||||||
| 119 | =head1 METHODS | ||||||||||||||
| 120 | |||||||||||||||
| 121 | =head2 infer | ||||||||||||||
| 122 | |||||||||||||||
| 123 | This implements the inferrence described in L</DESCRIPTION>. | ||||||||||||||
| 124 | |||||||||||||||
| 125 | =head1 AUTHOR | ||||||||||||||
| 126 | |||||||||||||||
| 127 | Andrew Sterling Hanenkamp <hanenkamp@cpan.org> | ||||||||||||||
| 128 | |||||||||||||||
| 129 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 130 | |||||||||||||||
| 131 | This software is copyright (c) 2014 by Qubling Software LLC. | ||||||||||||||
| 132 | |||||||||||||||
| 133 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 134 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||
| 135 | |||||||||||||||
| 136 | =cut | ||||||||||||||