File Coverage

blib/lib/DCI/Context.pm
Criterion Covered Total %
statement 34 34 100.0
branch 6 10 60.0
condition 2 6 33.3
subroutine 6 6 100.0
pod 2 2 100.0
total 50 58 86.2


line stmt bran cond sub pod time code
1             package DCI::Context;
2 15     15   87 use strict;
  15         27  
  15         586  
3 15     15   85 use warnings;
  15         33  
  15         421  
4              
5 15     15   78 use Carp();
  15         30  
  15         7049  
6              
7             sub import {
8 20     20   204 my $class = shift;
9 20         73 my $caller = caller;
10              
11 20 50       556 $class->before_import( $class, $caller, \@_ )
12             if $class->can( 'before_import' );
13              
14 20         153 $class->dci_export_to( $caller, @_ );
15              
16 20 50       457 $class->after_import_import( $class, $caller, @_ )
17             if $class->can( 'after_import' );
18             }
19              
20             sub dci_export_to {
21 20     20 1 32 my $class = shift;
22 20         59 my ( $caller, @export_list ) = @_;
23              
24 20         79 my %exports = $class->dci_meta->dci_exports;
25 20 100       97 @export_list = keys %exports unless @export_list;
26              
27 20         52 for my $name ( @export_list ) {
28 32   33     98 my $sub = $exports{ $name } || Carp::croak "$class does not export '$name'";
29 32         121 $class->dci_meta->inject( $caller, $name => $sub );
30             }
31             }
32              
33             sub new {
34 32     32 1 7694 my $class = shift;
35 32         281 my %args = @_;
36              
37 32         225 my $self = bless {}, $class;
38              
39 32         858 my %roles = map {( $_ => 1 )} $class->dci_meta->roles;
  76         263  
40 32         322 my %maybe = map {( $_ => 1 )} $class->dci_meta->maybe_roles;
  24         281  
41 32         163 for my $arg ( keys %args ) {
42 76 50 33     672 Carp::croak( "$class has no role '$arg' to fill" )
43             unless $roles{$arg} || $maybe{$arg};
44              
45 76         206 delete $roles{$arg};
46              
47             # The method takes care of applying the cast.
48 76         467 $self->$arg( $args{$arg} );
49             }
50              
51             # Ensure all roles are filled.
52 32 50       148 Carp::croak( "No objects provided for roles: " . join( ", ", keys %roles ))
53             if keys %roles;
54              
55 32         208 return $self;
56             }
57              
58             1;
59              
60             __END__