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 5     19   74869 use MooseX::DIC::Types;
  5         14  
  5         176  
4 5     5   30 use aliased 'MooseX::DIC::Configuration::ServiceMetadata';
  5         11  
  5         39  
5 5     5   954 use MooseX::DIC::Configuration::ServiceMetadata::Dependency 'from_attribute';
  5         11  
  5         54  
6              
7 5     5   3568 use MooseX::Role::Parameterized;
  5         286381  
  5         24  
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         211 map { ($_->name => from_attribute($_)) }
35 14     14   72 $args{consumer}->get_all_attributes;
        14      
36              
37             return ServiceMetadata->new(
38             class_name => $args{consumer}->{package},
39 14         583 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;