Dir.pm

filter: Statementstmt 91.6% 22 / 24 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.7% 30 / 32 CC 1 SCAR 0.4
CC
1
Cov
92%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Types.pm ↑ Summary
1 package Sweet::Dir;
2 6 ▶use latest;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use Sweet::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use File::Path qw(make_path remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use MooseX::AttributeShortcuts;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use MooseX::Types::Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use Sweet::File;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use Try::Tiny;
13  
14 use namespace::autoclean;
15  
16 has path => (
17 coerce => 1,
18 is => 'lazy',
19 isa => 'Path::Class::Dir',
20 );
21  
22 sub create {
23 my $self = shift;
24  
25 my $path = $self->path;
26  
27 my $make_path_error;
28  
29 return $self if $self->is_a_directory;
30  
31 try {
32 make_path( $path, { error => \$make_path_error } );
33 }
34 catch {
35 croack $make_path_error;
36 };
37 }
38  
39 sub does_not_exists { !-d shift->path }
40  
41 sub erase {
42 my $self = shift;
43  
44 my $path = $self->path;
45 my $remove_path_error;
46  
47 try {
48 remove_path( $path, { error => \$remove_path_error } );
49 }
50 catch {
51 croack $remove_path_error;
52 };
53 }
54  
55 sub file {
56 my ( $self, $name, $builder ) = @_;
57  
58 my $default_builder = sub {
59 my ( $dir, $name ) = @_;
60  
61 my $file = Sweet::File->new(
62 dir => $dir,
63 name => $name
64 );
65  
66 return $file;
67 };
68  
69 $builder = $default_builder if (not defined $builder);
70  
71 my $file = try {
72 $builder->( $self, $name );
73 }
74 catch {
75 croak $_;
76 };
77  
78 return $file;
79 }
80  
81 sub file_list {
82 my ( $self, $regexp ) = @_;
83  
84 my $path = $self->path;
85  
86 opendir my ($dir), $path or croak "Could not open $path: $!";
87 my @file_names = grep { not m/ ^ \.\.? $ /x } readdir $dir;
88 closedir $dir;
89  
90 return @file_names if not defined $regexp;
91  
92 return grep { m/$regexp/ } @file_names;
93 }
94  
95 sub is_a_directory { -d shift->path }
96  
97 sub sub_dir {
98 my $self = shift;
99  
100 my @path;
101  
102 if ( scalar(@_) == 1 and ref $_[0] eq 'ARRAY' ) {
103 @path = @{ $_[0] };
104 }
105 else {
106 @path = @_;
107 }
108  
109 my $sub_dir_path = File::Spec->catfile( $self->path, @path );
110  
111 my $sub_dir = Sweet::Dir->new( path => $sub_dir_path );
112  
113 return $sub_dir;
114 }
115  
116 use overload q("") => sub { shift->path }, bool => sub { 1 }, fallback => 1;
117  
118 __PACKAGE__->meta->make_immutable;
119  
120 __END__
« Types.pm ↑ Summary