File Coverage

blib/lib/Locale/Maketext/Lexicon/Slurp.pm
Criterion Covered Total %
statement 59 62 95.1
branch 23 28 82.1
condition 7 9 77.7
subroutine 9 9 100.0
pod 4 4 100.0
total 102 112 91.0


line stmt bran cond sub pod time code
1             package Locale::Maketext::Lexicon::Slurp;
2              
3 1     1   27488 use strict;
  1         4  
  1         61  
4 1     1   8 use warnings;
  1         4  
  1         45  
5              
6 1     1   8 use File::Basename ();
  1         9  
  1         36  
7 1     1   979 use Path::Class;
  1         68216  
  1         766  
8              
9             our $VERSION = "0.01";
10             our $AUTHORITY = 'NUFFIN';
11              
12             sub read_file {
13 11     11 1 52 my ( $self, %args ) = @_;
14              
15 11         34 local $/;
16              
17 11         19 my $file = $args{path};
18              
19 11 50       508 open my $fh, "<", $file or die "open($file): $!";
20              
21 11 50       30 binmode $fh, $args{binmode} if exists $args{binmode};
22              
23 11         487 scalar(<$fh>);
24             }
25              
26             sub readdir {
27 2     2 1 4 my ( $self, $dir ) = @_;
28 2         8 grep { -f $_ } $dir->children;
  6         1245  
29             }
30              
31             sub get_files {
32 9     9 1 15 my ( $self, $args ) = @_;
33              
34 9         12 my @files;
35 9 100 100     159 my $dir = $args->{dir} && -d $args->{dir} ? Path::Class::dir($args->{dir}) : undef;
36              
37 9 100       666 if ( my $files = $args->{files} ) {
    100          
    50          
38 4 100       11 if ( $dir ) {
39 3 100 100     27 if ( (ref($files)||'') ne 'ARRAY' ) {
40 2         9 @files = map { Path::Class::file($_) } glob( $dir->file($files)->stringify );
  2         376  
41             } else {
42 1         2 @files = map { $dir->file($_) } @$files;
  2         119  
43             }
44             } else {
45 1 50 50     9 if ( (ref($files)||'') ne 'ARRAY' ) {
46 0         0 @files = map { Path::Class::file($_) } glob($files);
  0         0  
47             } else {
48 1         4 @files = @$files;
49             }
50             }
51             } elsif ( $dir ) {
52 2   50     16 my $readdir = $args->{readdir} || "readdir";
53 2         8 @files = $self->$readdir( $dir );
54             } elsif ( $args->{dir} ) { # not a directory, special case for 1 arg form
55 3         65 @files = map { Path::Class::file($_) } glob($args->{dir});
  3         292  
56             }
57              
58 9 100       619 if ( my $re = $args->{regex} ) {
59 2         4 @files = grep { $_ =~ $re } @files;
  6         126  
60             }
61              
62 9 100       81 if ( my $filter = $args->{filter} ) {
63 1         3 @files = grep { $self->$filter( $_ ) } @files;
  2         9  
64             }
65              
66 9 50       25 if ( @files ) {
67 9 100       17 if ( $dir ) {
68 5         28 my $dir_obj = Path::Class::dir($dir);
69 5         212 return { map { Path::Class::file($_)->relative( $dir_obj )->stringify => "$_" } @files },
  8         997  
70             } else {
71 4         6 return { map { File::Basename::basename($_) => "$_" } @files };
  5         163  
72             }
73             }
74              
75 0         0 die "No files specified";
76             }
77              
78             sub parse {
79 9     9 1 11730 my ( $self, @args ) = @_;
80              
81 9 100       39 unshift @args, "dir" if @args % 2 == 1; # work in Lexicon's * mode
82              
83 9         29 my $args = { @args };
84              
85 9         34 my $files = $self->get_files( $args );
86              
87             return {
88 13         50 map {
89 9         1735 my $name = $_;
90 13         19 my $path = $files->{$name};
91             $name => sub {
92 11     11   5902 return $self->read_file(
93             %$args,
94             path => $path,
95             name => $name,
96             args => \@_
97             )
98             }
99 13         125 } keys %$files
100             };
101             }
102              
103             1;
104              
105             __END__