KiokuDB/Backend/Serialize/JSPON/Expander.pm

filter: Statementstmt 96.6% 87 / 90 Branchbran 68.5% 48 / 70 Conditioncond 83.3% 10 / 12 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 84.6% 160 / 189 CC 42 SCAR 31.5
CC
42
Cov
84%
__ANON__
34.1
__ANON__
22.3
_build__expander
2.0
CRAP 23.2
↑ Summary KiokuDB.pm »
1 #!/usr/bin/perl
2  
3 package KiokuDB::Backend::Serialize::JSPON::Expander;
4 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use KiokuDB::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use KiokuDB::Reference;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 with qw(KiokuDB::Backend::Serialize::JSPON::Converter);
15  
16 sub expand_jspon {
17 1754 ▶ my ( $self, $data, @attrs ) = @_;
Subroutine expand_jspon: called CC=1 SCAR=0.0
Pod documented
18  
19 1754 return $self->_expander->($data, @attrs);
20 }
21  
22 6 has _expander => (
23 isa => "CodeRef",
24 is => "ro",
25 lazy_build => 1,
26 );
27  
28 sub _build__expander {
29 19 ▶ my $self = shift;
Subroutine _build__expander: called CC=2 SCAR=7.1
30  
31 19 my $expander;
32  
33 my (
34 $ref_field, $id_field, $data_field,
35 $class_field, $tied_field, $root_field,
36 $deleted_field, $class_meta_field, $class_version_field,
37 $backend_data_field
38 )
39 19 = map { my $m = $_ . "_field"; $self->$m() }
40 qw(ref id data class tied root deleted class_meta class_version backend_data);
41  
42 19 ▶ unless ( $self->inline_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->inline_data) { } 1 18 19
43 18 my $data_field_re = qr/\. \Q$data_field\E $/x;
44  
45 $expander = sub {
46 9686 ▶ my ( $data, @attrs ) = @_;
Subroutine __ANON__: called CC=22 SCAR=31.0
47  
48 9686 ▶ if ( my $ref = ref($data) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $data) 9638 48 9686
49 9638 ▶ if ( $ref eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 5028 4610 9638
elsif (ref $data eq 'ARRAY') { } 4610 0 4610
50 5028 ▶ if ( my $id = $data->{$ref_field} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $id = $data->{$ref_field}) { } 1476 3552 5028
elsif (exists $data->{$class_field} or exists $data->{$id_field} or exists $data->{$tied_field}) { } 1808 1744 3552
51 1476 $id =~ s/$data_field_re//;
52 1476 ▶ return KiokuDB::Reference->new( id => $id, ( $data->{weak} ? ( is_weak => 1 ) : () ) );
Branch: 1 branch
BranchTrueFalseTotal
$data->{'weak'} ? : 546 930 1476
53 ▶ } elsif ( exists $data->{$class_field}
Condition100%6/6
exists $data->{$class_field} or exists $data->{$id_field} or exists $data->{$tied_field}
l!l&&r!l&&!r
180621744
exists $data->{$class_field} or exists $data->{$id_field}
l!l&&r!l&&!r
1748581746
MC/DC100%3/3
exists $data->{$class_field} or exists $data->{$id_field} or exists $data->{$tied_field}
exists $data->{$class_field} exists $data->{$id_field} exists $data->{$tied_field}
Truth table100%4/4
A or B or C
A= exists $data->{$class_field}B= exists $data->{$id_field}C= exists $data->{$tied_field}
ABCresult
1XX1
01X1
0011
0000
54 or exists $data->{$id_field}
55 or exists $data->{$tied_field}
56 ) {
57 1808 ▶ if ( exists $data->{$class_field} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{$class_field}) 1748 60 1808
58 # check the class more thoroughly here ...
59 1748 my ($class, $version, $authority) = (split '-' => $data->{$class_field});
60 1748 push @attrs, class => $class;
61  
62 1748 ▶ push @attrs, class_meta => $data->{$class_meta_field} if exists $data->{$class_meta_field};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{$class_meta_field} 10 1738 1748
63 1748 ▶ push @attrs, class_version => $data->{$class_version_field} if exists $data->{$class_version_field};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{$class_version_field} 6 1742 1748
64 }
65  
66 1808 ▶ push @attrs, id => $data->{$id_field} if exists $data->{$id_field};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{$id_field} 1749 59 1808
67 1808 ▶ push @attrs, tied => substr($data->{$tied_field}, 0, 1) if exists $data->{$tied_field};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{$tied_field} 10 1798 1808
68 1808 ▶ push @attrs, root => $data->{$root_field} ? 1 : 0 if exists $data->{$root_field};
Branch: 2 branches
BranchTrueFalseTotal
if exists $data->{$root_field} 996 812 1808
$data->{$root_field} ? : 996 0 996
69 1808 ▶ push @attrs, deleted => $data->{$deleted_field} ? 1 : 0 if exists $data->{$deleted_field};
Branch: 2 branches
BranchTrueFalseTotal
if exists $data->{$deleted_field} 0 1808 1808
$data->{$deleted_field} ? : 0 0 0
70 1808 ▶ push @attrs, backend_data => $data->{$backend_data_field} if exists $data->{$backend_data_field};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{$backend_data_field} 0 1808 1808
71  
72 1808 push @attrs, data => $expander->( $data->{$data_field} );
73  
74 1808 return KiokuDB::Entry->new( @attrs );
75 } else {
76 1744 my %hash;
77  
78 1744 foreach my $key ( keys %$data ) {
79 7619 my $unescaped = $key;
80 7619 $unescaped =~ s/^public:://;
81  
82 7619 my $value = $data->{$key};
83 7619 ▶ $hash{$unescaped} = ref($value) ? $expander->($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 4896 2723 7619
84 }
85  
86 1744 return \%hash;
87 }
88 } elsif ( ref $data eq 'ARRAY' ) {
89 4610 ▶ return [ map { ref($_) ? $expander->($_) : $_ } @$data ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 1229 65 1294
90 }
91 }
92  
93 48 return $data;
94 }
95 18 } else {
96 $expander = sub {
97 4 ▶ my ( $data, @attrs ) = @_;
Subroutine __ANON__: called CC=20 SCAR=35.3
98  
99 4 ▶ if ( my $ref = ref($data) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $data) 4 0 4
100 4 ▶ if ( $ref eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 4 0 4
elsif (ref $data eq 'ARRAY') { } 0 0 0
101  
102 4 ▶ if ( my $id = $data->{$ref_field} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $id = $data->{$ref_field}) { } 1 3 4
elsif (exists $data->{$class_field} or exists $data->{$id_field} or exists $data->{$tied_field}) { } 1 2 3
103 1 ▶ return KiokuDB::Reference->new( id => $id, ( $data->{weak} ? ( is_weak => 1 ) : () ) );
Branch: 1 branch
BranchTrueFalseTotal
$data->{'weak'} ? : 1 0 1
104 ▶ } elsif ( exists $data->{$class_field}
Condition66%4/6
exists $data->{$class_field} or exists $data->{$id_field} or exists $data->{$tied_field}
l!l&&r!l&&!r
102
exists $data->{$class_field} or exists $data->{$id_field}
l!l&&r!l&&!r
102
MC/DC33%1/3
exists $data->{$class_field} or exists $data->{$id_field} or exists $data->{$tied_field}
exists $data->{$class_field} exists $data->{$id_field} exists $data->{$tied_field}
Truth table50%2/4
A or B or C
A= exists $data->{$class_field}B= exists $data->{$id_field}C= exists $data->{$tied_field}
ABCresult
1XX1
01X1
0011
0000
105 or exists $data->{$id_field}
106 or exists $data->{$tied_field}
107 ) {
108 1 my %copy = %$data;
109  
110 1 ▶ if ( exists $copy{$class_field} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $copy{$class_field}) 1 0 1
111 # check the class more thoroughly here ...
112 1 my ($class, $version, $authority) = (split '-' => delete $copy{$class_field});
113 1 push @attrs, class => $class;
114  
115 1 ▶ push @attrs, class_meta => delete $copy{$class_meta_field} if exists $copy{$class_meta_field};
Branch: 1 branch
BranchTrueFalseTotal
if exists $copy{$class_meta_field} 1 0 1
116 }
117  
118 1 ▶ push @attrs, id => delete $copy{$id_field} if exists $copy{$id_field};
Branch: 1 branch
BranchTrueFalseTotal
if exists $copy{$id_field} 1 0 1
119 1 ▶ push @attrs, tied => delete $copy{$tied_field} if exists $copy{$tied_field};
Branch: 1 branch
BranchTrueFalseTotal
if exists $copy{$tied_field} 0 1 1
120 1 ▶ push @attrs, root => delete $copy{$root_field} ? 1 : 0 if exists $copy{$root_field};
Branch: 2 branches
BranchTrueFalseTotal
if exists $copy{$root_field} 1 0 1
delete $copy{$root_field} ? : 1 0 1
121 1 ▶ push @attrs, deleted => delete $copy{$deleted_field} ? 1 : 0 if exists $copy{$deleted_field};
Branch: 2 branches
BranchTrueFalseTotal
if exists $copy{$deleted_field} 0 1 1
delete $copy{$deleted_field} ? : 0 0 0
122  
123 1 push @attrs, data => $expander->( \%copy );
124  
125 1 return KiokuDB::Entry->new( @attrs );
126 } else {
127 2 my %hash;
128  
129 2 foreach my $key ( keys %$data ) {
130 5 my $unescaped = $key;
131 5 $unescaped =~ s/^public:://;
132  
133 5 my $value = $data->{$key};
134 5 ▶ $hash{$unescaped} = ref($value) ? $expander->($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 2 3 5
135 }
136  
137 2 return \%hash;
138 }
139 } elsif ( ref $data eq 'ARRAY' ) {
140 0 ▶ return [ map { ref($_) ? $expander->($_) : $_ } @$data ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
141 }
142 }
143  
144 0 return $data;
145 }
146 1 }
147  
148 19 my $copy = $expander;
149 19 weaken($expander);
150  
151 19 return $copy;
152 }
153  
154 6 __PACKAGE__->meta->make_immutable;
155  
156 6 __PACKAGE__
157  
158 __END__
159  
160 =pod
161  
162 =head1 NAME
163  
164 KiokuDB::Backend::Serialize::JSPON::Expander - Inflate JSPON to entry
165 data.
166  
167 =head1 SYNOPSIS
168  
169 my $c = KiokuDB::Backend::Serialize::JSPON::Expander->new(
170 id_field => "_id",
171 );
172  
173 my $entry = $c->collapse_jspon($hashref);
174  
175 =head1 DESCRIPTION
176  
177 This object is used by L<KiokuDB::Backend::Serialize::JSPON> to expand JSPON
178 compliant hash references to L<KiokuDB::Entry> objects.
179  
180 =head1 ATTRIBUTES
181  
182 See L<KiokuDB::Backend::Serialize::JSPON::Converter> for attributes shared by
183 L<KiokuDB::Backend::Serialize::JSPON::Collapser> and
184 L<KiokuDB::Backend::Serialize::JSPON::Expander>.
185  
186 =head1 METHODS
187  
188 =over 4
189  
190 =item expand_jspon $hashref
191  
192 Recursively inlates the hash reference, returning a L<KiokuDB::Entry> object.
193  
194 =back
↑ Summary KiokuDB.pm »