File Coverage

blib/lib/Bolts/Injector/Setter.pm
Criterion Covered Total %
statement 14 14 100.0
branch 1 2 50.0
condition 1 3 33.3
subroutine 5 5 100.0
pod 1 1 100.0
total 22 25 88.0


line stmt bran cond sub pod time code
1             package Bolts::Injector::Setter;
2             $Bolts::Injector::Setter::VERSION = '0.143171';
3             # ABSTRACT: Inject by calling a setter method with a value
4              
5 1     1   559 use Moose;
  1         2  
  1         6  
6              
7             with 'Bolts::Injector';
8              
9 1     1   4461 use Carp ();
  1         2  
  1         12  
10 1     1   3 use Scalar::Util;
  1         1  
  1         194  
11              
12              
13             has name => (
14             is => 'ro',
15             isa => 'Str',
16             lazy_build => 1,
17             );
18              
19 1     1   21 sub _build_name { $_[0]->key }
20              
21              
22             sub post_inject_value {
23 2     2 1 3 my ($self, $loc, $value, $object) = @_;
24              
25 2 50 33     12 Carp::croak(qq[Can't use setter injection on "$object".])
26             unless defined $object and Scalar::Util::blessed($object);
27              
28 2         52 my $name = $self->name;
29 2         41 $object->$name($value);
30             }
31              
32             __PACKAGE__->meta->make_immutable;
33              
34             __END__
35              
36             =pod
37              
38             =encoding UTF-8
39              
40             =head1 NAME
41              
42             Bolts::Injector::Setter - Inject by calling a setter method with a value
43              
44             =head1 VERSION
45              
46             version 0.143171
47              
48             =head1 SYNOPSIS
49              
50             use Bolts;
51              
52             artifact thing => (
53             class => 'MyApp::Thing',
54             setters => {
55             set_foo => dep('other_thing'),
56             },
57             );
58              
59             =head1 DESCRIPTION
60              
61             This controls injection by setter, which causes a method to be called on the constructed artifact with the value to be injected.
62              
63             =head1 ROLES
64              
65             =over
66              
67             =item *
68              
69             L<Bolts::Injector>
70              
71             =back
72              
73             =head1 ATTRIBUTES
74              
75             =head2 name
76              
77             This is the name of the method to call during injection. It defaults to L<Bolts::Injector/key>.
78              
79             =head1 METHODS
80              
81             =head2 post_inject_value
82              
83             Performs the injection into the setter.
84              
85             =head1 AUTHOR
86              
87             Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
88              
89             =head1 COPYRIGHT AND LICENSE
90              
91             This software is copyright (c) 2014 by Qubling Software LLC.
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