lib/File/DataClass/Storage/Any.pm

filter: Statementstmt 98.6% 72 / 73 Branchbran 77.2% 17 / 22 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 23 / 23 Podpod 100% 13 / 13 totaltotal 94.2% 131 / 139 CC 15 SCAR 10.1
CC
15
Cov
93%
load
6.0
__ANON__
5.1
__ANON__
3.1
CRAP 2.8
« lib/File/DataClass/Storage.pm ↑ Summary lib/File/DataClass/Storage/JSON.pm »
1 package File::DataClass::Storage::Any;
2  
3 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use File::Basename qw( basename );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use File::DataClass::Constants qw( FALSE TRUE );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::DataClass::Functions qw( ensure_class_loaded first_char
Subroutine BEGIN: called CC=1 SCAR=0.0
8 qualify_storage_class map_extension2class
9 3 is_stale merge_file_data throw );
10 3 ▶use File::DataClass::Storage;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::DataClass::Types qw( Object HashRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 has 'schema' => is => 'ro', isa => Object,
15 handles => [ 'cache', 'storage_attributes', ],
16 required => TRUE, weak_ref => TRUE;
17  
18  
19 6 ▶has '_stores' => is => 'ro', isa => HashRef, default => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
20  
21 # Private methods
22 37 ▶my $_get_store_from_extension = sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
23 37 my ($self, $extn) = @_; my $stores = $self->_stores;
24  
25 37 ▶ exists $stores->{ $extn } and return $stores->{ $extn };
Branch: 1 branch
BranchTrueFalseTotal
exists $stores->{$extn} and return $stores->{$extn} 30 7 37
26  
27 7 ▶ my $list; ($list = map_extension2class( $extn ) and my $class = $list->[ 0 ])
Branch: 1 branch
BranchTrueFalseTotal
$list = &map_extension2class($extn) and my $class = $list->[0] or &throw("Extension [_1] has no class", [$extn]) 6 1 7
Condition66%2/3
$list = &map_extension2class($extn) and my $class = $list->[0]
!ll&&!rl&&r
106
MC/DC50%1/2
$list = &map_extension2class($extn) and my $class = $list->[0]
$list = &map_extension2class($extn) my $class = $list->[0]
Truth table66%2/3
A and B
A= $list = &map_extension2class($extn)B= my $class = $list->[0]
ABresult
0X0
100
111
28 or throw 'Extension [_1] has no class', [ $extn ];
29  
30 0 ▶ if (first_char $class eq '+') { $class = substr $class, 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (&first_char($class) eq '+') { } 0 6 6
31 6 else { $class = qualify_storage_class $class }
32  
33 6 ensure_class_loaded $class;
34  
35 return $stores->{ $extn } = $class->new
36 6 ( { %{ $self->storage_attributes }, schema => $self->schema } );
37 3 };
38  
39 37 ▶my $_get_store_from_path = sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
40 37 my ($self, $path) = @_; my $file = basename( "${path}" );
41  
42 37 ▶ my $extn = (split m{ \. }mx, $file)[ -1 ]
Branch: 1 branch
BranchTrueFalseTotal
my $extn = (split(/ \. /mx, $file, 0))[-1] or &throw("File [_1] has no extension", [$file]) 37 0 37
43 or throw 'File [_1] has no extension', [ $file ];
44  
45 37 ▶ my $store = $self->$_get_store_from_extension( ".${extn}" )
Branch: 1 branch
BranchTrueFalseTotal
my $store = $self->$_get_store_from_extension(".$extn") or &throw("Extension [_1] has no store", [$extn]) 36 0 36
46 or throw 'Extension [_1] has no store', [ $extn ];
47  
48 36 return $store;
49 3 };
50  
51 # Public methods
52 1 ▶sub create_or_update {
Subroutine create_or_update: called CC=1 SCAR=0.0
Pod documented
53 1 return shift->$_get_store_from_path( $_[ 0 ] )->create_or_update( @_ );
54 }
55  
56 2 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
57 2 return shift->$_get_store_from_path( $_[ 0 ] )->delete( @_ );
58 }
59  
60 5 ▶sub dump {
Subroutine dump: called CC=1 SCAR=0.0
Pod documented
61 5 return shift->$_get_store_from_path( $_[ 0 ] )->dump( @_ );
62 }
63  
64 1 ▶sub extn {
Subroutine extn: called CC=1 SCAR=0.0
Pod documented
65 }
66  
67 1 ▶sub insert {
Subroutine insert: called CC=1 SCAR=0.0
Pod documented
68 1 return shift->$_get_store_from_path( $_[ 0 ] )->insert( @_ );
69 }
70  
71 8 ▶sub load {
Subroutine load: called CC=6 SCAR=17.9
Pod documented
72 8 ▶ my ($self, @paths) = @_; $paths[ 0 ] or return {};
Branch: 1 branch
BranchTrueFalseTotal
$paths[0] or return {} 7 1 8
73  
74 7 ▶ scalar @paths == 1 and return ($self->read_file( $paths[ 0 ], FALSE ))[ 0 ];
Branch: 1 branch
BranchTrueFalseTotal
scalar @paths == 1 and return ($self->read_file($paths[0], 0))[0] 6 1 7
75  
76 1 my ($loaded, $meta, $newest) = $self->cache->get_by_paths( \@paths );
77 1 my $cache_mtime = $self->meta_unpack( $meta );
78  
79 1 ▶ not is_stale $loaded, $cache_mtime, $newest and return $loaded;
Branch: 1 branch
BranchTrueFalseTotal
&is_stale($loaded, $cache_mtime, $newest) or return $loaded 1 0 1
80  
81 1 $loaded = {}; $newest = 0;
82  
83 1 for my $path (@paths) { # Different storage classes by filename extension
84 2 my ($red, $path_mtime) = $self->read_file( $path, FALSE );
85  
86 2 ▶ $path_mtime > $newest and $newest = $path_mtime;
Branch: 1 branch
BranchTrueFalseTotal
$path_mtime > $newest and $newest = $path_mtime 1 1 2
87 2 merge_file_data $loaded, $red;
88 }
89  
90 1 $self->cache->set_by_paths( \@paths, $loaded, $self->meta_pack( $newest ) );
91 1 return $loaded;
92 }
93  
94 3 ▶sub meta_pack {
Subroutine meta_pack: called CC=3 SCAR=11.0
Pod documented
95 3 ▶ my ($self, $mtime) = @_; my $attr = $self->{_meta_cache} || {};
Condition100%2/2
$self->{'_meta_cache'} || {}
l!l
21
MC/DC100%1/1
$self->{'_meta_cache'} || {}
$self->{'_meta_cache'}
Truth table100%2/2
A
A= $self->{'_meta_cache'}
Aresult
11
00
96  
97 3 ▶ defined $mtime and $attr->{mtime} = $mtime; return $attr;
Branch: 1 branch
BranchTrueFalseTotal
defined $mtime and $attr->{"mtime"} = $mtime 2 1 3
98 }
99  
100 2 ▶sub meta_unpack {
Subroutine meta_unpack: called CC=2 SCAR=7.1
Pod documented
101 2 my ($self, $attr) = @_; $self->{_meta_cache} = $attr;
102  
103 2 ▶ return $attr ? $attr->{mtime} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$attr ? : 2 0 2
104 };
105  
106 8 ▶sub read_file {
Subroutine read_file: called CC=1 SCAR=0.0
Pod documented
107 8 return shift->$_get_store_from_path( $_[ 0 ] )->read_file( @_ );
108 }
109  
110 8 ▶sub select {
Subroutine select: called CC=1 SCAR=0.0
Pod documented
111 8 return shift->$_get_store_from_path( $_[ 0 ] )->select( @_ );
112 }
113  
114 8 ▶sub txn_do {
Subroutine txn_do: called CC=1 SCAR=0.0
Pod documented
115 8 return shift->$_get_store_from_path( $_[ 0 ] )->txn_do( @_ );
116 }
117  
118 2 ▶sub update {
Subroutine update: called CC=1 SCAR=0.0
Pod documented
119 2 return shift->$_get_store_from_path( $_[ 0 ] )->update( @_ );
120 }
121  
122 2 ▶sub validate_params {
Subroutine validate_params: called CC=1 SCAR=0.0
Pod documented
123 2 return shift->$_get_store_from_path( $_[ 0 ] )->validate_params( @_ );
124 }
125  
126 3 1;
127  
128 __END__
« lib/File/DataClass/Storage.pm ↑ Summary lib/File/DataClass/Storage/JSON.pm »