| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 3 | ▶ | warn "No category information about '$file'" unless defined $self->{category_hash}{$file}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 3 | ▶ | my @cats = map AI::Categorizer::Category->by_name(name => $_), @{ $self->{category_hash}{$file} || [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'category_hash'}{$file} || []
MC/DC0%0/1
$self->{'category_hash'}{$file} || []
$self->{'category_hash'}{$file}
Truth table50%1/2
A
A= $self->{'category_hash'}{$file}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 60 | 1 | ▶ | return undef unless @{$self->{path}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 ".."
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 ".."
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$self->{"recurse"} and not grep({$_ eq $path;} @{$$self{"path"};}, @{$$self{"used"};})
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"};})
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | $self->rewind; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | my $count = 0; | |||||||||||||||||||||||||||
| 88 | 1 | ▶ | $count++ while defined $self->_read_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 1 | $self->rewind; | |||||||||||||||||||||||||||
| 91 | 1 | return $self->{document_count} = $count; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 7 | 1; | |||||||||||||||||||||||||||
| 95 | __END__ | ||||||||||||||||||||||||||||