KiokuDB/TypeMap/Entry/StorableHook.pm

filter: Statementstmt 77.1% 81 / 105 Branchbran 55.5% 30 / 54 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 83.3% 15 / 18 Podpod 0.0% 0 / 4 totaltotal 68.2% 127 / 186 CC 31 SCAR 20.1
CC
31
Cov
69%
_type
16.0
__ANON__
10.7
_new
10.5
CRAP 7.5
« KiokuDB/Test.pm ↑ Summary KiokuDB/Test/Fixture/TypeMap/Default.pm »
1 #!/usr/bin/perl
2  
3 package KiokuDB::TypeMap::Entry::StorableHook;
4 13 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 13 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # predeclare for namespace::clean;
12 sub _type ($);
13 sub _new ($;$);
14 sub _clear ($);
15  
16 13 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 10 with qw(
19 KiokuDB::TypeMap::Entry::Std
20 KiokuDB::TypeMap::Entry::Std::Expand
21 );
22  
23 sub compile_collapse_body {
24 36 ▶ my ( $self, $class, @args ) = @_;
Subroutine compile_collapse_body: called CC=2 SCAR=7.3
Pod undocumented
25  
26 36 ▶ my $attach = $class->can("STORABLE_attach") ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$class->can('STORABLE_attach') ? : 0 36 36
27  
28 return sub {
29 36 ▶ my ( $self, %args ) = @_;
Subroutine __ANON__: called CC=9 SCAR=23.7
30  
31 36 my $object = $args{object};
32  
33 36 my @type = _type($object);
34  
35 36 my ( $str, @refs ) = $object->STORABLE_freeze(0);
36  
37 36 my $data;
38  
39 36 ▶ if ( @refs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@refs) { } 35 1 36
40 35 ▶ croak sprintf "Freeze cannot return references if %s class is using STORABLE_attach", $class if $attach;
Branch: 1 branch
BranchTrueFalseTotal
if $attach 0 35 35
41  
42 35 ▶ if ( my @non_refs = grep { not ref } @refs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@non_refs) = grep({not ref $_;} @refs)) 0 35 35
43 0 croak blessed($object) . "::STORABLE_freeze returned non reference values: @non_refs";
44 }
45  
46 35 my @collapsed = $self->visit(@refs);
47  
48 35 foreach my $ref ( @collapsed ) {
49 68 ▶ next unless ref($ref) eq 'KiokuDB::Reference';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ref eq "KiokuDB::Reference" 0 68 68
50 68 ▶ next if $self->may_compact($ref);
Branch: 1 branch
BranchTrueFalseTotal
if $self->may_compact($ref) 68 0 68
51 0 $ref = $ref->id; # don't save a bunch of Reference objects when all we need is the ID
52 }
53  
54 35 $data = [ @type, $str, @collapsed ],
55 } else {
56 1 ▶ unless ( $attach ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attach) { } 0 1 1
57 1 ▶ if ( @type == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@type == 1) { } 1 0 1
58 1 $data = ( $type[0] . $str );
59 } else {
60 0 $data = [ @type, $str ];
61 }
62 } else {
63 0 $data = $str;
64 }
65 }
66  
67 36 return $self->make_entry(
68 %args,
69 data => $data,
70 );
71 36 };
72 }
73  
74 sub compile_create {
75 36 ▶ my ( $self, $class, @args ) = @_;
Subroutine compile_create: called CC=2 SCAR=7.8
Pod undocumented
76  
77 36 ▶ unless ( $class->can("STORABLE_attach") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can('STORABLE_attach')) { } 0 36 36
78 # normal form, STORABLE_freeze
79 return sub {
80 28 ▶ my ( $self, $entry ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.7
81  
82 28 my $data = $entry->data;
83  
84 28 ▶ my ( $reftype, @args ) = ref $data ? @$data : ( substr($data, 0, 1), substr($data, 1) );
Branch: 1 branch
BranchTrueFalseTotal
ref $data ? : 27 1 28
85  
86 28 my $instance;
87  
88 28 ▶ if ( ref $args[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0]) { } 0 28 28
89 0 my $tied;
90 0 $self->queue_ref(shift(@args), \$tied);
91 0 $instance = _new( $reftype, $tied );
92 } else {
93 28 $instance = _new( $reftype );
94 }
95  
96 28 bless $instance, $entry->class;
97 36 };
98 } else {
99 # esotheric STORABLE_attach form
100 return sub {
101 0 ▶ my ( $self, $entry ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
102  
103 0 $entry->class->STORABLE_attach( 0, $entry->data ); # FIXME support non ref
104 0 };
105 }
106 }
107  
108 sub compile_clear {
109 36 ▶ my ( $self, $class, @args ) = @_;
Subroutine compile_clear: called CC=1 SCAR=1.2
Pod undocumented
110  
111 return sub {
112 0 ▶ my ( $linker, $instance ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
113  
114 0 _clear($instance);
115 36 };
116 }
117  
118 sub compile_expand_data {
119 72 ▶ my ( $self, $class, @args ) = @_;
Subroutine compile_expand_data: called CC=2 SCAR=7.1
Pod undocumented
120  
121 72 ▶ unless ( $class->can("STORABLE_attach") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can('STORABLE_attach')) { } 0 72 72
122 # normal form, STORABLE_freeze
123 return sub {
124 28 ▶ my ( $self, $instance, $entry ) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.3
125  
126 28 my $data = $entry->data;
127  
128 28 ▶ my ( $reftype, @args ) = ref $data ? @$data : ( substr($data, 0, 1), substr($data, 1) );
Branch: 1 branch
BranchTrueFalseTotal
ref $data ? : 27 1 28
129  
130 28 ▶ shift @args if ref $args[0]; # tied
Branch: 1 branch
BranchTrueFalseTotal
if ref $args[0] 0 28 28
131  
132 28 my ( $str, @refs ) = @args;
133  
134 28 my @inflated;
135  
136 28 foreach my $ref ( @refs ) {
137 52 push @inflated, undef;
138  
139 52 ▶ if ( ref $ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref) { } 52 0 52
140 52 $self->inflate_data($ref, \$inflated[-1]);
141 } else {
142 0 $self->queue_ref($ref, \$inflated[-1]);
143 }
144 }
145  
146 $self->queue_finalizer(sub {
147 28 ▶ $instance->STORABLE_thaw( 0, $str, @inflated );
Subroutine __ANON__: called CC=1 SCAR=0.0
148 28 });
149 72 };
150 } else {
151 # esotheric STORABLE_attach form
152 0 ▶ return sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
153 }
154 }
155  
156 sub _type ($) {
157 36 ▶ my $obj = shift;
Subroutine _type: called CC=8 SCAR=27.7
158  
159 36 my $type = reftype($obj);
160  
161 36 ▶ if ( $type eq 'SCALAR' or $type eq 'REF' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'SCALAR' or $type eq 'REF') { } 0 36 36
elsif ($type eq 'HASH') { } 3 33 36
elsif ($type eq 'ARRAY') { } 33 0 33
Condition33%1/3
$type eq "SCALAR" or $type eq "REF"
l!l&&r!l&&!r
0036
MC/DC0%0/2
$type eq "SCALAR" or $type eq "REF"
$type eq "SCALAR" $type eq "REF"
Truth table33%1/3
A or B
A= $type eq "SCALAR"B= $type eq "REF"
ABresult
1X1
011
000
162 0 ▶ if ( my $tied = tied $$obj ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tied = tied $$obj) { } 0 0 0
163 0 return ( S => $tied );
164 } else {
165 0 return 'S';
166 }
167 } elsif ( $type eq 'HASH' ) {
168 3 ▶ if ( my $tied = tied %$obj ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tied = tied %$obj) { } 0 3 3
169 0 return ( H => $tied );
170 } else {
171 3 return 'H';
172 }
173 } elsif ( $type eq 'ARRAY' ) {
174 33 ▶ if ( my $tied = tied @$obj ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tied = tied @$obj) { } 0 33 33
175 0 return ( A => $tied );
176 } else {
177 33 return 'A';
178 }
179 } else {
180 0 croak sprintf "Unexpected object type (%s)", reftype($obj);
181 }
182 }
183  
184 sub _new ($;$) {
185 28 ▶ my ( $type, $tied ) = @_;
Subroutine _new: called CC=7 SCAR=23.6
186  
187 28 ▶ if ( $type eq 'S' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'S') { } 0 28 28
elsif ($type eq 'H') { } 3 25 28
elsif ($type eq 'A') { } 25 0 25
188 0 my $ref = \( my $x );
189 0 ▶ tie $x, "To::Object", $tied if ref $tied;
Branch: 1 branch
BranchTrueFalseTotal
if ref $tied 0 0 0
190 0 return $ref;
191 } elsif ( $type eq 'H' ) {
192 3 my $ref = {};
193 3 ▶ tie %$ref, "To::Object", $tied if ref $tied;
Branch: 1 branch
BranchTrueFalseTotal
if ref $tied 0 3 3
194 3 return $ref;
195 } elsif ( $type eq 'A' ) {
196 25 my $ref = [];
197 25 ▶ tie @$ref, "To::Object", $tied if ref $tied;
Branch: 1 branch
BranchTrueFalseTotal
if ref $tied 0 25 25
198 25 return $ref;
199 } else {
200 0 croak sprintf "Unexpected object type (%d)", $type;
201 }
202 }
203  
204 sub _clear ($) {
205 my $obj = shift;
206  
207 my $type = reftype($obj);
208  
209 if ( $type eq 'SCALAR' or $type eq 'REF' ) {
210 undef $$obj;
211 } elsif ( $type eq 'HASH' ) {
212 %$obj = ();
213 } elsif ( $type eq 'ARRAY' ) {
214 @$obj = ();
215 } else {
216 croak sprintf "Unexpected object type (%s)", reftype($obj);
217 }
218 }
219  
220  
221 10 __PACKAGE__->meta->make_immutable;
222  
223 10 __PACKAGE__
224  
225 __END__
226  
227 =pod
228  
229 =head1 NAME
230  
231 KiokuDB::TypeMap::Entry::StorableHook - Reuse existing L<Storable> hooks for
232 L<KiokuDB> storage.
233  
234 =head1 SYNOPSIS
235  
236 use KiokuDB::TypeMap::Entry::StorableHook;
237  
238 =head1 DESCRIPTION
239  
240 =cut
241  
242  
« KiokuDB/Test.pm ↑ Summary KiokuDB/Test/Fixture/TypeMap/Default.pm »