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 4     4   26 use File::Find;
  4         8  
  4         239  
4 4     4   1062 use File::Slurper 'read_text';
  4         38804  
  4         212  
5 4     4   29 use Try::Tiny;
  4         11  
  4         1122  
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 7     7 0 17 my $path = shift;
13              
14 7         17 my @injectable_packages = ();
15             find(
16             sub {
17 48     48   111 my $file_name = $File::Find::name;
18 48 100       112 push @injectable_packages,
19             extract_package_name_from_filename($file_name)
20             if is_injectable($file_name);
21             },
22 7         707 @$path
23             );
24              
25 7         52 return @injectable_packages;
26             }
27              
28             sub is_injectable {
29 48     48 0 79 my $file_name = shift;
30              
31             # Must be a file
32 48 100       1180 return 0 unless -f $file_name;
33              
34             # Must be a perl module
35 39 100       167 return 0 if index( $file_name, '.pm' ) == -1;
36              
37             # Must have the injectable role applied
38 35     35   195 my $file_content = try { read_text( $file_name ) };
  35         971  
39 35 100 66     3069 return 0
40             unless ( $file_content
41             and ( index( $file_content, 'MooseX::DIC::Injectable' ) != -1 ) );
42              
43 12         52 return 1;
44             }
45              
46             # This method assumes there's only one package per file
47             sub extract_package_name_from_filename {
48 12     12 0 25 my $file_name = shift;
49              
50 12         19 my $package_name;
51 12     12   53 my $file_content = try { read_text( $file_name ) };
  12         278  
52 12 50       789 if ($file_content) {
53 12         81 $file_content =~ /package\ +([a-zA-Z0-9]+)/;
54 12         38 $package_name = $1;
55             }
56              
57 12         152 return $package_name;
58             }
59              
60             1;