blib/lib/Rose/DBx/Object/MoreHelpers.pm

filter: Statementstmt 48.7% 58 / 119 Branchbran 15.2% 7 / 46 Conditioncond 8.3% 1 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 54.5% 12 / 22 Podpod 100% 12 / 12 totaltotal 41.1% 90 / 219 CC 32 SCAR 30.0
CC
32
Cov
37%
exists
56.0
has_related_pages
42.0
has_related
30.0
CRAP 20.0
↑ Summary
1 package Rose::DBx::Object::MoreHelpers;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Rose::Object::MixIn;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 our @ISA = qw(Rose::Object::MixIn);
8  
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Data::Dump qw( dump );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Rose::DB::Object::Manager;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Rose::DB::Object::Metadata::Relationship::OneToMany;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 Rose::DB::Object::Metadata::Relationship::OneToMany
16 ->default_auto_method_types(
17 qw(
18 find
19 get_set_on_save
20 add_on_save
21 count
22 iterator
23 )
24 );
25 2 ▶use Rose::DB::Object::Metadata::Relationship::ManyToMany;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 Rose::DB::Object::Metadata::Relationship::ManyToMany
27 ->default_auto_method_types(
28 qw(
29 find
30 get_set_on_save
31 add_on_save
32 count
33 iterator
34 )
35 );
36  
37 2 ▶use Rose::Class::MakeMethods::Generic ( scalar => ['debug'], );
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 2 our $VERSION = '0.100';
40  
41 2 __PACKAGE__->export_tags(
42 all => [
43 qw(
44 primary_key_uri_escaped primary_key_value
45 flatten exists has_related has_related_pages
46 fetch_all fetch_all_iterator fetch_count
47 )
48 ],
49  
50 # This exists for the benefit of the test suite
51 all_noprereq => [
52 qw(
53 primary_key_uri_escaped primary_key_value
54 flatten exists has_related has_related_pages
55 fetch_all fetch_all_iterator fetch_count
56 )
57 ],
58 );
59  
60 =head1 NAME
61  
62 Rose::DBx::Object::MoreHelpers - more mixin helpers for RDBO
63  
64 =head1 SYNOPSIS
65  
66 package MyRDBO;
67 use base qw( Rose::DB::Object );
68 use Rose::DBx::Object::MoreHelpers;
69
70 =head1 DESCRIPTION
71  
72 In the tradition of Rose::DB::Object::Helpers, more importable methods.
73  
74 This class inherits from Rose::Object::MixIn.
75 See the Rose::Object::MixIn documentation for
76 a full explanation of how to import methods from this class.
77 The helper methods themselves are described below.
78  
79 =head1 METHODS
80  
81 MoreHelpers changes the default relationship method makers to include:
82  
83 find
84 get_set_on_save
85 add_on_save
86 count
87 iterator
88  
89 =cut
90  
91 =head2 primary_key_uri_escaped
92  
93 Returns the primary key value, URI-escaped. If there are multiple
94 columns comprising the primary key, they are joined into a single
95 string.
96  
97 If there are no values set for any of the column(s) comprising
98 the primary key, returns 0.
99  
100 Otherwise, returns all column values joined with C<;;> as per
101 CatalystX::CRUD::Controller API.
102  
103 =cut
104  
105 2 ▶sub primary_key_uri_escaped {
Subroutine primary_key_uri_escaped: called CC=5 SCAR=16.3
Pod documented
106 2 my $self = shift;
107 2 my $val = $self->primary_key_value;
108 2 ▶ my @vals = ref $val eq 'ARRAY' ? @$val : ($val);
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 1 1 2
109 2 my @esc;
110 2 for my $v (@vals) {
111 3 ▶ $v = '' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 3 3
112 3 $v =~ s/;/\%3b/g;
113 3 push @esc, $v;
114 }
115 2 ▶ if ( !grep { length($_) } @esc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {length $_;} @esc) 0 2 2
116 0 return 0;
117 }
118 2 my $pk = join( ';;', @esc );
119 2 $pk =~ s!/!\%2f!g;
120 2 return $pk;
121 }
122  
123 =head2 primary_key_value
124  
125 Returns the value of the primary key column(s). If the
126 value is comprised of multiple column values, the return
127 value will be an array ref of values.
128  
129 =cut
130  
131 2 ▶sub primary_key_value {
Subroutine primary_key_value: called CC=5 SCAR=16.7
Pod documented
132 2 my $self = shift;
133 2 my @cols = $self->meta->primary_key_columns;
134 2 my @vals;
135 2 for my $col (@cols) {
136 3 my $m;
137 3 ▶ if ( ref $col ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $col) { } 3 0 3
138 3 ▶ $m = $col->alias || $col->name;
Condition33%1/3
$col->alias || $col->name
l!l&&r!l&&!r
030
MC/DC0%0/2
$col->alias || $col->name
$col->alias $col->name
Truth table33%1/3
A || B
A= $col->aliasB= $col->name
ABresult
1X1
011
000
139 }
140 else {
141 0 $m = $col;
142 }
143 3 push( @vals, scalar $self->$m );
144 }
145 2 ▶ return scalar(@vals) > 1 ? \@vals : $vals[0];
Branch: 1 branch
BranchTrueFalseTotal
scalar @vals > 1 ? : 1 1 2
146 }
147  
148 =head2 unique_value
149  
150 Returns the first single-column unique value from the object by default.
151 This is intended for the common case where you use a serial integer as
152 the primary key but want to display a more human-friendly value
153 programmatically, like a name.
154  
155 If no unique single-column values are found, returns the primary_key_value().
156  
157 =cut
158  
159 0 ▶sub unique_value {
Subroutine unique_value: not called CC=4 SCAR=30.0
Pod documented
160 0 my $self = shift;
161 0 my @ukeys = $self->meta->unique_keys_column_names;
162 0 ▶ if (@ukeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ukeys) 0 0 0
163 0 for my $k (@ukeys) {
164 0 ▶ if ( scalar(@$k) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$k == 1) 0 0 0
165 0 my $method = $k->[0];
166 0 return $self->$method; # TODO column alias ??
167 }
168 }
169 }
170 0 return $self->primary_key_value;
171 }
172  
173 =head2 moniker
174  
175 Returns the name of the class without any leading namespace qualifier.
176 Similar to the DBIx::Class concept of 'moniker'.
177  
178 =cut
179  
180 0 ▶sub moniker {
Subroutine moniker: not called CC=1 SCAR=6.9
Pod documented
181 0 my $self = shift;
182 0 my $class = $self->meta->class;
183 0 $class =~ s/^.+:://;
184 0 return $class;
185 }
186  
187 =head2 flatten( I<args> )
188  
189 Returns the serialized object and its immediately related objects.
190  
191 As of version 0.03, this is just a wrapper around the as_tree()
192 Helper method with the "force_load" arg set to true. I<args>
193 are passed directly to the as_tree() method.
194  
195 Requires RDBO 0.7712 or later.
196  
197 =cut
198  
199 0 ▶sub flatten {
Subroutine flatten: not called CC=3 SCAR=24.8
Pod documented
200 0 my $self = shift;
201 0 ▶ if ( $Rose::DB::Object::VERSION < 0.7712 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Rose::DB::Object::VERSION < 0.7712) { } 0 0 0
elsif (not $self->can('as_tree')) { } 0 0 0
202 0 croak "Rose::DB::Object VERSION 0.7712 or later required";
203 }
204 elsif ( !$self->can('as_tree') ) {
205 0 croak "You must load Rose::DB::Object::Helpers as_tree() method";
206 }
207 0 return $self->as_tree( @_, force_load => 1 );
208 }
209  
210 =head2 exists( [ @I<params> ] )
211  
212 Returns true if the object exists in the database, false otherwise.
213  
214 May be called as class or object method.
215  
216 This method uses the Rose::DB::Object::Manager class to check
217 the database based on non-unique column(s). Call it like you
218 would load_speculative() but when you do not have a unique combination
219 of columns (which all the load* methods require).
220  
221 When called as object method, if @I<params> is omitted,
222 the current column values of the object are used.
223  
224 Example:
225  
226 # 'title' has no unique constraints on it
227 my $object = Object->new(title => 'Foo');
228 $object->save unless $object->exists;
229  
230 B<NOTE:> Using exists() as a way of enforcing data integrity
231 is far inferior to actually placing a constraint on a table
232 in the database. However, for things like testing and development
233 data, it can be a useful utility method.
234  
235 =cut
236  
237 0 ▶sub exists {
Subroutine exists: not called CC=7 SCAR=40.3
Pod documented
238 0 my $self = shift;
239 0 my @arg = @_;
240 0 ▶ if ( !@arg && ref($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @arg and ref $self) 0 0 0
Condition0%0/3
not @arg and ref $self
!ll&&!rl&&r
000
MC/DC0%0/2
not @arg and ref $self
not @arg ref $self
Truth table0%0/3
not(A) and B
A= not @argB= ref $self
ABresult
0X0
100
111
241 0 for my $col ( $self->meta->column_accessor_method_names ) {
242 0 ▶ push( @arg, $col, $self->$col ) if defined( $self->$col );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->$col 0 0 0
243 }
244 }
245 0 ▶ my $count = Rose::DB::Object::Manager->get_objects_count(
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
246 object_class => ref($self) || $self,
247 query => [@arg]
248 );
249  
250 0 ▶ return $count if defined($count);
Branch: 1 branch
BranchTrueFalseTotal
if defined $count 0 0 0
251 0 croak "Error: " . Rose::DB::Object::Manager->error;
252 }
253  
254 =head2 has_related( I<relationship_name> )
255  
256 Returns the number of related objects defined by the I<relationship_name>
257 accessor.
258  
259 Just a wrapper around the B<count> RDBO method type.
260  
261 =cut
262  
263 0 ▶sub has_related {
Subroutine has_related: not called CC=5 SCAR=34.0
Pod documented
264 0 my $self = shift;
265 0 ▶ my $rel = shift or croak "need Relationship name";
Branch: 1 branch
BranchTrueFalseTotal
my $rel = shift() or croak("need Relationship name") 0 0 0
266 0 my $method = $rel . '_count';
267 0 ▶ if ( $self->can($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($method)) { } 0 0 0
268 0 return $self->$method;
269 }
270 else {
271 0 my $other = $self->$rel;
272 0 ▶ if ( ref $other eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $other eq 'ARRAY') { } 0 0 0
273 0 return scalar @$other;
274 }
275 else {
276 0 ▶ return $other ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$other ? : 0 0 0
277 }
278 }
279 }
280  
281 =head2 has_related_pages( I<relationship_name>, I<page_size> )
282  
283 Returns the number of "pages" given I<page_size> for the count of related
284 objects for I<relationship_name>. Useful for creating pagers.
285  
286 =cut
287  
288 0 ▶sub has_related_pages {
Subroutine has_related_pages: not called CC=6 SCAR=37.4
Pod documented
289 0 my $self = shift;
290 0 ▶ my $rel = shift or croak "need Relationship name";
Branch: 1 branch
BranchTrueFalseTotal
my $rel = shift() or croak("need Relationship name") 0 0 0
291 0 ▶ my $pgsize = shift or croak "need page_size";
Branch: 1 branch
BranchTrueFalseTotal
my $pgsize = shift() or croak("need page_size") 0 0 0
292 0 ▶ if ( $pgsize =~ m/\D/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pgsize =~ /\D/) 0 0 0
293 0 croak "page_size must be an integer";
294 }
295 0 my $n = $self->has_related($rel);
296 0 ▶ return 0 if !$n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 0 0
297 0 ▶ if ( $n % $pgsize ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n % $pgsize) { } 0 0 0
298 0 return int( $n / $pgsize ) + 1;
299 }
300 else {
301 0 return $n / $pgsize;
302 }
303 }
304  
305 =head2 fetch_all
306  
307 Shortcut for the Manager method get_objects().
308  
309 =cut
310  
311 0 ▶sub fetch_all {
Subroutine fetch_all: not called CC=1 SCAR=6.9
Pod documented
312 0 my $self = shift;
313 0 my $class = $self->meta->class;
314 0 return Rose::DB::Object::Manager->get_objects(
315 object_class => $class,
316 @_
317 );
318 }
319  
320 =head2 fetch_all_iterator
321  
322 Shortcut for the Manager method get_objects_iterator().
323  
324 =cut
325  
326 0 ▶sub fetch_all_iterator {
Subroutine fetch_all_iterator: not called CC=1 SCAR=6.9
Pod documented
327 0 my $self = shift;
328 0 my $class = $self->meta->class;
329 0 return Rose::DB::Object::Manager->get_objects_iterator(
330 object_class => $class,
331 @_
332 );
333 }
334  
335 =head2 fetch_count
336  
337 Shortcut for the Manager method get_objects_count().
338  
339 =cut
340  
341 0 ▶sub fetch_count {
Subroutine fetch_count: not called CC=1 SCAR=6.9
Pod documented
342 0 my $self = shift;
343 0 my $class = $self->meta->class;
344 0 return Rose::DB::Object::Manager->get_objects_count(
345 object_class => $class,
346 @_
347 );
348 }
349  
350 =head2 column_is_boolean( I<column_name> )
351  
352 Returns true if the column type for I<column_name> is 'boolean'.
353  
354 =cut
355  
356 0 ▶sub column_is_boolean {
Subroutine column_is_boolean: not called CC=4 SCAR=30.0
Pod documented
357 0 my $self = shift;
358 0 my $col = shift;
359 0 ▶ croak "column_name required" unless defined $col;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col 0 0 0
360 0 ▶ if ( $self->meta->column($col)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->meta->column($col) and $self->meta->column($col)->type eq 'boolean') { } 0 0 0
Condition0%0/3
$self->meta->column($col) and $self->meta->column($col)->type eq "boolean"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->meta->column($col) and $self->meta->column($col)->type eq "boolean"
$self->meta->column($col) $self->meta->column($col)->type eq "boolean"
Truth table0%0/3
A and B
A= $self->meta->column($col)B= $self->meta->column($col)->type eq "boolean"
ABresult
0X0
100
111
361 and $self->meta->column($col)->type eq 'boolean' )
362 {
363 0 return 1;
364 }
365 else {
366 0 return 0;
367 }
368 }
369  
370 =head1 AUTHOR
371  
372 Peter Karman, C<< <karman at cpan.org> >>
373  
374 =head1 BUGS
375  
376 Please report any bugs or feature requests to
377 C<bug-rose-dbx-object-morehelpers at rt.cpan.org>, or through the web interface at
378 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Rose-DBx-Object-MoreHelpers>.
379 I will be notified, and then you'll automatically be notified of progress on
380 your bug as I make changes.
381  
382 =head1 SUPPORT
383  
384 You can find documentation for this module with the perldoc command.
385  
386 perldoc Rose::DBx::Object::MoreHelpers
387  
388 You can also look for information at:
389  
390 =over 4
391  
392 =item * AnnoCPAN: Annotated CPAN documentation
393  
394 L<http://annocpan.org/dist/Rose-DBx-Object-MoreHelpers>
395  
396 =item * CPAN Ratings
397  
398 L<http://cpanratings.perl.org/d/Rose-DBx-Object-MoreHelpers>
399  
400 =item * RT: CPAN's request tracker
401  
402 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Rose-DBx-Object-MoreHelpers>
403  
404 =item * Search CPAN
405  
406 L<http://search.cpan.org/dist/Rose-DBx-Object-MoreHelpers>
407  
408 =back
409  
410 =head1 ACKNOWLEDGEMENTS
411  
412 The Minnesota Supercomputing Institute C<< http://www.msi.umn.edu/ >>
413 sponsored the development of this software.
414  
415 =head1 COPYRIGHT & LICENSE
416  
417 Copyright 2008 by the Regents of the University of Minnesota.
418  
419 This program is free software; you can redistribute it and/or modify it
420 under the same terms as Perl itself.
421  
422 =cut
423  
424 2 1;
↑ Summary