File Coverage

blib/lib/Bolts/Inference/Moose.pm
Criterion Covered Total %
statement 25 27 92.5
branch 7 10 70.0
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 37 42 88.1


line stmt bran cond sub pod time code
1             package Bolts::Inference::Moose;
2             $Bolts::Inference::Moose::VERSION = '0.142930';
3             # ABSTRACT: Inference engine for Moose classes
4              
5 6     6   4269 use Moose;
  6         11  
  6         39  
6              
7             with 'Bolts::Inference';
8              
9 6     6   30919 use Class::Load ();
  6         12  
  6         87  
10 6     6   25 use Moose::Util ();
  6         7  
  6         1159  
11              
12              
13             sub infer {
14 31     31 1 36 my ($self, $blueprint) = @_;
15              
16 31 50       129 return unless $blueprint->isa('Bolts::Blueprint::Factory');
17              
18 31         814 my $class = $blueprint->class;
19 31         90 Class::Load::load_class($class);
20              
21 31         108057 my $meta = Moose::Util::find_meta($class);
22              
23 31 50       287 return unless defined $meta;
24              
25 31         38 my @parameters;
26 31         107 ATTR: for my $attr ($meta->get_all_attributes) {
27 198         1624 my ($preferred_injector, $key);
28 198 100       572 if (defined $attr->init_arg) {
    50          
29 172         153 $preferred_injector = 'parameter_name';
30 172         245 $key = $attr->init_arg;
31             }
32             elsif ($attr->has_write_method) {
33 0         0 $preferred_injector = 'setter';
34 0         0 $key = $attr->get_write_method;
35             }
36             else {
37 26         254 next ATTR;
38             }
39              
40 172         3907 my $isa = $attr->type_constraint;
41 172 100       4887 push @parameters, {
42             key => $key,
43             inject_via => [ 'injector', $preferred_injector ],
44             (defined $isa ? (isa => $isa) : ()),
45             required => $attr->is_required,
46             };
47             }
48              
49 31         258 return @parameters;
50             }
51              
52             __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.142930
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