blib/lib/PGObject/Util/DBMethod.pm

filter: Statementstmt 75.2% 64 / 85 Branchbran 50.0% 19 / 38 Conditioncond 44.4% 4 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 90.9% 10 / 11 Podpod 100% 1 / 1 totaltotal 66.6% 100 / 150 CC 44 SCAR 33.1
CC
44
Cov
66%
__ANON__
37.5
baz
37.5
strictargtest
37.5
CRAP 27.5
↑ Summary
1 package PGObject::Util::DBMethod;
2  
3 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Sub::Util qw( set_subname );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 PGObject::Util::DBMethod - Declarative stored procedure <-> object mappings for
12 the PGObject Framework
13  
14 =head1 VERSION
15  
16 Version 1.01.000
17  
18 =cut
19  
20 4 our $VERSION = '1.01.000';
21  
22  
23 =head1 SYNOPSIS
24  
25 Without PGObject::Util::DBobject, you would:
26  
27 sub mymethod {
28 my ($self) = @_;
29 return $self->call_dbmethod(funcname => 'foo');
30 }
31  
32 With this you'd do this instead:
33  
34 dbmethod mymethod => (funcname => 'foo');
35  
36 =head1 EXPORT
37  
38 This exports only dbmethod, which it always exports.
39  
40 =cut
41  
42 4 our @EXPORT = qw(dbmethod);
43  
44 =head1 SUBROUTINES/METHODS
45  
46 =head2 dbmethod
47  
48 use as dbmethod (name => (default_arghash))
49  
50 For example:
51  
52 package MyObject;
53 use PGObject::Utils::DBMethod;
54  
55 dbmethod save => (
56 strict_args => 0,
57 funcname => 'save_user',
58 funcschema => 'public',
59 args => { admin => 0 },
60 );
61 $MyObject->save(args => {username => 'foo', password => 'bar'});
62  
63 Special arguments are:
64  
65 =over
66  
67 =item arg_lit
68  
69 It set must point to a hashref. Used to allow mapping of function arguments
70 to arg hash elements. If this is set then funcname, funcschema, etc, cannot be
71 overwritten on the call.
72  
73 =item strict_args
74  
75 If true, args override args provided by user.
76  
77 =item returns_objects
78  
79 If true, bless returned hashrefs before returning them.
80  
81 =item merge_back
82  
83 If true, merges the first record back to the $self at the end before returning,
84 and returns $self. Note this is a copy only one layer deep which is fine for
85 the use case of merging return values from the database into the current
86 object.
87  
88 =back
89  
90 =cut
91  
92 10 ▶sub dbmethod {
Subroutine dbmethod: called CC=1 SCAR=1.0
Pod documented
93 10 my $name = shift;
94 10 my %defaultargs = @_;
95 10 my ($target) = caller;
96  
97 0 ▶ my $coderef = sub {
Subroutine __ANON__: not called CC=13 SCAR=36.2
Subroutine strictargtest: called CC=13 SCAR=36.2
98 0 my $self = shift @_;
99 0 my %args;
100 0 ▶ if ($defaultargs{arg_list}){
Branch: 2 branches
BranchTrueFalseTotal
if ($defaultargs{'arg_list'}) { } 0 0 0
if ($defaultargs{'arg_list'}) { } 1 10 11
101 0 %args = ( args => _process_args($defaultargs{arg_list}, @_) );
102 } else {
103 0 %args = @_;
104 }
105 0 for my $key (keys %{$defaultargs{args}}){
106 $args{args}->{$key} = $defaultargs{args}->{$key}
107 0 ▶ unless $args{args}->{$key} or $defaultargs{strict_args};
Branch: 2 branches
BranchTrueFalseTotal
unless $args{"args"}{$key} or $defaultargs{"strict_args"} 0 0 0
unless $args{"args"}{$key} or $defaultargs{"strict_args"} 1 5 6
Condition33%2/6
$args{"args"}{$key} or $defaultargs{"strict_args"}
l!l&&r!l&&!r
000
$args{"args"}{$key} or $defaultargs{"strict_args"}
l!l&&r!l&&!r
501
MC/DC0%0/2
$args{"args"}{$key} or $defaultargs{"strict_args"}
$args{"args"}{$key} $defaultargs{"strict_args"}
MC/DC50%1/2
$args{"args"}{$key} or $defaultargs{"strict_args"}
$args{"args"}{$key} $defaultargs{"strict_args"}
Truth table0%0/3
A or B
A= $args{"args"}{$key}B= $defaultargs{"strict_args"}
ABresult
1X1
011
000
Truth table66%2/3
A or B
A= $args{"args"}{$key}B= $defaultargs{"strict_args"}
ABresult
1X1
011
000
108 $args{args}->{$key} = $defaultargs{args}->{$key}
109 0 ▶ if $defaultargs{strict_args};
Branch: 2 branches
BranchTrueFalseTotal
if $defaultargs{"strict_args"} 0 0 0
if $defaultargs{"strict_args"} 3 3 6
110 }
111 0 for my $key(keys %defaultargs){
112 0 ▶ next if grep(/^$key$/, qw(strict_args args returns_objects));
Branch: 2 branches
BranchTrueFalseTotal
if grep /^$key$/, ("strict_args", "args", "returns_objects") 0 0 0
if grep /^$key$/, ("strict_args", "args", "returns_objects") 15 24 39
113 0 ▶ $args{$key} = $defaultargs{$key} if $defaultargs{$key};
Branch: 2 branches
BranchTrueFalseTotal
if $defaultargs{$key} 0 0 0
if $defaultargs{$key} 24 0 24
114 }
115 0 my @results = $self->call_dbmethod(%args);
116 0 ▶ if ($defaultargs{returns_objects}){
Branch: 2 branches
BranchTrueFalseTotal
if ($defaultargs{"returns_objects"}) 0 0 0
if ($defaultargs{"returns_objects"}) 1 10 11
117 0 for my $ref(@results){
118 0 $ref = "$target"->new(%$ref);
119 }
120 }
121 0 ▶ if ($defaultargs{merge_back}){
Branch: 2 branches
BranchTrueFalseTotal
if ($defaultargs{"merge_back"}) 0 0 0
if ($defaultargs{"merge_back"}) 5 6 11
122 0 _merge($self, shift @results);
123 0 return $self;
124 }
125 0 ▶ return shift @results unless wantarray;
Branch: 2 branches
BranchTrueFalseTotal
unless wantarray 0 0 0
unless wantarray 1 5 6
126 0 return @results;
127 10 };
128 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
129 10 set_subname "${target}::${name}", $coderef;
130 10 *{"${target}::${name}"} = $coderef;
131 }
132  
133 # private function _merge($dest, $src)
134 # used to merge incoming db rows to a hash ref.
135 # hash table entries in $src overwrite those in $dest.
136 # Since this is an incoming row, we can generally assume we are not having to
137 # do a deep copy.
138  
139 5 ▶sub _merge {
Subroutine _merge: called CC=6 SCAR=18.1
140 5 my ($dest, $src) = @_;
141 5 ▶ if (eval {$dest->can('has') and $dest->can('extends')}){
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $dest->can('has') and $dest->can('extends') } }) { } 4 1 5
Condition66%2/3
$dest->can('has') && $dest->can('extends')
!ll&&!rl&&r
104
MC/DC50%1/2
$dest->can('has') && $dest->can('extends')
$dest->can('has') $dest->can('extends')
Truth table66%2/3
A && B
A= $dest->can('has')B= $dest->can('extends')
ABresult
0X0
100
111
142 # Moo or Moose. Use accessors, though better would be to just return
143 # objects in this case.
144 4 for my $att (keys %$src){
145 7 ▶ $dest->can($att)->($dest, $src->{$att}) if $dest->can($att);
Branch: 1 branch
BranchTrueFalseTotal
if $dest->can($att) 7 0 7
146 }
147 } else {
148 1 $dest->{$_} = $src->{$_} for (keys %$src);
149 }
150 }
151  
152 # private method _process_args.
153 # first arg $arrayref of argnames
154 # after that we just pass in @_ from the function call
155 # then we return a hash with the args as specified.
156  
157 1 ▶sub _process_args {
Subroutine _process_args: called CC=3 SCAR=11.0
158 1 my $arglist = shift @_;
159 1 my @args = @_;
160  
161 1 my $arghref = {};
162  
163 1 my $maxlen = scalar @_;
164 1 my $it = 1;
165 1 for my $argname (@$arglist){
166 1 ▶ last if $it > $maxlen;
Branch: 1 branch
BranchTrueFalseTotal
if $it > $maxlen 0 1 1
167 1 $arghref->{$argname} = shift @args;
168 1 ++$it;
169 }
170 1 return $arghref;
171 }
172  
173 =head1 AUTHOR
174  
175 Chris Travers, C<< <chris.travers at gmail.com> >>
176  
177 =head1 BUGS
178  
179 Please report any bugs or feature requests to C<bug-pgobject-util-dbmethod at rt.cpan.org>, or through
180 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PGObject-Util-DBMethod>. I will be notified, and then you'll
181 automatically be notified of progress on your bug as I make changes.
182  
183  
184  
185  
186 =head1 SUPPORT
187  
188 You can find documentation for this module with the perldoc command.
189  
190 perldoc PGObject::Util::DBMethod
191  
192  
193 You can also look for information at:
194  
195 =over 4
196  
197 =item * RT: CPAN's request tracker (report bugs here)
198  
199 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=PGObject-Util-DBMethod>
200  
201 =item * AnnoCPAN: Annotated CPAN documentation
202  
203 L<http://annocpan.org/dist/PGObject-Util-DBMethod>
204  
205 =item * CPAN Ratings
206  
207 L<http://cpanratings.perl.org/d/PGObject-Util-DBMethod>
208  
209 =item * Search CPAN
210  
211 L<http://search.cpan.org/dist/PGObject-Util-DBMethod/>
212  
213 =back
214  
215  
216 =head1 ACKNOWLEDGEMENTS
217  
218  
219 =head1 LICENSE AND COPYRIGHT
220  
221 Copyright 2014 Chris Travers.
222  
223 This program is released under the following license: BSD
224  
225  
226 =cut
227  
228 4 1; # End of PGObject::Util::DBMethod
↑ Summary