blib/lib/File/Dir/Dumper/DigestCache/FS.pm

filter: Statementstmt 98.3% 59 / 60 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 96.3% 80 / 83 SCAR 16.1
CC
5
Cov
96%
get_digests
3.0
_init
2.0
_slurp
2.0
CRAP 5.0
« blib/lib/File/Dir/Dumper/Stream/JSON/Reader.pm ↑ Summary blib/lib/File/Dir/Dumper.pm »
1 package File::Dir::Dumper::DigestCache::FS;
2 3 $File::Dir::Dumper::DigestCache::FS::VERSION = '0.6.4';
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use parent 'File::Dir::Dumper::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Class::XSAccessor accessors => { _path => '_path', };
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use File::Basename qw/ dirname /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use File::Path 2.00 qw/ make_path /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 sub _init
20 {
21 2 ▶ my ( $self, $args ) = @_;
Subroutine _init: called CC=2 SCAR=7.0
22  
23 my $basepath = $args->{params}->{path}
24 2 ▶ or die "path not specified as a parameter!";
Branch: 1 branch
BranchTrueFalseTotal
my $basepath = $args->{"params"}{"path"} or die "path not specified as a parameter!" 0 2 2
25 2 $self->_path( File::Spec->catdir( $basepath, 'digests-cache-dir' ) );
26  
27 2 return;
28 }
29  
30  
31 sub _slurp
32 {
33 7 ▶ my $filename = shift;
Subroutine _slurp: called CC=2 SCAR=7.0
34  
35 7 ▶ open my $in, '<', $filename
Branch: 1 branch
BranchTrueFalseTotal
&File::Dir::Dumper::DigestCache::FS::open(my $in, "<", $filename) or die "Cannot open '${filename}' for slurping - $!" 0 7 7
36 or die "Cannot open '$filename' for slurping - $!";
37  
38 7 local $/;
39 7 my $contents = <$in>;
40  
41 7 close($in);
42  
43 7 return $contents;
44 }
45  
46 sub get_digests
47 {
48 6 ▶ my ( $self, $args ) = @_;
Subroutine get_digests: called CC=3 SCAR=11.0
Pod documented
49 6 my $mtime = $args->{mtime};
50 6 my $path = File::Spec->catfile( $self->_path, @{ $args->{path} } );
51 6 my $cb = $args->{calc_cb};
52  
53 my $update = sub {
54 5 ▶ open my $out, '>', $path;
Subroutine __ANON__: called CC=1 SCAR=0.0
55 5 $out->print( JSON::MaybeXS->new( canonical => 1 )
56 ->encode( +{ mtime => $mtime, digests => scalar( $cb->() ) } )
57 );
58 5 close $out;
59  
60 5 return;
61 6 };
62 6 ▶ if ( !-f $path )
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $path) 4 2 6
63 {
64 4 make_path( dirname($path) );
65 4 $update->();
66 }
67 6 while (1)
68 {
69 7 my $json =
70 JSON::MaybeXS->new( canonical => 1 )->decode( _slurp($path) );
71 7 ▶ if ( $json->{mtime} == $mtime )
Branch: 1 branch
BranchTrueFalseTotal
if ($json->{'mtime'} == $mtime) { } 6 1 7
72 {
73 6 return $json->{digests};
74 }
75 else
76 {
77 1 $update->();
78 }
79 }
80 0 die "Bug";
81 }
82  
83 3 1; # End of File::Dir::Dumper
84  
85 __END__
« blib/lib/File/Dir/Dumper/Stream/JSON/Reader.pm ↑ Summary blib/lib/File/Dir/Dumper.pm »