File Coverage

blib/lib/Sweet/Dir.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Sweet::Dir;
2 5     5   81554 use Moose;
  0            
  0            
3             use namespace::autoclean;
4              
5             use Try::Tiny;
6              
7             use MooseX::Types::Path::Class;
8             use File::Path qw(make_path remove_tree);
9             use Try::Tiny;
10              
11             use Sweet::File;
12              
13             has path => (
14             builder => '_build_path',
15             coerce => 1,
16             is => 'ro',
17             isa => 'Path::Class::Dir',
18             lazy => 1,
19             );
20              
21             sub create {
22             my $self = shift;
23             my $path = $self->path;
24             my $make_path_error;
25              
26             return $self if $self->is_a_directory;
27              
28             try {
29             make_path( $path, { error => \$make_path_error } );
30             }
31             catch {
32             die $make_path_error;
33             };
34             }
35              
36             sub does_not_exists { !-d shift->path }
37              
38             sub erase {
39             my $self = shift;
40              
41             my $path = $self->path;
42             my $remove_path_error;
43              
44             try {
45             remove_path( $path, { error => \$remove_path_error } );
46             }
47             catch {
48             die $remove_path_error;
49             };
50             }
51              
52             sub file {
53             my $self = shift;
54              
55             my $name = shift;
56              
57             my $file = Sweet::File->new( dir => $self, name => $name );
58              
59             return $file;
60             }
61              
62             sub is_a_directory { -d shift->path }
63              
64             sub sub_dir {
65             my $self = shift;
66              
67             my @path;
68              
69             if ( scalar(@_) == 1 and ref $_[0] eq 'ARRAY' ) {
70             @path = @{ $_[0] };
71             }
72             else {
73             @path = @_;
74             }
75              
76             my $sub_dir_path = File::Spec->catfile( $self->path, @path );
77              
78             my $sub_dir = Sweet::Dir->new( path => $sub_dir_path );
79              
80             return $sub_dir;
81             }
82              
83             use overload q("") => sub { shift->path };
84              
85             __PACKAGE__->meta->make_immutable;
86              
87             1;
88              
89             __END__
90              
91             =encoding utf8
92              
93             =head1 NAME
94              
95             Sweet::Dir
96              
97             =head1 SYNOPSIS
98              
99             use Sweet::Dir;
100              
101             my $dir = Sweet::Dir->new(path => '/path/to/dir');
102             $dir->create;
103              
104              
105             say $dir; # /path/to/dir
106              
107             =head1 ATTRIBUTES
108              
109             =head2 path
110              
111             =head1 METHODS
112              
113             =head2 create
114              
115             $dir->create;
116              
117             =head2 does_not_exists
118              
119             $dir->create if $dir->does_not_exists;
120              
121             =head2 erase
122              
123             $dir->erase;
124              
125             =head2 file
126              
127             Instance of file inside dir. Returns a L<Sweet::File>.
128              
129             my $file = $dir->file('foo.txt');
130             say $file; # /path/to/dir/foo.txt
131              
132             =head2 is_a_directory
133              
134             # Create dir if it does not exists.
135             $dir->is_a_directory or $dir->create;
136              
137             =head2 sub_dir
138              
139             my $dir2 = $dir->sub_dir('foo', bar');
140             # Or pass an arrayref if you prefer.
141             # my $dir2 = $dir->sub_dir(['foo', bar']);
142              
143             # Create foo/bar sub directory.
144             $dir2->create;
145              
146             =cut
147