File Coverage

lib/MooseX/DIC/Injectable.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 21 21 100.0


line stmt bran cond sub pod time code
1             package MooseX::DIC::Injectable;
2              
3 4     18   55155 use MooseX::DIC::Types;
  4         9  
  4         121  
4 4     4   19 use aliased 'MooseX::DIC::Configuration::ServiceMetadata';
  4         33  
  4         29  
5 4     4   615 use MooseX::DIC::Configuration::ServiceMetadata::Dependency 'from_attribute';
  4         9  
  4         33  
6              
7 4     4   2410 use MooseX::Role::Parameterized;
  4         203775  
  4         17  
8              
9             parameter scope => ( isa => 'ServiceScope', default => 'singleton' );
10             parameter environment => ( isa => 'Str', default => 'default' );
11             parameter implements => ( isa => 'Str', predicate => 'has_implements' );
12             parameter qualifiers => ( isa => 'ArrayRef[Str]', default => sub { [] } );
13             parameter builder => ( isa => 'ServiceBuilder', default => 'Moose' );
14              
15             role {
16             my ( $p, %args ) = @_;
17              
18             # If this injectable is a factory, it must provide the build_service
19             # method so that the container can use it.
20             # The build_service will receive:
21             # - the service metadata object
22             # - the container
23             # - injection point metadata
24             if ( $p->builder eq 'Factory' ) {
25             requires 'build_service';
26             }
27              
28              
29             # Inject in the package metadata the mooseX metadata
30             $args{consumer}->add_method(
31             get_service_metadata => sub {
32             # Prepare dependencies metadata
33             my %dependencies =
34 4         165 map { ($_->name => from_attribute($_)) }
35 12     12   51 $args{consumer}->get_all_attributes;
        12      
36              
37             return ServiceMetadata->new(
38             class_name => $args{consumer}->{package},
39 12         465 scope => $p->scope,
40             environment => $p->environment,
41             qualifiers => $p->qualifiers,
42             implements => $p->implements,
43             builder => $p->builder,
44             dependencies => \%dependencies
45             );
46             }
47             );
48             };
49              
50             1;