File Coverage

blib/lib/IOC/Service/SetterInjection.pm
Criterion Covered Total %
statement 49 51 96.0
branch 16 16 100.0
condition 9 12 75.0
subroutine 10 10 100.0
pod 1 1 100.0
total 85 90 94.4


line stmt bran cond sub pod time code
1              
2             package IOC::Service::SetterInjection;
3              
4 8     8   65466 use strict;
  8         16  
  8         334  
5 8     8   48 use warnings;
  8         16  
  8         303  
6              
7             our $VERSION = '0.06';
8              
9 8     8   578 use IOC::Exceptions;
  8         14  
  8         185  
10              
11 8     8   40 use base 'IOC::Service';
  8         13  
  8         9977  
12              
13             sub new {
14 17     17 1 17452 my ($_class, $name, $component_class, $component_constructor, $setter_parameters) = @_;
15 17   33     88 my $class = ref($_class) || $_class;
16 17         32 my $service = {};
17 17         45 bless($service, $class);
18 17         66 $service->_init($name, $component_class, $component_constructor, $setter_parameters);
19 12         58 return $service;
20             }
21              
22             sub _init {
23 17     17   91 my ($self, $name, $component_class, $component_constructor, $setter_parameters) = @_;
24 17 100 100     130 (defined($component_class) && defined($component_constructor))
25             || throw IOC::InsufficientArguments "You must provide a class and a constructor method";
26 15 100 100     107 (defined($setter_parameters) && ref($setter_parameters) eq 'ARRAY')
27             || throw IOC::InsufficientArguments "You must provide a set of setter parameters";
28 12         78 $self->{component_class} = $component_class;
29 12         27 $self->{component_constructor} = $component_constructor;
30 12         30 $self->{setter_parameters} = $setter_parameters;
31             $self->SUPER::_init(
32             $name,
33             sub {
34 14     14   121 my $c = shift;
35             # check if there is an entry in the
36             # symbol table for this class already
37             # (meaning it has been loaded) and if
38             # not, then require it
39 14 100   1   104 eval {
  1     1   1016  
  1         11  
  1         16  
  1         881  
  0            
  0            
40 8     8   45 no strict 'refs';
  8         12  
  8         4215  
41             # check for the symbol table itself ...
42             (keys %{"${component_class}::"} ||
43             # and then to be sure, lets look for
44             # either the VERSION or the ISA variables
45             (defined ${"${component_class}::VERSION"}
46 14 100 66     21 || defined @{"${component_class}::ISA"})) ? 1 : 0;
47             } || eval "use $component_class";
48             # throw our exception if the class fails to load
49 14 100       60 throw IOC::ClassLoadingError "The class '$component_class' could not be loaded" => $@ if $@;
50             # check to see if the specified
51             # constructor is there
52 13         131 my $constructor = $component_class->can($component_constructor);
53             # if it is not, then throw our exception
54 13 100       52 (defined($constructor))
55             || throw IOC::ConstructorNotFound "The constructor '$component_constructor' could not be found for class '$component_class'";
56             # now create our instance
57 12         44 my $instance = $component_class->$constructor();
58             # now take care of the parameters
59 12         63 foreach my $setter_param (@{$setter_parameters}) {
  12         29  
60 16         3562 my ($setter_key, $setter_value) = %{$setter_param};
  16         67  
61 16         65 my $setter_method = $instance->can($setter_key);
62 16 100       49 (defined($setter_method))
63             || throw IOC::MethodNotFound "Could not resolve setter method '$setter_key'";
64 15 100       52 if ($setter_value =~ /\//) {
65 5         18 $instance->$setter_method($c->find($setter_value));
66             }
67             else {
68 10         149 $instance->$setter_method($c->get($setter_value));
69             }
70             }
71 11         3102 return $instance;
72             }
73 12         158 );
74             }
75              
76             1;
77              
78             __END__