blib/lib/PGObject/Type/Composite.pm

filter: Statementstmt 77.9% 67 / 86 Branchbran 30.7% 8 / 26 Conditioncond 23.0% 3 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 84.6% 11 / 13 Podpod n/a totaltotal 60.9% 89 / 146 CC 20 SCAR 30.7
CC
20
Cov
62%
__ANON__
72.0
__ANON__
9.1
__ANON__
7.0
CRAP 21.6
↑ Summary
1 package PGObject::Type::Composite;
2  
3 6 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use PGObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use PGObject::Util::Catalog::Types qw(get_attributes);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use PGObject::Util::PseudoCSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 PGObject::Type::Composite - Composite Type handler for PGObject
13  
14 =head1 VERSION
15  
16 Version 1
17  
18 =cut
19  
20 6 our $VERSION = 1.000000;
21  
22  
23 =head1 SYNOPSIS
24  
25 package MyObject;
26 use Moo;
27 with 'PGObject::Type::Composite';
28  
29 Then
30  
31 use MyObject;
32 my $dbh = DBI->connect;
33 MyObject->initialize(dbh => $dbh);
34 MyObject->register(registry => 'default', type => 'foo');
35  
36 And now every column of type foo (which must be a composite type) will get
37 deserialized into MyObject.
38  
39 =head1 EXPORTS
40  
41 =over
42  
43 =item initialize
44  
45 =item from_db
46  
47 =item to_db
48  
49 =back
50  
51 =cut
52  
53 =head1 SUBROUTINES/METHODS
54  
55 =head2 initialize
56  
57 =head2 register
58  
59 =head2 from_db
60  
61 =head2 to_db
62  
63 =cut
64  
65 5 ▶sub import {
Subroutine import: called CC=2 SCAR=8.5
66 5 my ($importer) = caller;
67 5 my @cols;
68 5 ▶ my $can_has if *{ "${importer}::has" }; # moo/moose lolmoose?
Branch: 1 branch
BranchTrueFalseTotal
if *{"${importer}::has";} 5 0 5
69  
70 3 ▶ my $initialize = sub {
Subroutine __ANON__: called CC=7 SCAR=22.0
71 3 my ($pkg, %args) = @_;
72 3 ▶ croak 'first argument must be a package name' if ref $pkg;
Branch: 1 branch
BranchTrueFalseTotal
if ref $pkg 0 3 3
73 croak 'Must supply a dbh or columns argument'
74 3 ▶ unless $args{dbh} or scalar @{$args{columns}};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"dbh"} or scalar @{$args{"columns"};} 0 3 3
Condition50%1/2
$args{"dbh"} or scalar @{$args{"columns"};}
l!l
03
MC/DC0%0/1
$args{"dbh"} or scalar @{$args{"columns"};}
$args{"dbh"}
Truth table50%1/2
A
A= $args{"dbh"}
Aresult
11
00
75  
76 3 ▶ @cols = @{$args{columns}} if @{$args{columns}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$args{"columns"};} 3 0 3
77 3 ▶ if ($args{dbh} and !@cols){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"dbh"} and not @cols) 0 3 3
Condition33%1/3
$args{"dbh"} and not @cols
!ll&&!rl&&r
300
MC/DC0%0/2
$args{"dbh"} and not @cols
$args{"dbh"} not @cols
Truth table33%1/3
A and not(B)
A= $args{"dbh"}B= not @cols
ABresult
0X0
100
111
78 @cols = get_attributes(
79 typeschema => "$pkg"->_get_schema,
80 typename => "$pkg"->_get_typename,
81 dbh => $args{dbh}
82 0 );
83 }
84 3 return @cols;
85 5 };
86  
87 6 ▶ my $from_db = sub {
Subroutine __ANON__: called CC=5 SCAR=19.4
88 6 my ($to_pkg, $string) = @_;
89 6 my $hashref = pcsv2hash($string, map { $_->{attname}} @cols);
90 6 ▶ if ($PGObject::VERSION =~ /^1./){
Branch: 1 branch
BranchTrueFalseTotal
if ($PGObject::VERSION =~ /^1./) { } 0 6 6
91 $hashref = {
92 0 map { $_->{attname} => PGObject::process_type(
93 $hashref->{$_->{attname}},
94 $_->{atttype},
95 0 ▶ (eval {$to_pkg->can('_get_registry')} ?
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $to_pkg->can('_get_registry') } } ? : 0 0 0
96 "$to_pkg"->_get_registry :
97 'default'))
98 } @cols
99 };
100 } else {
101 $hashref = {
102 6 map { $_->{attname} => PGObject::Type::Registry->deserialize(
103 dbstring => $hashref->{$_->{attname}},
104 dbtype => $_->{atttype},
105 registry =>
106 18 ▶ (eval {$to_pkg->can('_get_registry')} ?
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $to_pkg->can('_get_registry') } } ? : 0 18 18
107 "$to_pkg"->_get_registry :
108 'default'))
109 } @cols
110 };
111 }
112 6 ▶ if ($can_has){ # moo/moose
Branch: 1 branch
BranchTrueFalseTotal
if ($can_has) { } 0 6 6
113 0 return "$pkg"->new(%$hashref);
114 } else {
115 6 return bless($hashref, $to_pkg);
116 }
117 5 };
118  
119 3 ▶ my $to_db = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
120 3 my ($self) = @_;
121 my $hashref = { map {
122 3 my $att = $_->{attname};
123 9 ▶ my $val = eval { $self->$att } || $self->{$att};
Condition33%1/3
eval { do { $self->$att } } || $self->{$att}
l!l&&r!l&&!r
090
MC/DC0%0/2
eval { do { $self->$att } } || $self->{$att}
eval { do { $self->$att } } $self->{$att}
Truth table33%1/3
A || B
A= eval { do { $self->$att } }B= $self->{$att}
ABresult
1X1
011
000
124 9 $att => $val;
125 } @cols };
126 return {
127 type => $typename,
128 3 value => hash2pcsv($hashref, map {$_->{attname}} @cols),
129 };
130 5 };
131  
132 0 ▶ my $register = sub { # easier here than also doing export
Subroutine __ANON__: not called CC=8 SCAR=42.8
133 0 my $self = shift @_;
134 0 ▶ croak "Can't pass reference to register \n".
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 0 0 0
135 "Hint: use the class instead of the object" if ref $self;
136 0 my %args = @_;
137 0 my $registry = $args{registry};
138 0 ▶ $registry ||= 'default';
Condition0%0/2
$registry ||= "default"
l!l
00
MC/DC0%0/1
$registry ||= "default"
$registry
Truth table0%0/2
A
A= $registry
Aresult
11
00
139 0 my $types = $args{types};
140 0 ▶ croak 'Must supply types as arrayref'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $types and @$types 0 0 0
Condition0%0/3
defined $types and @$types
!ll&&!rl&&r
000
MC/DC0%0/2
defined $types and @$types
defined $types @$types
Truth table0%0/3
A and B
A= defined $typesB= @$types
ABresult
0X0
100
111
141 unless defined $types and @$types;
142 0 for my $type (@$types){
143 0 ▶ if ($PGObject::VERSION =~ /^1./){
Branch: 1 branch
BranchTrueFalseTotal
if ($PGObject::VERSION =~ /^1./) { } 0 0 0
144 0 my $ret =
145 PGObject->register_type(registry => $registry,
146 pg_type => $type,
147 perl_class => "$self");
148 0 ▶ return $ret unless $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 0 0 0
149 } else {
150 0 my $ret =
151 PGObject::Type::Registry->register_type(
152 registry => $registry,
153 dbtype => $type,
154 apptype => "$self");
155 }
156 0 return 1;
157 }
158 5 };
159 0 ▶ my $_get_cols = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 0 return @cols;
161 5 };
162  
163 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
164 5 *{ "${importer}::initialize" } = $initialize;
165 5 *{ "${importer}::register" } = $register;
166 5 *{ "${importer}::from_db" } = $from_db;
167 5 *{ "${importer}::to_db" } = $to_db;
168 5 *{ "${importer}::_get_cols" } = $_get_cols;
169 }
170  
171 =head1 AUTHOR
172  
173 Chris Travers, C<< <chris.travers at gmail.com> >>
174  
175 =head1 BUGS
176  
177 Please report any bugs or feature requests to C<bug-pgobject-type-composite at rt.cpan.org>, or through
178 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PGObject-Type-Composite>. I will be notified, and then you'll
179 automatically be notified of progress on your bug as I make changes.
180  
181  
182  
183  
184 =head1 SUPPORT
185  
186 You can find documentation for this module with the perldoc command.
187  
188 perldoc PGObject::Type::Composite
189  
190  
191 You can also look for information at:
192  
193 =over 4
194  
195 =item * RT: CPAN's request tracker (report bugs here)
196  
197 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=PGObject-Type-Composite>
198  
199 =item * AnnoCPAN: Annotated CPAN documentation
200  
201 L<http://annocpan.org/dist/PGObject-Type-Composite>
202  
203 =item * CPAN Ratings
204  
205 L<http://cpanratings.perl.org/d/PGObject-Type-Composite>
206  
207 =item * Search CPAN
208  
209 L<http://search.cpan.org/dist/PGObject-Type-Composite/>
210  
211 =back
212  
213  
214 =head1 ACKNOWLEDGEMENTS
215  
216  
217 =head1 LICENSE AND COPYRIGHT
218  
219 Copyright 2014 Chris Travers.
220  
221 This program is distributed under the (Revised) BSD License:
222 L<http://www.opensource.org/licenses/BSD-3-Clause>
223  
224 Redistribution and use in source and binary forms, with or without
225 modification, are permitted provided that the following conditions
226 are met:
227  
228 * Redistributions of source code must retain the above copyright
229 notice, this list of conditions and the following disclaimer.
230  
231 * Redistributions in binary form must reproduce the above copyright
232 notice, this list of conditions and the following disclaimer in the
233 documentation and/or other materials provided with the distribution.
234  
235 * Neither the name of Chris Travers's Organization
236 nor the names of its contributors may be used to endorse or promote
237 products derived from this software without specific prior written
238 permission.
239  
240 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
241 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
242 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
243 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
244 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
245 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
246 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
247 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
248 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
249 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
250 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251  
252  
253 =cut
254  
255 6 1; # End of PGObject::Type::Composite
↑ Summary