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.143171';
3             # ABSTRACT: Inference engine for Moose classes
4              
5 6     6   4050 use Moose;
  6         12  
  6         39  
6              
7             with 'Bolts::Inference';
8              
9 6     6   29801 use Class::Load ();
  6         12  
  6         73  
10 6     6   20 use Moose::Util ();
  6         7  
  6         1309  
11              
12              
13             sub infer {
14 57     57 1 83 my ($self, $blueprint) = @_;
15              
16 57 50       219 return unless $blueprint->isa('Bolts::Blueprint::Factory');
17              
18 57         1499 my $class = $blueprint->class;
19 57         145 Class::Load::load_class($class);
20              
21 57         105827 my $meta = Moose::Util::find_meta($class);
22              
23 57 50       485 return unless defined $meta;
24              
25 57         61 my @parameters;
26 57         180 ATTR: for my $attr ($meta->get_all_attributes) {
27 377         2623 my ($preferred_injector, $key);
28 377 100       1007 if (defined $attr->init_arg) {
    50          
29 325         266 $preferred_injector = 'parameter_name';
30 325         477 $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 52         468 next ATTR;
38             }
39              
40 325         7160 my $isa = $attr->type_constraint;
41 325 100       8791 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 57         545 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.143171
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