Categorizer/Collection/Files.pm

filter: Statementstmt 88.4% 61 / 69 Branchbran 66.6% 16 / 24 Conditioncond 50.0% 4 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 80.9% 98 / 121 CC 16 SCAR 18.6
CC
16
Cov
80%
_read_file
15.3
next
4.2
_next_path
3.0
CRAP 6.4
« Categorizer/Collection/DBI.pm ↑ Summary Categorizer/Document.pm »
1 package AI::Categorizer::Collection::Files;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 7 ▶use AI::Categorizer::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use base qw(AI::Categorizer::Collection);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use Params::Validate qw(:types);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 __PACKAGE__->valid_params
11 (
12 path => { type => SCALAR|ARRAYREF },
13 recurse => { type => BOOLEAN, default => 0 },
14 );
15  
16 1 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
17 1 my $class = shift;
18 1 my $self = $class->SUPER::new(@_);
19
20 1 $self->{dir_fh} = do {local *FH; *FH}; # double *FH avoids a warning
21  
22 # Documents are contained in a directory, or list of directories
23 1 ▶ $self->{path} = [$self->{path}] unless ref $self->{path};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"path"} 1 0 1
24 1 $self->{used} = [];
25  
26 1 $self->_next_path;
27 1 return $self;
28 }
29  
30 4 ▶sub _next_path {
Subroutine _next_path: called CC=3 SCAR=11.0
31 4 my $self = shift;
32 4 ▶ closedir $self->{dir_fh} if $self->{cur_dir};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cur_dir"} 3 1 4
33  
34 4 $self->{cur_dir} = shift @{$self->{path}};
35 4 push @{$self->{used}}, $self->{cur_dir};
36 4 ▶ opendir $self->{dir_fh}, $self->{cur_dir} or die "$self->{cur_dir}: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir $self->{"dir_fh"}, $self->{"cur_dir"} or die qq[$self->{"cur_dir"}: $!] 4 0 4
37 }
38  
39 3 ▶sub next {
Subroutine next: called CC=4 SCAR=14.3
Pod documented
40 3 my $self = shift;
41 3 my $file = $self->_read_file;
42 3 ▶ return unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 3 3
43
44 3 ▶ warn "No category information about '$file'" unless defined $self->{category_hash}{$file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"category_hash"}{$file} 0 3 3
45 3 ▶ my @cats = map AI::Categorizer::Category->by_name(name => $_), @{ $self->{category_hash}{$file} || [] };
Condition50%1/2
$self->{'category_hash'}{$file} || []
l!l
30
MC/DC0%0/1
$self->{'category_hash'}{$file} || []
$self->{'category_hash'}{$file}
Truth table50%1/2
A
A= $self->{'category_hash'}{$file}
Aresult
11
00
46  
47 return $self->call_method('document', 'read',
48 3 path => File::Spec->catfile($self->{cur_dir}, $file),
49 name => $file,
50 categories => \@cats,
51 );
52 }
53  
54 16 ▶sub _read_file {
Subroutine _read_file: called CC=8 SCAR=27.2
55 16 my ($self) = @_;
56
57 16 my $file = readdir $self->{dir_fh};
58  
59 16 ▶ if (!defined $file) { # Directory has been exhausted
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $file) { } 1 15 16
elsif ($file eq '.' or $file eq '..') { } 8 7 15
elsif (-d (my $path = 'File::Spec'->catdir($self->{'cur_dir'}, $file))) { } 0 7 7
60 1 ▶ return undef unless @{$self->{path}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"path"};} 1 0 1
61 0 $self->_next_path;
62 0 return $self->_read_file;
63 ▶ } elsif ($file eq '.' or $file eq '..') {
Condition100%3/3
$file eq "." or $file eq ".."
l!l&&r!l&&!r
447
MC/DC100%2/2
$file eq "." or $file eq ".."
$file eq "." $file eq ".."
Truth table100%3/3
A or B
A= $file eq "."B= $file eq ".."
ABresult
1X1
011
000
64 8 return $self->_read_file;
65 } elsif (-d (my $path = File::Spec->catdir($self->{cur_dir}, $file))) {
66 0 push @{$self->{path}}, $path # Add for later processing
67 0 ▶ if $self->{recurse} and !grep {$_ eq $path} @{$self->{path}}, @{$self->{used}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"recurse"} and not grep({$_ eq $path;} @{$$self{"path"};}, @{$$self{"used"};}) 0 0 0
Condition0%0/3
$self->{"recurse"} and not grep({$_ eq $path;} @{$$self{"path"};}, @{$$self{"used"};})
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"recurse"} and not grep({$_ eq $path;} @{$$self{"path"};}, @{$$self{"used"};})
$self->{"recurse"} not grep({$_ eq $path;} @{$$self{"path"};}, @{$$self{"used"};})
Truth table0%0/3
A and not(B)
A= $self->{"recurse"}B= not grep({$_ eq $path;} @{$$self{"path"};}, @{$$self{"used"};})
ABresult
0X0
100
111
68 0 return $self->_read_file;
69 }
70 7 return $file;
71 }
72  
73 3 ▶sub rewind {
Subroutine rewind: called CC=1 SCAR=0.0
Pod documented
74 3 my $self = shift;
75 3 push @{$self->{path}}, @{$self->{used}};
76 3 @{$self->{used}} = ();
77 3 $self->_next_path;
78 }
79  
80 # This should share an iterator with next()
81 2 ▶sub count_documents {
Subroutine count_documents: called CC=3 SCAR=11.0
Pod documented
82 2 my $self = shift;
83 2 ▶ return $self->{document_count} if defined $self->{document_count};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"document_count"} 1 1 2
84
85 1 $self->rewind;
86
87 1 my $count = 0;
88 1 ▶ $count++ while defined $self->_read_file;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->_read_file and do { ++$count } 4 1 5
89  
90 1 $self->rewind;
91 1 return $self->{document_count} = $count;
92 }
93  
94 7 1;
95 __END__
« Categorizer/Collection/DBI.pm ↑ Summary Categorizer/Document.pm »