KiokuDB/TypeMap/Entry/Callback.pm

filter: Statementstmt 86.1% 56 / 65 Branchbran 83.3% 10 / 12 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 78.5% 11 / 14 Podpod 0.0% 0 / 4 totaltotal 82.0% 82 / 100 CC 9 SCAR 9.7
CC
9
Cov
86%
_entry_data_to_args
5.0
compile_refresh
3.0
__ANON__
3.0
CRAP 2.6
« KiokuDB/Meta/Attribute/Lazy.pm ↑ Summary KiokuDB/Backend/Hash.pm »
1 #!/usr/bin/perl
2  
3 package KiokuDB::TypeMap::Entry::Callback;
4 15 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 15 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 15 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 15 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 15 with qw(KiokuDB::TypeMap::Entry::Std);
13  
14 15 has [qw(collapse expand)] => (
15 is => "ro",
16 isa => "Str|CodeRef",
17 required => 1,
18 );
19  
20 15 has [qw(id refresh)] => (
21 is => "ro",
22 isa => "Str|CodeRef",
23 );
24  
25 sub compile_collapse_body {
26 38 ▶ my ( $self, $class, @args ) = @_;
Subroutine compile_collapse_body: called CC=1 SCAR=0.0
Pod undocumented
27  
28 38 my $collapse_object = $self->collapse;
29  
30 return sub {
31 57 ▶ my ( $self, %args ) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
32  
33 57 my @data = $args{object}->$collapse_object;
34  
35 57 my $data;
36  
37 57 ▶ if ( @data == 1 and not ref $data[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@data == 1 and not ref $data[0]) { } 48 9 57
Condition100%3/3
@data == 1 and not ref $data[0]
!ll&&!rl&&r
5448
MC/DC100%2/2
@data == 1 and not ref $data[0]
@data == 1 not ref $data[0]
Truth table100%3/3
A and not(B)
A= @data == 1B= not ref $data[0]
ABresult
0X0
100
111
38 48 $data = $data[0];
39 } else {
40 9 $data = [ map { $self->visit($_) } @data ];
41 }
42  
43 57 return $self->make_entry(
44 %args,
45 data => $data,
46 );
47 38 };
48 }
49  
50 sub _entry_data_to_args {
51 50 ▶ my ( $self, $linker, $entry ) = @_;
Subroutine _entry_data_to_args: called CC=5 SCAR=16.1
52  
53 50 my $data = $entry->data;
54  
55 50 ▶ if ( ref $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data) { } 6 44 50
56 6 my @args;
57  
58 6 my $refs = 0;
59  
60 6 foreach my $value ( @$data ) {
61 11 ▶ if ( ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 7 4 11
62 7 push @args, undef;
63 7 $linker->inflate_data($value, \$args[-1]);
64 7 $refs++;
65 } else {
66 4 push @args, $value;
67 }
68 }
69  
70 6 ▶ $linker->load_queue if $refs; # force @args to be fully vivified
Branch: 1 branch
BranchTrueFalseTotal
if $refs 5 1 6
71  
72 6 return @args;
73 } else {
74 44 return $data;
75 }
76 }
77  
78 sub compile_expand {
79 38 ▶ my ( $self, $class, @args ) =@_;
Subroutine compile_expand: called CC=1 SCAR=0.0
Pod undocumented
80  
81 38 my $expand_object = $self->expand;
82  
83 return sub {
84 50 ▶ my ( $linker, $entry ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
85  
86 50 my @args = $self->_entry_data_to_args($linker, $entry);
87  
88 # does *NOT* support circular refs
89 50 my $object = $entry->class->$expand_object(@args);
90  
91 50 $linker->register_object( $entry => $object );
92  
93 50 return $object;
94 38 };
95 }
96  
97  
98 sub compile_refresh {
99 38 ▶ my ( $self, $class, @args ) = @_;
Subroutine compile_refresh: called CC=2 SCAR=11.1
Pod undocumented
100  
101 38 ▶ if ( my $refresh_object = $self->refresh ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $refresh_object = $self->refresh) { } 0 38 38
102 return sub {
103 0 ▶ my ( $linker, $object, $entry ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
104  
105 0 my @args = $self->_entry_data_to_args($linker, $entry);
106  
107 0 $object->$refresh_object(@args);
108  
109 0 return $object;
110 0 };
111 } else {
112 return sub {
113 0 ▶ croak "No refresh method provided for $class by typemap entry $self";
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 38 };
115 }
116 }
117  
118 sub compile_id {
119 38 ▶ my ( $self, $class, @args ) = @_;
Subroutine compile_id: called CC=2 SCAR=9.2
Pod undocumented
120  
121 38 ▶ if ( my $get_id = $self->id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $get_id = $self->id) { } 0 38 38
122 return sub {
123 0 ▶ my ( $self, $object ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
124 0 $object->$get_id;
125 0 };
126 } else {
127 38 return "generate_uuid";
128 }
129 }
130  
131 15 __PACKAGE__->meta->make_immutable;
132  
133 15 __PACKAGE__
134  
135 __END__
136  
137 =pod
138  
139 =head1 NAME
140  
141 KiokuDB::TypeMap::Entry::Callback - Callback based inflation/deflation of objects
142  
143 =head1 SYNOPSIS
144  
145 KiokuDB::TypeMap->new(
146 entries => {
147 'My::Class' => KiokuDB::TypeMap::Entry::Callback->new(
148 expand => "new", # My::Class->new(%$self)
149 collapse => sub {
150 my $self = shift;
151 return %$self; # provide args to 'new' in this example
152 },
153 id => sub { "foo" }, # 'id' callback is optional
154 ),
155 },
156 );
157  
158 =head1 DESCRIPTION
159  
160 This L<KiokuDB::TypeMap> entry provides callback based inflation/deflation.
161  
162 The major limitation of this method is that it cannot be used for self
163 referential structures. This is because the object being inflated is only
164 constructed after all of its arguments are.
165  
166 For the overwhelming majority of the use cases this is good enough though.
167  
168 =head1 ATTRIBUTES
169  
170 =over 4
171  
172 =item collapse
173  
174 A method name or code reference invoked on the object during collapsing.
175  
176 This is evaluated in list context, and the list of values it returns will be
177 collapsed by the L<KiokuDB::Collapser> and then store.
178  
179 =item expand
180  
181 A method name or code reference invoked on the class of the object during loading.
182  
183 The arguments are as returned by the C<collapse> callback.
184  
185 =item id
186  
187 An optional method name or code reference invoked to get an ID for the object.
188  
189 If one is not provided the default (UUID based) generation is used.
190  
191 =item intrinsic
192  
193 A boolean denoting whether or not the object should be collapsed with no ID,
194 and serialized as part of its parent object.
195  
196 This is useful for value like objects, for whom the reference address makes no
197 difference (such as L<URI> objects).
198  
199 =back
« KiokuDB/Meta/Attribute/Lazy.pm ↑ Summary KiokuDB/Backend/Hash.pm »