blib/lib/Parse/CPAN/Cached.pm

filter: Statementstmt 98.1% 54 / 55 Branchbran 90.0% 18 / 20 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 5 / 5 Subroutinesub 93.7% 15 / 16 Podpod 100% 1 / 1 totaltotal 96.2% 102 / 106 CC 14 SCAR 16.6
CC
14
Cov
96%
parse_cpan
11.0
_build_cache_dir
4.1
__ANON__
2.0
CRAP 5.2
↑ Summary
1 package Parse::CPAN::Cached;
2  
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp qw<carp>;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use CPAN::Mini;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use App::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Parse::CPAN::Authors;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Parse::CPAN::Packages;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Parse::CPAN::Whois;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.02';
13  
14 has 'cpan_mini_config' => (
15 is => 'ro',
16 isa => 'HashRef',
17 1 ▶ default => sub { return { CPAN::Mini->read_config } },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 1 );
19 has 'cache' => (
20 is => 'ro',
21 isa => 'Object',
22 0 ▶ default => sub { return App::Cache->new() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 1 documentation => 'optional App::Cache compatible object',
24 );
25 1 has 'cache_dir' => (
26 is => 'ro',
27 isa => 'Str',
28 lazy_build => 1, # we're using other constructor params
29 init_arg => undef, # private. can't be set via constructor
30 );
31 has 'parsers' => (
32 is => 'rw',
33 isa => 'HashRef',
34 18 ▶ default => sub { return {
Subroutine __ANON__: called CC=1 SCAR=0.0
35 18 packages => {
36 file => '02packages.details.txt.gz',
37 subdir => 'modules',
38 parser => 'Parse::CPAN::Packages',
39 },
40 authors => {
41 file => '01mailrc.txt.gz',
42 subdir => 'authors',
43 parser => 'Parse::CPAN::Authors',
44 },
45 whois => {
46 file => '00whois.xml',
47 subdir => 'authors',
48 parser => 'Parse::CPAN::Whois',
49 },
50 } },
51 1 );
52 has 'info' => (
53 is => 'rw',
54 isa => 'CodeRef',
55 17 ▶ default => sub { sub { } },
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
56 1 documentation => q{coderef called with single string param of debug info},
57 );
58  
59 14 ▶sub _build_cache_dir {
Subroutine _build_cache_dir: called CC=4 SCAR=14.0
60 14 my ($self) = @_;
61 14 my $dir = $self->cpan_mini_config->{local};
62 14 ▶ die 'Have you loaded minicpan? No local dir in config' if !defined $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir 1 13 14
63 13 my @a = glob $dir;
64 13 ▶ die "local dir in cpan_mini_config '$dir' not valid" if !@a;
Branch: 1 branch
BranchTrueFalseTotal
unless @a 0 13 13
65 13 ▶ die "local dir in cpan_mini_config '$dir' matches multiple files" if @a > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @a > 1 0 13 13
66 13 return $a[0];
67 }
68  
69 24 ▶sub parse_cpan {
Subroutine parse_cpan: called CC=11 SCAR=24.0
Pod documented
70 24 my ($self, $type, $upgrade) = @_;
71  
72 24 ▶ die 'No type' if !defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 1 23 24
73 die "Unknown cpan type '$type'\n"
74 23 ▶ if !$self->parsers->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->parsers->{$type} 2 21 23
75  
76 # Silently upgrade authors to whois iff available
77 21 ▶ if ($type eq 'authors') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "authors") 6 15 21
78 6 my $whois = $self->parse_cpan(whois => 1);
79 6 ▶ return $whois if $whois;
Branch: 1 branch
BranchTrueFalseTotal
if $whois 2 4 6
80 }
81  
82 my $filename = file(
83 $self->cache_dir,
84 $self->parsers->{$type}{subdir},
85 $self->parsers->{$type}{file}
86 19 )->stringify;
87  
88 return
89 19 ▶ if $type eq 'whois'
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "whois" and not -f $filename and $upgrade 4 15 19
Condition100%6/6
$type eq "whois" and not -f $filename and $upgrade
!ll&&!rl&&r
1414
$type eq "whois" and not -f $filename
!ll&&!rl&&r
1045
MC/DC100%3/3
$type eq "whois" and not -f $filename and $upgrade
$type eq "whois" not -f $filename $upgrade
Truth table100%4/4
A and not(B) and C
A= $type eq "whois"B= not -f $filenameC= $upgrade
ABCresult
0XX0
10X0
1100
1111
90 && !-f $filename
91 && $upgrade;
92  
93 15 ▶ die "$filename not found"
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filename 1 14 15
94 if !-f $filename;
95  
96 14 ▶ my $real_type = $type eq 'whois' && $upgrade ? 'authors' : $type;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'whois' && $upgrade ? : 2 12 14
Condition100%3/3
$type eq "whois" and $upgrade
!ll&&!rl&&r
1022
MC/DC100%2/2
$type eq "whois" and $upgrade
$type eq "whois" $upgrade
Truth table100%3/3
A and B
A= $type eq "whois"B= $upgrade
ABresult
0X0
100
111
97 return $self->cache->get_code(
98 $filename,
99 5 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
100 5 $self->info->("Caching '$filename' for $real_type");
101 5 $self->parsers->{$type}{parser}->new($filename);
102 }
103 14 );
104 }
105  
106 1 1;
107  
108 __END__
↑ Summary