File Coverage

lib/MooseX/DIC/Configuration/Scanner/Injectable.pm
Criterion Covered Total %
statement 30 30 100.0
branch 9 10 90.0
condition 2 3 66.6
subroutine 9 9 100.0
pod 0 3 0.0
total 50 55 90.9


line stmt bran cond sub pod time code
1             package MooseX::DIC::Configuration::Scanner::Injectable;
2              
3 5     5   37 use File::Find;
  5         11  
  5         369  
4 5     5   1640 use File::Slurper 'read_text';
  5         61237  
  5         318  
5 5     5   47 use Try::Tiny;
  5         16  
  5         1713  
6              
7             require Exporter;
8             @ISA = qw/Exporter/;
9             @EXPORT_OK = qw/fetch_injectable_packages_from_path/;
10              
11             sub fetch_injectable_packages_from_path {
12 8     8 0 54 my $path = shift;
13              
14 8         30 my @injectable_packages = ();
15             find(
16             sub {
17 55     55   304 my $file_name = $File::Find::name;
18 55 100       167 push @injectable_packages,
19             extract_package_name_from_filename($file_name)
20             if is_injectable($file_name);
21             },
22 8         941 @$path
23             );
24              
25 8         75 return @injectable_packages;
26             }
27              
28             sub is_injectable {
29 55     55 0 140 my $file_name = shift;
30              
31             # Must be a file
32 55 100       1744 return 0 unless -f $file_name;
33              
34             # Must be a perl module
35 45 100       321 return 0 if index( $file_name, '.pm' ) == -1;
36              
37             # Must have the injectable role applied
38 41     41   330 my $file_content = try { read_text( $file_name ) };
  41         1591  
39 41 100 66     4916 return 0
40             unless ( $file_content
41             and ( index( $file_content, 'MooseX::DIC::Injectable' ) != -1 ) );
42              
43 14         84 return 1;
44             }
45              
46             # This method assumes there's only one package per file
47             sub extract_package_name_from_filename {
48 14     14 0 85 my $file_name = shift;
49              
50 14         35 my $package_name;
51 14     14   91 my $file_content = try { read_text( $file_name ) };
  14         509  
52 14 50       1387 if ($file_content) {
53 14         131 $file_content =~ /package\ +([a-zA-Z0-9]+)/;
54 14         61 $package_name = $1;
55             }
56              
57 14         228 return $package_name;
58             }
59              
60             1;