Collector.pm

filter: Statementstmt 96.4% 220 / 228 Branchbran 73.8% 65 / 88 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 36 / 36 Podpod 100% 16 / 16 totaltotal 91.0% 344 / 378 SCAR 42.0
CC
63
Cov
90%
_generate_short_names
8.0
_get_args
8.0
_get_file_manifest
6.0
CRAP 66.9
↑ Summary Collector/Processor.pm »
1 package File::Collector ;
2 3 $File::Collector::VERSION = '0.037';
3 3 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # public methods
11  
12 sub AUTOLOAD {
13 41 ▶ our $AUTOLOAD;
Subroutine AUTOLOAD: called CC=6 SCAR=17.9
14 41 my $s = shift;
15 41 ▶ $AUTOLOAD =~ /.*::(next_|isa_|get_)*(\w+)_files*$/ or
Branch: 1 branch
BranchTrueFalseTotal
$File::Collector::AUTOLOAD =~ /.*::(next_|isa_|get_)*(\w+)_files*$/ or croak("No such method: $File::Collector::AUTOLOAD") 1 40 41
16 croak "No such method: $AUTOLOAD";
17  
18 40 ▶ if (!$s->{_files}{"$2_files"}) { $s->_scroak("No such file category exists: '$2' at "); }
Branch: 1 branch
BranchTrueFalseTotal
if (not $s->{'_files'}{"$2_files"}) { } 1 39 40
19 39 ▶ else { return $s->{_files}{"$2_files"} if !$1; }
Branch: 1 branch
BranchTrueFalseTotal
unless $1 2 37 39
20  
21 37 ▶ if ($1 eq 'next_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "next_") 23 14 37
22 23 return $s->{_files}{"$2_files"}->next;
23 }
24  
25 14 ▶ if ($1 eq 'isa_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "isa_") 2 12 14
26 2 return $s->{_files}{"$2_files"}->_isa($s->selected);
27 }
28  
29 # must be a "get" method
30 12 my $cat = $2;
31 12 my $class = $s->{_processor_map}{$cat};
32 my $obj = $class->new($s->{_files}{all},
33 \($s->{selected}),
34 12 $s->{_files}{"${cat}_files"}{_files});
35 12 return $obj;
36 }
37  
38 sub new {
39 11 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
40  
41 # process args
42 11 my ($user_opts, $classes, @resources) = _get_args(@_);
43  
44 # get options hash
45 6 my $default_opts = { recurse => 1 };
46 6 my %opts = (%$default_opts, %$user_opts);
47  
48 # construct object
49 6 my $s = bless {
50 _files => { all => {} },
51 _common_dir => '',
52 selected => '',
53 _options => \%opts,
54 _classes => $classes,
55 all => undef,
56 }, $class;
57  
58 # eval class code
59 6 foreach my $class ( @$classes ) {
60 5 eval "require $class";
61 }
62  
63 # a bit of trickery to make Processor class code consistent with base class
64 6 $s->{all} = $s->{_files}{all};
65  
66 # add rersources and process files
67 6 $s->add_resources(@resources);
68  
69 5 return $s;
70 }
71  
72 sub add_resources {
73 6 ▶ my ($s, @resources) = @_;
Subroutine add_resources: called CC=5 SCAR=16.1
Pod documented
74  
75 # collect the files
76 6 foreach my $resource (@resources) {
77 7 $s->_exists($resource);
78 6 ▶ $s->_add_file($resource) if -f $resource;
Branch: 1 branch
BranchTrueFalseTotal
if -f $resource 1 5 6
79 6 ▶ $s->_get_file_manifest($resource) if -d $resource;
Branch: 1 branch
BranchTrueFalseTotal
if -d $resource 5 1 6
80 }
81  
82 5 $s->_generate_short_names; # calculate the short names
83 5 $s->_init_all_processors;
84 5 foreach my $file (@{$s->{_files}{new}}) {
85 23 $s->{selected} = $file;
86 23 $s->_classify_all;
87 }
88 5 $s->_run_all;
89 5 undef $s->{selected};
90 5 undef $s->{_files}{new}; # clear the new_file array
91 }
92  
93 sub get_count {
94 3 ▶ my $s = shift;
Subroutine get_count: called CC=1 SCAR=0.0
Pod documented
95 3 return (scalar keys %{$s->{_files}{all}})
96 }
97  
98 sub get_files {
99 7 ▶ my $s = shift;
Subroutine get_files: called CC=1 SCAR=0.0
Pod documented
100  
101 7 my @files = sort keys %{$s->{_files}{all}};
102 7 return @files;
103 }
104  
105 sub get_file {
106 2 ▶ my ($s, $file) = @_;
Subroutine get_file: called CC=2 SCAR=6.9
Pod documented
107 2 ▶ $s->_scroak('No file argument passed to method. Aborting.') if !$file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 1 1 2
108  
109 1 return $s->{_files}{all}{$file};
110 }
111  
112 sub list_files_long {
113 1 ▶ my $s = shift;
Subroutine list_files_long: called CC=2 SCAR=6.9
Pod documented
114  
115 1 my @files = $s->get_files;
116 1 print $_ . "\n" for @files;
117 }
118  
119 sub list_files {
120 2 ▶ my $s = shift;
Subroutine list_files: called CC=2 SCAR=6.9
Pod documented
121  
122 2 my @files = map { $s->{_files}{all}{$_}{short_path} } sort keys %{$s->{_files}{all}};
123 2 print "\nFiles found in '".$s->{_common_dir}."':\n\n";
124 2 print $_ . "\n" for @files;
125 }
126  
127 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
128 }
129  
130 # private methods meant for used by subclasses
131  
132 sub _classify {
133 23 ▶ my ($s, @classes) = @_;
Subroutine _classify: called CC=4 SCAR=14.1
134 23 foreach my $type (@classes) {
135 23 my $t = $type . '_files';
136 23 my $file = $s->selected;
137  
138 # die if bad args given
139 23 ▶ die ("No $type argument sent to _classify method. Aborting.") if !$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 23 23
140 23 ▶ die ("No processor called $type exists. Aborting.") if !$s->{_files}{$t};
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"_files"}{$t} 0 23 23
141  
142 23 $s->{_files}{$t}->_add_file($file, $s->{_files}{all}{$file});
143 }
144 }
145  
146 sub _add_obj {
147 23 ▶ my ($s, $type, $obj) = @_;
Subroutine _add_obj: called CC=2 SCAR=7.1
148 23 ▶ $s->_scroak("Missing args to 'add_obj' method. Aborting.") if (!$obj);
Branch: 1 branch
BranchTrueFalseTotal
unless $obj 0 23 23
149  
150 23 $s->{_files}{all}{$s->selected}{"${type}_obj"} = $obj;
151 }
152  
153 # Methods for iterators
154  
155 sub get_obj_prop {
156 121 ▶ my ($s, $obj, $prop) = @_;
Subroutine get_obj_prop: called CC=5 SCAR=16.7
Pod documented
157  
158 121 ▶ if (!$prop) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prop) 1 120 121
159 1 $s->_scroak ("Missing arguments to get_obj_prop method");
160 }
161  
162 my $file = ref ($s->selected) eq 'HASH'
163 ? $s->selected->{full_path}
164 120 ▶ : $s->selected;
Branch: 1 branch
BranchTrueFalseTotal
ref $s->selected eq 'HASH' ? : 120 0 120
165 120 my $attr = "_$prop";
166 120 my $o = $obj . '_obj';
167 120 my $object = $s->{all}{$file}{$o};
168 120 ▶ if (! exists $object->{$attr} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $object->{$attr}) 0 120 120
169 0 $s->_scroak ("Non-existent $obj object attribute requested: '_$prop'");
170 }
171 120 my $value = $object->{$attr};
172 120 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 120 120
173 0 return @$value;
174 } else {
175 120 return $value;
176 }
177 }
178  
179 sub get_obj {
180 23 ▶ my ($s, $obj) = @_;
Subroutine get_obj: called CC=3 SCAR=11.8
Pod documented
181  
182 23 ▶ if (!$obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj) 0 23 23
183 0 _scroak ("Missing arguments to get_obj method");
184 }
185  
186 my $file = ref ($s->selected) eq 'HASH'
187 ? $s->selected->{full_path}
188 23 ▶ : $s->selected;
Branch: 1 branch
BranchTrueFalseTotal
ref $s->selected eq 'HASH' ? : 0 23 23
189 23 my $o = $obj . '_obj';
190  
191 23 return $s->{all}{$file}{$o};
192 }
193  
194 sub set_obj_prop {
195 23 ▶ my ($s, $obj, $prop, $val) = @_;
Subroutine set_obj_prop: called CC=3 SCAR=11.7
Pod documented
196  
197 23 ▶ if (!$val) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($val) 0 23 23
198 0 $s->_scroak ("Missing arguments to set_obj_prop method");
199 }
200  
201 23 my $file = $s->selected;
202  
203 23 my $o = $obj . '_obj';
204 23 my $object = $s->{all}{$file}{$o};
205 23 my $attr = "_$prop";
206 23 ▶ if (! exists $object->{$attr} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $object->{$attr}) 0 23 23
207 0 $s->_scroak ("Non-existent $obj object attribute requested: '$prop'");
208 }
209  
210 23 $object->{$attr} = $val;
211 }
212  
213 sub get_filename {
214 23 ▶ my $s = shift;
Subroutine get_filename: called CC=1 SCAR=0.0
Pod documented
215 23 my $file = $s->selected;
216  
217 23 return $s->{all}{$file}{filename};
218 }
219  
220 sub obj_meth {
221 # Keep these args shifted individually
222 23 ▶ my $s = shift;
Subroutine obj_meth: called CC=4 SCAR=14.8
Pod documented
223 23 my $obj = shift;
224 23 my $meth = shift;
225 my $file = ref ($s->selected) eq 'HASH'
226 ? $s->selected->{full_path}
227 23 ▶ : $s->selected;
Branch: 1 branch
BranchTrueFalseTotal
ref $s->selected eq 'HASH' ? : 0 23 23
228  
229 23 ▶ if (!$meth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($meth) 0 23 23
230 0 $s->_scroak ("Missing arguments to obj_meth method");
231 }
232  
233 23 my $o = $obj . '_obj';
234 23 $obj = $s->{all}{$file}{$o};
235  
236 23 ▶ if (! $obj->can($meth)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj->can($meth)) 0 23 23
237 0 _scroak ("Non-existent method on $obj object: '$meth'");
238 }
239 23 return $obj->$meth($s->_short_name, @_);
240 }
241  
242 sub selected {
243 932 ▶ my $s = shift;
Subroutine selected: called CC=1 SCAR=0.0
Pod documented
244 932 $s->{selected};
245 }
246  
247 sub has_obj {
248 24 ▶ my ($s, $type) = @_;
Subroutine has_obj: called CC=3 SCAR=11.0
Pod documented
249  
250 24 ▶ if (!$type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 1 23 24
251 1 $s->_scroak ("Missing argument to has method");
252 }
253  
254 23 my $to = "${type}_obj";
255 my $file = ref ($s->selected) eq 'HASH'
256 ? $s->selected->{full_path}
257 23 ▶ : $s->selected;
Branch: 1 branch
BranchTrueFalseTotal
ref $s->selected eq 'HASH' ? : 0 23 23
258 23 return defined $s->{all}{$file}{$to};
259 }
260  
261 sub attr_defined {
262 23 ▶ my $s = shift;
Subroutine attr_defined: called CC=2 SCAR=7.2
Pod documented
263 23 my $obj = shift;
264 23 my $attr = shift;
265  
266 23 ▶ if (ref $s->selected eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s->selected eq 'HASH') { } 0 23 23
267 0 return exists $s->selected->{"${obj}_obj"}->{"_${attr}"};
268 } else {
269 23 return exists $s->{_files}{all}{$s->selected}{"${obj}_obj"}->{"_${attr}"};
270 }
271 }
272  
273 sub print_short_name {
274 30 ▶ my $s = shift;
Subroutine print_short_name: called CC=2 SCAR=7.1
Pod documented
275  
276 30 ▶ $s->_scroak ("The 'print_short_name' method does not accept methods") if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 30 30
277 30 print $s->_short_name . "\n";
278 }
279  
280 # private helper methods
281  
282 sub _short_name {
283 53 ▶ my $s = shift;
Subroutine _short_name: called CC=2 SCAR=6.9
284 my $file = ref ($s->selected) eq 'HASH'
285 ? $s->selected->{full_path}
286 53 ▶ : $s->selected;
Branch: 1 branch
BranchTrueFalseTotal
ref $s->selected eq 'HASH' ? : 10 43 53
287 53 $s->{all}{$file}{short_path};
288 }
289  
290 sub _exists {
291 7 ▶ my $s = shift;
Subroutine _exists: called CC=2 SCAR=6.9
292 7 ▶ $s->_scroak("'$_[0]' does not exist, aborting call from: ") if ! -e $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if not -e $_[0] 1 6 7
293 }
294  
295 sub _scroak {
296 5 ▶ my $s = shift;
Subroutine _scroak: called CC=1 SCAR=0.0
297 5 my $msg = shift;
298 5 croak($msg . ' ' . (fileparse((caller(1))[1]))[0] . ', line ' . (caller(1))[2] . "\n");
299 }
300  
301 sub _get_file_manifest {
302 10 ▶ my ($s, $dir) = @_;
Subroutine _get_file_manifest: called CC=6 SCAR=18.0
303  
304 10 ▶ opendir (my $dh, $dir) or die "Can't opendir $dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $dir or die "Can't opendir ${dir}: $!" 0 10 10
305 10 my @dirs_and_files = grep { /^[^\.]/ } readdir($dh);
306  
307 10 my @files = grep { -f File::Spec->catfile($dir, $_) } @dirs_and_files;
308 10 $s->_add_file( File::Spec->catfile($dir, $_)) for @files;
309  
310 10 ▶ my @dirs = grep { -d File::Spec->catdir( $dir, $_ ) } @dirs_and_files if $s->{_options}{recurse};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"_options"}{"recurse"} 9 1 10
311 10 foreach my $tdir (@dirs) {
312 5 ▶ opendir (my $tdh, File::Spec->catdir($dir, $tdir)) || die "Can't opendir $tdir: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $tdh, "File::Spec"->catdir($dir, $tdir) or die "Can't opendir ${tdir}: $!" 0 5 5
313 5 $s->_get_file_manifest(File::Spec->catdir( $dir, $tdir ));
314 }
315 }
316  
317 sub _run_all {
318 5 ▶ my $s = shift;
Subroutine _run_all: called CC=3 SCAR=11.1
319 5 my $classes = $s->{_classes};
320 5 foreach my $c ( @$classes ) {
321 5 my $role = Role::Tiny->apply_roles_to_object ($s, $c);
322 5 ▶ $role->_run_processes if $role->can('_run_processes');;
Branch: 1 branch
BranchTrueFalseTotal
if $role->can("_run_processes") 0 5 5
323 }
324 }
325  
326 sub _generate_short_names {
327 5 ▶ my $s = shift;
Subroutine _generate_short_names: called CC=8 SCAR=20.8
328  
329 5 my @files = $s->get_files;
330 5 my $file = pop @files;
331 5 my @letters = split //, $file;
332 5 my ($new_string, $longest_string) = '';
333 5 foreach my $cfile (@files) {
334 18 my @cletters = split //, $cfile;
335 18 my $lc = 0;
336  
337 18 foreach my $cletter (@cletters) {
338 1072 ▶ if (defined $letters[$lc] && $cletters[$lc] eq $letters[$lc]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $letters[$lc] and $cletters[$lc] eq $letters[$lc]) 1054 18 1072
Condition100%3/3
defined $letters[$lc] and $cletters[$lc] eq $letters[$lc]
!ll&&!rl&&r
1531054
MC/DC100%2/2
defined $letters[$lc] and $cletters[$lc] eq $letters[$lc]
defined $letters[$lc] $cletters[$lc] eq $letters[$lc]
Truth table100%3/3
A and B
A= defined $letters[$lc]B= $cletters[$lc] eq $letters[$lc]
ABresult
0X0
100
111
339 1054 $new_string .= $cletters[$lc++];
340 1054 next;
341 }
342 18 $longest_string = $new_string;
343 18 @letters = split //, $new_string;
344 18 $new_string = '';
345 18 last;
346 }
347 }
348  
349 5 ▶ $s->{_common_dir} = $longest_string || (fileparse($file))[1];
Condition66%2/3
$longest_string || (fileparse($file))[1]
l!l&&r!l&&!r
320
MC/DC0%0/2
$longest_string || (fileparse($file))[1]
$longest_string (fileparse($file))[1]
Truth table66%2/3
A || B
A= $longest_stringB= (fileparse($file))[1]
ABresult
1X1
011
000
350  
351 5 ▶ if (@files) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files) { } 3 2 5
352 3 foreach my $file ( @files, $file ) {
353 21 my $tfile = $file;
354 21 $tfile =~ s/\Q$longest_string\E//;
355 21 $s->{_files}{all}{$file}{short_path} = $tfile;
356 }
357 } else {
358 2 $s->{_files}{all}{$file}{short_path} = $file;
359 }
360 }
361  
362 sub _add_file {
363 23 ▶ my ($s, $file) = @_;
Subroutine _add_file: called CC=2 SCAR=7.0
364  
365 23 $file = File::Spec->rel2abs( $file );
366 23 $s->{_files}{all}{$file}{full_path} = $file;
367 23 my $filename = (fileparse($file))[0];
368 23 $s->{_files}{all}{$file}{filename} = $filename;
369  
370 23 ▶ push @{$s->{_files}{new}}, $file if !$s->{_files}{$file};
Branch: 1 branch
BranchTrueFalseTotal
if not $s->{"_files"}{$file} 23 0 23
371 }
372  
373 sub _init_all_processors {
374 5 ▶ my $s = shift;
Subroutine _init_all_processors: called CC=5 SCAR=16.3
375  
376 5 foreach my $c ( @{ $s->{_classes} } ) {
377 5 ▶ my @processors = $c->_init_processors if $c->can('_init_processors');
Branch: 1 branch
BranchTrueFalseTotal
if $c->can("_init_processors") 5 0 5
378 5 my $it_class = $c . '::Processor';
379 5 foreach my $it ( @processors ) {
380 10 ▶ next if ($s->{_files}{"${it}_files"}); # don't overwrite existing processor
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"_files"}{"${it}_files"} 0 10 10
381 10 $s->{_processor_map}{$it} = $it_class;
382 10 $s->{_files}{"${it}_files"} = $it_class->new($s->{_files}{all}, \($s->{selected}));
383 }
384 }
385 }
386  
387 sub _classify_all {
388 23 ▶ my $s = shift;
Subroutine _classify_all: called CC=3 SCAR=11.1
389 23 foreach my $c ( @{ $s->{_classes} } ) {
390 23 my $role = Role::Tiny->apply_roles_to_object ($s, $c);
391 23 ▶ $role->_classify_file() if $role->can('_classify_file');;
Branch: 1 branch
BranchTrueFalseTotal
if $role->can("_classify_file") 23 0 23
392 }
393 }
394  
395 sub _get_args {
396 11 ▶ my $user_opts = {};
Subroutine _get_args: called CC=8 SCAR=20.8
397 11 my @resources;
398 my $classes;
399 11 foreach my $arg (@_) {
400 24 ▶ if (!ref $arg) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $arg) { } 10 14 24
elsif (ref $arg eq 'HASH') { } 4 10 14
elsif (ref $arg eq 'ARRAY') { } 9 1 10
401 10 push @resources, $arg;
402 } elsif (ref($arg) eq 'HASH') {
403 4 ▶ die ('Only one option hash allowed in constructor. Aborting.') if %$user_opts;
Branch: 1 branch
BranchTrueFalseTotal
if %$user_opts 1 3 4
404 3 $user_opts = $arg;
405 } elsif (ref($arg) eq 'ARRAY') {
406 9 ▶ die ('Only one class array allowed in constructor. Aborting.') if $classes;
Branch: 1 branch
BranchTrueFalseTotal
if $classes 1 8 9
407 8 $classes = $arg;
408 } else {
409 1 die ('Unrecognized argument type passed to constructor');
410 }
411 }
412 8 ▶ die('No list of resources passed to constructor. Aborting.') if ! @resources;
Branch: 1 branch
BranchTrueFalseTotal
unless @resources 2 6 8
413 #die('No Collector class array passed to constructor. Aborting.') if !$classes;
414  
415 6 return ($user_opts, $classes, @resources);
416 }
417  
418 3 1; # Magic true value
419 # ABSTRACT: Base class for custom File::Collector classes for classifying files and calling File::Collector::Processor methods for processing files
420  
421 __END__
↑ Summary Collector/Processor.pm »