blib/lib/Data/Persist.pm

filter: Statementstmt 86.3% 63 / 73 Branchbran 50.0% 6 / 12 Conditioncond 38.8% 7 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 88.8% 16 / 18 Podpod 100% 3 / 3 totaltotal 70.3% 95 / 135 CC 13 SCAR 19.2
CC
13
Cov
74%
BUILD
21.5
read
5.3
write
3.2
CRAP 6.8
↑ Summary
1 package Data::Persist;
2 {
3 3 $Data::Persist::VERSION = '0.12';
4 }
5 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 $Data::Persist::AUTHORITY = 'cpan:TEX';
7 }
8 # ABSTRACT: an easy-to-use data-to-disk dumper
9  
10 3 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # use IO::Handle;
18 # use autodie;
19 # use MooseX::Params::Validate;
20  
21 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use Data::Serializer;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use File::Blarf;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 3 has 'filename' => (
26 'is' => 'rw',
27 'isa' => 'Str',
28 'required' => 1,
29 );
30  
31 3 has '_serializer' => (
32 'is' => 'ro',
33 'isa' => 'Data::Serializer',
34 'lazy' => 1,
35 'builder' => '_init_serializer',
36 );
37  
38 3 with qw(Log::Tree::RequiredLogger);
39  
40 1 ▶sub _init_serializer {
Subroutine _init_serializer: called CC=1 SCAR=0.0
41 1 my $self = shift;
42  
43 1 my $ser = Data::Serializer::->new( compress => 1, );
44 1 return $ser;
45 }
46  
47 1 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=30.7
Pod documented
48 1 my $self = shift;
49  
50 # make sure we can either write to the exisiting file or, if it does not exist, write
51 # to the parent directory
52 1 ▶ if ( $self->filename() && -e $self->filename() && !-w $self->filename() ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->filename and -e $self->filename and not -w $self->filename) { } 0 1 1
elsif ($self->filename and not -e $self->filename) { } 0 1 1
Condition33%2/6
$self->filename and -e $self->filename and not -w $self->filename
!ll&&!rl&&r
010
$self->filename and -e $self->filename
!ll&&!rl&&r
001
MC/DC0%0/3
$self->filename and -e $self->filename and not -w $self->filename
$self->filename -e $self->filename not -w $self->filename
Truth table25%1/4
A and B and not(C)
A= $self->filenameB= -e $self->filenameC= not -w $self->filename
ABCresult
0XX0
10X0
1100
1111
53 0 die( 'Can not write cache file at ' . $self->filename() );
54 }
55 ▶ elsif ( $self->filename() && !-e $self->filename() ) {
Condition33%1/3
$self->filename and not -e $self->filename
!ll&&!rl&&r
010
MC/DC0%0/2
$self->filename and not -e $self->filename
$self->filename not -e $self->filename
Truth table33%1/3
A and not(B)
A= $self->filenameB= not -e $self->filename
ABresult
0X0
100
111
56 0 my @path = split /\//, $self->filename();
57 0 my $file = pop @path;
58 0 my $dir = join q{/}, @path;
59 0 ▶ if ( !-w $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w $dir) 0 0 0
60 0 die( 'File ' . $self->filename() . ' does not exist and parent directory '.$dir.' is not writeable.' );
61 }
62 }
63  
64 1 return 1;
65 }
66 ## no critic (ProhibitBuiltinHomonyms)
67 1 ▶sub write {
Subroutine write: called CC=3 SCAR=11.7
Pod documented
68 ## use critic
69 1 my $self = shift;
70 1 my $hash_ref = shift;
71 1 ▶ my $filename = shift || $self->filename();
Condition33%1/3
shift() || $self->filename
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || $self->filename
shift() $self->filename
Truth table33%1/3
A || B
A= shift()B= $self->filename
ABresult
1X1
011
000
72  
73 1 ▶ my $success = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
74 1 my $text = $self->_serializer()->freeze($hash_ref);
75 1 File::Blarf::blarf( $filename, $text, { Flock => 1, } );
76 1 1;
77 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 0 $self->logger()->log( message => 'Failed to serialize cache: '.$_, level => 'warning' );
79 1 };
80 1 ▶ if ( !$success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 1 1
81 0 return;
82 }
83 1 $self->logger()->log( message => 'Serialized cache to ' . $filename, level => 'debug' );
84 1 return 1;
85 }
86 ## no critic (ProhibitBuiltinHomonyms)
87 2 ▶sub read {
Subroutine read: called CC=5 SCAR=16.8
Pod documented
88 ## use critic
89 2 my $self = shift;
90 2 ▶ my $filename = shift || $self->filename();
Condition66%2/3
shift() || $self->filename
l!l&&r!l&&!r
110
MC/DC0%0/2
shift() || $self->filename
shift() $self->filename
Truth table66%2/3
A || B
A= shift()B= $self->filename
ABresult
1X1
011
000
91  
92 2 ▶ if ( !-e $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $filename) 1 1 2
93 1 $self->logger()->log( message => 'No cache file found at ' . $filename, level => 'notice' );
94 1 return;
95 }
96  
97 1 my $text = File::Blarf::slurp( $filename, { Flock => 1, } );
98 1 my $unser;
99 1 ▶ my $success = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
100 1 $unser = $self->_serializer()->thaw($text);
101 1 1;
102 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
103 0 $self->logger()->log( message => 'Failed to unserialize cache: '.$_, level => 'warning' );
104 1 };
105 1 ▶ if ( !$success || !$unser ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success and $unser) 0 1 1
Condition33%1/3
$success and $unser
!ll&&!rl&&r
001
MC/DC0%0/2
$success and $unser
$success $unser
Truth table33%1/3
A and B
A= $successB= $unser
ABresult
0X0
100
111
106 0 return;
107 }
108 1 $self->logger()->log( message => 'Unserialized cache from ' . $filename, level => 'debug' );
109 1 return $unser;
110 }
111  
112 3 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
113 3 __PACKAGE__->meta->make_immutable;
114  
115 3 1;
116  
117 __END__
↑ Summary