Model/File.pm

filter: Statementstmt 90.6% 116 / 128 Branchbran 71.7% 33 / 46 Conditioncond 70.0% 14 / 20 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 87.8% 29 / 33 Podpod 81.8% 9 / 11 totaltotal 82.3% 205 / 249 CC 32 SCAR 15.1
CC
32
Cov
84%
slurp
10.4
list
9.1
change_dir
8.1
CRAP 4.5
↑ Summary Helper/Model/File.pm »
1 package Catalyst::Model::File;
2  
3 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 extends 'Catalyst::Model';
7 5 with 'Catalyst::Component::InstancePerContext';
8  
9  
10 5 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use MooseX::Types::Path::Class qw/ Dir /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use MooseX::Types::Moose qw/ Str /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use IO::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Path::Class ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 our $VERSION = '0.10';
19  
20 =head1 NAME
21  
22 Catalyst::Model::File - File based storage model for Catalyst.
23  
24 =head1 SYNOPSIS
25  
26 # use the helper to create a model
27 myapp_create.pl model File File
28  
29 # configure in lib/MyApp.pm
30  
31 MyApp->config(
32 name => 'MyApp',
33 root => MyApp->path_to('root'),
34 'Model::File' => {
35 root_dir => MyApp->path_to('file_store')
36 },
37 );
38  
39 Simple file based storage model for Catalyst.
40  
41 @file_names = $c->model('File')->list;
42  
43 =head1 METHODS
44  
45 =cut
46  
47 5 has root_dir => (
48 isa => Dir,
49 is => 'ro',
50 coerce => 1,
51 required => 1,
52 );
53  
54 has _dir => (
55 isa => Dir,
56 is => 'rw',
57 lazy => 1,
58 10 ▶ default => sub { shift()->root_dir },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 5 clearer => '_clear_dir',
60 );
61  
62 5 has dir_create_mask => (
63 isa => Str,
64 is => 'ro',
65 default => '0775',
66 );
67  
68 has _directory => (
69 isa => Dir,
70 is => 'rw',
71 4 ▶ default => sub { Path::Class::dir('/') },
Subroutine __ANON__: called CC=1 SCAR=0.0
72 5 );
73  
74 4 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
75 4 my $self = shift;
76 4 mkdir($self->root_dir, oct($self->dir_create_mask));
77 }
78  
79 5 ▶sub build_per_context_instance {
Subroutine build_per_context_instance: called CC=1 SCAR=0.0
Pod undocumented
80 5 my ($self, $c) = @_;
81  
82 5 $self->cd('/');
83  
84 5 return $self;
85 }
86  
87 =head2 list
88  
89 Returns a list of files (and/or directories) found under the current working
90 dir. Default will return files (including those found under sub-directories)
91 but not directories.
92  
93 To change this behaviour specify a C<mode> param of C<files> (default),
94 C<dirs> or C<both>:
95  
96 $mdl->list(mode => 'both')
97  
98 To only get files/dirs directly under the current dir specify a C<recurse>
99 option of 0.
100  
101 Please note: the exact order in which files and directories are listed will
102 change from OS to OS.
103  
104 =cut
105  
106 7 ▶sub list {
Subroutine list: called CC=9 SCAR=22.0
Pod documented
107 7 my ($self, %opt) = @_;
108 7 my @files;
109 7 ▶ $opt{mode} ||= 'files';
Condition100%2/2
$opt{"mode"} ||= "files"
l!l
43
MC/DC100%1/1
$opt{"mode"} ||= "files"
$opt{"mode"}
Truth table100%2/2
A
A= $opt{"mode"}
Aresult
11
00
110 7 ▶ $opt{recurse} = 1 unless exists $opt{recurse};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opt{"recurse"} 4 3 7
111  
112 7 ▶ $opt{dir} = 1 if $opt{mode} =~ /^both|dirs$/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"mode"} =~ /^both|dirs$/ 2 5 7
113 7 ▶ $opt{file} = 1 if $opt{mode} =~ /^both|files$/;
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"mode"} =~ /^both|files$/ 6 1 7
114  
115 7 ▶ if ($opt{recurse}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"recurse"}) 4 3 7
116 11 ▶ $self->_dir->recurse(callback => sub {
Subroutine __ANON__: called CC=5 SCAR=17.5
117 11 my ($entry) = @_;
118 push @files, $entry
119 if !$entry->is_dir && $opt{file}
120 11 ▶ || $entry->is_dir && $opt{dir};
Branch: 1 branch
BranchTrueFalseTotal
if not $entry->is_dir and $opt{"file"} or $entry->is_dir and $opt{"dir"} 5 6 11
Condition55%5/9
not $entry->is_dir and $opt{"file"} or $entry->is_dir and $opt{"dir"}
l!l&&r!l&&!r
506
not $entry->is_dir and $opt{"file"}
!ll&&!rl&&r
605
$entry->is_dir and $opt{"dir"}
!ll&&!rl&&r
060
MC/DC25%1/4
not $entry->is_dir and $opt{"file"} or $entry->is_dir and $opt{"dir"}
not $entry->is_dir $opt{"file"} $entry->is_dir $opt{"dir"}
Truth table28%2/7
not(A) and B or C and D
A= not $entry->is_dirB= $opt{"file"}C= $entry->is_dirD= $opt{"dir"}
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
121 4 });
122 4 return map { $self->_rebless($_) } @files;
123 }
124  
125 3 @files = map { $self->_rebless($_) } $self->_dir->children;
126  
127 3 ▶ return @files if $opt{dir} && $opt{file};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"dir"} and $opt{"file"} 1 2 3
Condition100%3/3
$opt{"dir"} and $opt{"file"}
!ll&&!rl&&r
111
MC/DC100%2/2
$opt{"dir"} and $opt{"file"}
$opt{"dir"} $opt{"file"}
Truth table100%3/3
A and B
A= $opt{"dir"}B= $opt{"file"}
ABresult
0X0
100
111
128  
129 return $opt{dir} ?
130 4 grep { $_->is_dir } @files :
131 2 ▶ grep { !$_->is_dir } @files;
Branch: 1 branch
BranchTrueFalseTotal
$opt{'dir'} ? : 1 1 2
132  
133 }
134  
135 17 ▶sub _rebless {
Subroutine _rebless: called CC=2 SCAR=6.9
136 17 my ($self, $entity) = @_;
137  
138 17 $entity = $entity->absolute($self->root_dir);
139 17 ▶ if ($entity->is_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entity->is_dir) { } 3 14 17
140 3 bless $entity, 'Catalyst::Model::File::Dir';
141 }
142 else {
143 14 bless $entity, 'Catalyst::Model::File::File';
144 }
145  
146 17 $entity->{stringify_as} = $entity->relative($self->_dir)->as_foreign('Unix')->stringify;
147 17 return $entity;
148 }
149  
150 =head2 change_dir
151  
152 =head2 cd
153  
154 Set current working directory (relative to current) and return $self.
155  
156 =cut
157  
158 9 ▶sub cd { shift->change_dir(@_) }
Subroutine cd: called CC=1 SCAR=0.0
Pod documented
159  
160  
161 9 ▶sub change_dir {
Subroutine change_dir: called CC=8 SCAR=20.9
Pod documented
162 9 my $self = shift;
163  
164 9 my $dir = shift;
165  
166 9 ▶ return $self unless defined $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 0 9 9
167  
168 9 ▶ $dir = Path::Class::dir($dir, @_) unless ref $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $dir 9 0 9
169  
170 9 my @dir_list = ();
171 9 $self->_directory(Path::Class::dir(''));
172  
173 9 ▶ if ($dir->is_absolute) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir->is_absolute) { } 6 3 9
174 6 $self->_clear_dir;
175 6 @dir_list = $dir->dir_list(1);
176 } else {
177 3 $dir = $self->_dir->subdir($dir);
178 3 $self->_clear_dir;
179 3 ▶ return $self unless ($self->root_dir->subsumes($dir) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->root_dir->subsumes($dir) 0 3 3
180  
181 3 @dir_list = $dir->relative($self->{root_dir})->dir_list;
182 }
183  
184 # $self->_directory($self->_directory->subdir(@dir_list));
185 9 foreach my $subdir (@dir_list) {
186 7 ▶ $self->_dir($self->_dir->subdir($subdir)) unless $subdir eq '..';
Branch: 1 branch
BranchTrueFalseTotal
unless $subdir eq ".." 6 1 7
187 7 ▶ $self->_dir($self->_dir->parent) if $subdir eq '..';
Branch: 1 branch
BranchTrueFalseTotal
if $subdir eq ".." 1 6 7
188 }
189  
190 9 $self->_directory($self->_dir->relative($self->root_dir)->absolute('/'));
191  
192 9 return $self;
193 }
194  
195 =head2 directory
196  
197 =head2 pwd
198  
199 Get the current working directory, from which all relative paths are based.
200  
201 =cut
202  
203 7 ▶sub pwd { shift->directory(@_) }
Subroutine pwd: called CC=1 SCAR=0.0
Pod documented
204  
205 7 ▶sub directory {
Subroutine directory: called CC=1 SCAR=0.0
Pod documented
206 7 return shift->_directory->as_foreign('Unix');
207 }
208  
209 =head2 parent
210  
211 Move up to the parent of the working directory. Returns $self.
212  
213 =cut
214  
215 3 ▶sub parent {
Subroutine parent: called CC=2 SCAR=6.9
Pod documented
216 3 my ($self) = @_;
217  
218 3 $self->_dir($self->_dir->parent);
219  
220 3 ▶ unless ($self->root_dir->subsumes($self->_dir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->root_dir->subsumes($self->_dir)) 1 2 3
221 1 $self->_clear_dir;
222 1 return $self;
223 }
224  
225 2 $self->_directory($self->_dir->relative($self->root_dir)->absolute('/'));
226  
227 2 return $self;
228 }
229  
230 =head2 $self->file($file)
231  
232 Returns an L<Path::Class::File> object of $file (which can be a string or a
233 Class::Path::File object,) or undef if the file is an invalid path - i.e.
234 outside the directory structure specified in the config.
235  
236 =cut
237  
238 7 ▶sub file {
Subroutine file: called CC=4 SCAR=14.5
Pod documented
239 7 my ($self, $file) = @_;
240  
241 7 ▶ return unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 7 7
242  
243 7 ▶ $file = (ref $file ? $file : Path::Class::file($file) )->absolute($self->_dir);
Branch: 1 branch
BranchTrueFalseTotal
ref $file ? : 0 7 7
244  
245 7 ▶ return undef unless $self->root_dir->subsumes($file);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->root_dir->subsumes($file) 0 7 7
246  
247 # Make sure the dir tree exists
248 7 $file->dir->mkpath(0, oct($self->dir_create_mask));
249 7 return $file;
250  
251 }
252  
253 =head2 $self->slurp($file)
254  
255 Shortcut to $self->file($file)->slurp.
256  
257 In a scalar context, returns the contents of $file in a string. In a list
258 context, returns the lines of $file (according to how $/ is set) as a list. If
259 the file can't be read, this method will throw an exception.
260  
261 If you want "chomp()" run on each line of the file, pass a true value for the
262 "chomp" or "chomped" parameters:
263  
264 my @lines = $self->slurp($file, chomp => 1);
265  
266  
267 =cut
268  
269 3 ▶sub slurp {
Subroutine slurp: called CC=5 SCAR=23.4
Pod documented
270 3 ▶ my $file = shift->file(shift) or return wantarray ? () : undef;
Branch: 2 branches
BranchTrueFalseTotal
my $file = (shift())->file(shift()) or return wantarray ? () : undef 3 0 3
wantarray ? : 0 0 0
271  
272 3 ▶ return $file->stat ? $file->slurp(@_) : wantarray ? () : undef;
Branch: 2 branches
BranchTrueFalseTotal
$file->stat ? : 3 0 3
wantarray ? : 0 0 0
273 }
274  
275 =head2 $self->splat($file, PRINT_ARGS)
276  
277 Does a print to C<$file> with the specified C<PRINT_ARGS>. Does the same as
278 C<$self->file->openw->print(@_)>
279  
280 =cut
281  
282 3 ▶sub splat {
Subroutine splat: called CC=2 SCAR=7.2
Pod documented
283 3 ▶ my $file = shift->file(shift) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $file = (shift())->file(shift()) or return 3 0 3
284  
285 3 $file->openw->print(@_);
286 }
287  
288 5 __PACKAGE__->meta->make_immutable;
289  
290 package #
291 Catalyst::Model::File::File;
292 5 ▶use base 'Path::Class::File';
Subroutine BEGIN: called CC=1 SCAR=0.0
293 52 ▶sub stringify {
Subroutine stringify: called CC=2 SCAR=7.2
294 52 ▶ return $_[0]->{stringify_as} || $_[0]->abs_stringify;
Condition66%2/3
$_[0]{'stringify_as'} || $_[0]->abs_stringify
l!l&&r!l&&!r
23290
MC/DC0%0/2
$_[0]{'stringify_as'} || $_[0]->abs_stringify
$_[0]{'stringify_as'} $_[0]->abs_stringify
Truth table66%2/3
A || B
A= $_[0]{'stringify_as'}B= $_[0]->abs_stringify
ABresult
1X1
011
000
295 }
296  
297 29 ▶sub abs_stringify {
Subroutine abs_stringify: called CC=1 SCAR=0.0
298 29 Path::Class::File::stringify(shift)
299 }
300  
301 # All these would probably be better done with Moose or something, but i'm lazy
302 0 ▶sub open {
Subroutine open: not called CC=1 SCAR=6.9
303 0 my $s = shift;
304 0 local $s->{stringify_as};
305 0 return $s->SUPER::open(@_);
306 }
307  
308 0 ▶sub touch {
Subroutine touch: not called CC=1 SCAR=6.9
309 0 my $s = shift;
310 0 local $s->{stringify_as};
311 0 return $s->SUPER::touch(@_);
312 }
313  
314 0 ▶sub remove {
Subroutine remove: not called CC=1 SCAR=6.9
315 0 my $s = shift;
316 0 local $s->{stringify_as};
317 0 return $s->SUPER::touch(@_);
318 }
319  
320 1 ▶sub stat {
Subroutine stat: called CC=1 SCAR=0.0
321 1 my $s = shift;
322 1 local $s->{stringify_as};
323 1 return $s->SUPER::stat(@_);
324 }
325 0 ▶sub lstat {
Subroutine lstat: not called CC=1 SCAR=6.9
326 0 my $s = shift;
327 0 local $s->{stringify_as};
328 0 return $s->SUPER::lstat(@_);
329 }
330  
331 5 @Catalyst::Model::File::Dir::ISA = 'Path::Class::Dir';
332 11 ▶sub Catalyst::Model::File::Dir::stringify {
Subroutine stringify: called CC=2 SCAR=7.2
333 return $_[0]->{stringify_as}
334 11 ▶ || Path::Class::Dir::stringify($_[0]);
Condition66%2/3
$_[0]{'stringify_as'} || Path::Class::Dir::stringify($_[0])
l!l&&r!l&&!r
560
MC/DC0%0/2
$_[0]{'stringify_as'} || Path::Class::Dir::stringify($_[0])
$_[0]{'stringify_as'} Path::Class::Dir::stringify($_[0])
Truth table66%2/3
A || B
A= $_[0]{'stringify_as'}B= Path::Class::Dir::stringify($_[0])
ABresult
1X1
011
000
335 }
336  
337 =head1 AUTHOR
338  
339 Ash Berlin, C<ash@cpan.org>
340  
341 =head1 LICENSE
342  
343 This library is free software, you can redistribute it and/or modify
344 it under the same terms as Perl itself.
345  
346 =cut
347  
348 5 1;
349  
↑ Summary Helper/Model/File.pm »