MethodFixtures/Storage/File.pm

filter: Statementstmt 100% 54 / 54 Branchbran 100% 6 / 6 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 95.2% 80 / 84 CC 6 SCAR 8.0
CC
6
Cov
97%
new
5.0
retrieve
2.0
BEGIN
1.0
CRAP 2.2
« MethodFixtures.pm ↑ Summary MethodFixtures/Storage.pm »
1 package Test::MethodFixtures::Storage::File;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.08';
8  
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Digest::MD5 qw( md5_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Path::Tiny qw( path );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use base 'Test::MethodFixtures::Storage';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 __PACKAGE__->mk_accessors(qw/ dir /);
17  
18 2 our $DEFAULT_DIR = 't/.methodfixtures';
19  
20 4 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod documented
21 4 my ( $class, $args ) = @_;
22  
23 4 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
40
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
24 4 ▶ unless ( $args->{dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"dir"}) 1 3 4
25 1 path($DEFAULT_DIR)->mkpath;
26 1 $args->{dir} = $DEFAULT_DIR;
27 }
28  
29 croak "Unable to access "
30 . $args->{dir}
31 . " (do you need to create the storage directory?)"
32 4 ▶ unless -d $args->{dir} && -w $args->{dir};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $args->{"dir"} and -w $args->{"dir"} 1 3 4
Condition66%2/3
-d $args->{"dir"} and -w $args->{"dir"}
!ll&&!rl&&r
103
MC/DC50%1/2
-d $args->{"dir"} and -w $args->{"dir"}
-d $args->{"dir"} -w $args->{"dir"}
Truth table66%2/3
A and B
A= -d $args->{"dir"}B= -w $args->{"dir"}
ABresult
0X0
100
111
33  
34 3 return $class->SUPER::new($args);
35 }
36  
37 5 ▶sub store {
Subroutine store: called CC=1 SCAR=0.0
Pod documented
38 5 my ( $self, $args ) = @_;
39  
40 5 my $method = delete $args->{method};
41 5 my $key = delete $args->{key};
42  
43 5 my $storage = $self->_directory($method);
44 5 $storage->mkpath;
45 5 $storage->child( $self->_filename($key) )->spew_utf8( dump $args );
46  
47 5 return $self;
48 }
49  
50 7 ▶sub retrieve {
Subroutine retrieve: called CC=2 SCAR=6.9
Pod documented
51 7 my ( $self, $args ) = @_;
52  
53 7 my $method = $args->{method};
54 7 my $key = $args->{key};
55  
56 7 my $storage = $self->_directory($method)->child( $self->_filename($key) );
57 7 ▶ return unless $storage->is_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $storage->is_file 2 5 7
58  
59 5 my $data = eval $storage->slurp_utf8();
60  
61 5 return $data;
62 }
63  
64 # TODO test (and escape?) invalid characters
65 12 ▶sub _directory {
Subroutine _directory: called CC=1 SCAR=0.0
66 12 my ( $self, $method ) = @_;
67 12 $method =~ s/(::|')/-/g;
68 12 return path( $self->dir, $method );
69 }
70  
71 22 ▶sub _filename {
Subroutine _filename: called CC=1 SCAR=0.0
72 22 my $self = shift;
73 22 return md5_hex dump shift;
74 }
75  
76 2 1;
77  
78 __END__
« MethodFixtures.pm ↑ Summary MethodFixtures/Storage.pm »