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   36 use File::Find;
  5         16  
  5         369  
4 5     5   1558 use File::Slurper 'read_text';
  5         57576  
  5         318  
5 5     5   48 use Try::Tiny;
  5         14  
  5         1625  
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 22 my $path = shift;
13              
14 8         54 my @injectable_packages = ();
15             find(
16             sub {
17 55     55   139 my $file_name = $File::Find::name;
18 55 100       112 push @injectable_packages,
19             extract_package_name_from_filename($file_name)
20             if is_injectable($file_name);
21             },
22 8         980 @$path
23             );
24              
25 8         65 return @injectable_packages;
26             }
27              
28             sub is_injectable {
29 55     55 0 99 my $file_name = shift;
30              
31             # Must be a file
32 55 100       1496 return 0 unless -f $file_name;
33              
34             # Must be a perl module
35 45 100       170 return 0 if index( $file_name, '.pm' ) == -1;
36              
37             # Must have the injectable role applied
38 41     41   218 my $file_content = try { read_text( $file_name ) };
  41         1394  
39 41 100 66     3859 return 0
40             unless ( $file_content
41             and ( index( $file_content, 'MooseX::DIC::Injectable' ) != -1 ) );
42              
43 14         89 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 32 my $file_name = shift;
49              
50 14         26 my $package_name;
51 14     14   62 my $file_content = try { read_text( $file_name ) };
  14         338  
52 14 50       958 if ($file_content) {
53 14         104 $file_content =~ /package\ +([a-zA-Z0-9]+)/;
54 14         40 $package_name = $1;
55             }
56              
57 14         165 return $package_name;
58             }
59              
60             1;