ORM/Table.pm

filter: Statementstmt 96.3% 78 / 81 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.6% 22 / 23 Podpod 90.0% 9 / 10 totaltotal 95.9% 117 / 122 SCAR 17.9
CC
6
Cov
97%
transform
3.0
has_policy
2.0
has_one
2.0
CRAP 6.0
« Meta/Role/Relationship/HasOne.pm ↑ Summary Meta/HasOne/ViaFK.pm »
1 package Fey::ORM::Table;
2 {
3 9 $Fey::ORM::Table::VERSION = '0.46';
4 }
5  
6 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use Class::Load qw( load_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Fey::Meta::Class::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Fey::Object::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 ▶use Moose 1.15 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use MooseX::StrictConstructor 0.13 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use Moose::Util::MetaRole;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use MooseX::Params::Validate qw( pos_validated_list );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 9 Moose::Exporter->setup_import_methods(
21 with_meta =>
22 [qw( has_table has_policy has_one has_many transform query )],
23 as_is => [qw( inflate deflate handles )],
24 also => [ 'Moose', 'MooseX::StrictConstructor' ],
25 );
26  
27 sub init_meta {
28 31 ▶ shift;
Subroutine init_meta: called CC=1 SCAR=0.0
Pod undocumented
29 31 my %p = @_;
30  
31 31 return Moose->init_meta(
32 %p,
33 base_class => 'Fey::Object::Table',
34 metaclass => 'Fey::Meta::Class::Table',
35 );
36 }
37  
38 sub has_table {
39 26 ▶ my $meta = shift;
Subroutine has_table: called CC=1 SCAR=0.0
Pod documented
40  
41 26 my ($table) = pos_validated_list( \@_, { isa => 'Fey::Table' } );
42  
43 26 $meta->_associate_table(
44 $table,
45 _context(),
46 );
47 }
48  
49 sub has_policy {
50 2 ▶ my $meta = shift;
Subroutine has_policy: called CC=2 SCAR=6.9
Pod documented
51 2 my $policy = shift;
52  
53 2 ▶ unless ( ref $policy ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $policy) 1 1 2
54 1 load_class($policy);
55  
56 1 $policy = $policy->Policy();
57 }
58  
59 2 $meta->set_policy($policy);
60 }
61  
62 sub transform {
63 6 ▶ my $meta = shift;
Subroutine transform: called CC=3 SCAR=11.0
Pod documented
64  
65 6 my @p;
66  
67 6 ▶ push @p, pop @_ while ref $_[-1];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] and do { push @p, pop @_ } 10 6 16
68  
69 6 my %p = _combine_hashes(@p);
70  
71 6 for my $name (@_) {
72 7 $meta->_add_transform(
73 $name,
74 _context(),
75 %p,
76 );
77 }
78 }
79  
80 sub _combine_hashes {
81 6 ▶ return map { %{$_} } @_;
Subroutine _combine_hashes: called CC=1 SCAR=0.0
82 }
83  
84 sub inflate (&) {
85 4 ▶ return { inflate => $_[0] };
Subroutine inflate: called CC=1 SCAR=0.0
Pod documented
86 }
87  
88 sub deflate (&) {
89 5 ▶ return { deflate => $_[0] };
Subroutine deflate: called CC=1 SCAR=0.0
Pod documented
90 }
91  
92 sub handles ($) {
93 1 ▶ return { handles => $_[0] };
Subroutine handles: called CC=1 SCAR=0.0
Pod documented
94 }
95  
96 sub has_one {
97 12 ▶ my $meta = shift;
Subroutine has_one: called CC=2 SCAR=6.9
Pod documented
98  
99 12 my %p;
100 12 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 4 8 12
101 4 ( $p{table} ) = shift;
102 }
103 else {
104 8 $p{name} = shift;
105  
106 8 %p = ( %p, @_ );
107 }
108  
109 12 $meta->add_has_one(%p);
110 }
111  
112 sub has_many {
113 10 ▶ my $meta = shift;
Subroutine has_many: called CC=2 SCAR=6.9
Pod documented
114  
115 10 my %p;
116 10 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 3 7 10
117 3 ( $p{table} ) = shift;
118 }
119 else {
120 7 $p{name} = shift;
121  
122 7 %p = ( %p, @_ );
123  
124 }
125  
126 10 $meta->add_has_many(%p);
127 }
128  
129 sub query {
130 0 ▶ my $meta = shift;
Subroutine query: not called CC=1 SCAR=6.9
Pod documented
131 0 my $name = shift;
132  
133 0 $meta->add_query_method( name => $name, @_ );
134 }
135  
136 sub _context {
137 33 ▶ my %context;
Subroutine _context: called CC=1 SCAR=0.0
138 33 @context{qw(package file line)} = caller(2);
139  
140 33 return \%context;
141 }
142  
143 9 1;
144  
145 # ABSTRACT: Provides sugar for table-based classes
146  
147 __END__
148  
149 =pod
150  
151 =head1 NAME
152  
153 Fey::ORM::Table - Provides sugar for table-based classes
154  
155 =head1 VERSION
156  
157 version 0.46
158  
159 =head1 SYNOPSIS
160  
161 package MyApp::User;
162  
163 use Fey::ORM::Table;
164  
165 has_table ...;
166  
167 no Fey::ORM::Table;
168  
169 =head1 DESCRIPTION
170  
171 Use this class to associate your class with a table. It exports a
172 number of sugar functions to allow you to define things in a
173 declarative manner.
174  
175 =head1 EXPORTED FUNCTIONS
176  
177 This package exports the following functions:
178  
179 =head2 has_table($table)
180  
181 Given a L<Fey::Table> object, this method associates that table with
182 the calling class.
183  
184 Calling C<has_table()> will make your class a subclass of
185 L<Fey::Object::Table>, which provides basic CRUD operations for
186 L<Fey::ORM>. You should make sure to review the docs for
187 L<Fey::Object::Table>.
188  
189 Calling this function also generates a number of methods and
190 attributes in the calling class.
191  
192 First, it generates one attribute for each column in the associated
193 table. Of course, this assumes that your columns are named in such a
194 way as to be usable as Perl methods.
195  
196 It also generates a predicate for each attribute, where the predicate
197 is the column named prefixed with "has_". So for a column named
198 "user_id", you get a C<user_id()> attribute and a C<has_user_id()>
199 predicate.
200  
201 These column-named attributes do not have a public setter method. If
202 you want to change the value of these attributes, you need to use the
203 C<update()> method.
204  
205 =head2 has_policy($policy_class)
206  
207 =head2 has_policy($policy_object)
208  
209 This allows you to associate a policy with your class. See
210 L<Fey::ORM::Policy> for details on how policies work.
211  
212 =head2 has_one($table)
213  
214 =head2 has_one 'name' => ( table => $table, fk => $fk, cache => $bool, undef => $bool, handles => ... )
215  
216 The C<has_one()> function declares a relationship between the calling
217 class's table and another table. The method it creates returns an
218 object of the foreign table's class, or undef or none exists.
219  
220 With the single-argument form, you can simply pass a single
221 L<Fey::Table> object. This works when there is a single foreign key
222 between the calling class's table and the table passed to
223 C<has_one()>.
224  
225 With a single argument, the generated attribute will be named as C<<
226 lc $has_one_table->name() >>, and caching will be turned on.
227  
228 If you want to change any of the defaults, you can use the
229 multi-argument form. In this case, the first argument is the name of
230 the attribute or method to add. Then you can specify various
231 parameters by name. You must specify a C<table>, of course.
232  
233 The C<fk> parameter is required when there is more than one foreign
234 key between the two tables. Finally, you can turn off caching by
235 setting C<cache> to a false value.
236  
237 When caching is enabled, the object for the foreign table is only
238 fetched once, and is cached afterwards. This is independent of the
239 object caching for a particular class. If you turn off caching, then
240 the object is fetched every time the method is called.
241  
242 Also, a private clearer method is created when caching is enabled, of
243 the form C<< $object->_clear_$name() >>.
244  
245 The C<undef> parameter allows you to explicitly say whether the
246 attribute can be undefined. Normally this is calculated by looking at
247 the foreign key and seeing if any of the source columns are nullable.
248  
249 The C<handles> parameter works exactly like it does for any Moose
250 attribute, but it only works if C<cache> is true, since otherwise the
251 relationship is implemented via a simple method, not a Moose
252 attribute.
253  
254 =head2 has_one 'name' => ( table => $table, select => $select, bind_params => $sub, cache => $bool, undef => $bool, handles => ... )
255  
256 This is an alternative form of C<has_one()> that lets you declare a
257 relationship to another table via an arbitrary SELECT statement.
258  
259 In this form, you provide a query object to define the SQL used to fetch the
260 foreign row. You can provide a C<bind_params> parameter as a code reference,
261 which will be called as a method on your object. It is expected to return one
262 or more bind parameters. The C<cache> parameter works exactly the same as in
263 the first form of C<has_one()>.
264  
265 In this form the C<undef> parameter defaults to true, but you can set
266 it to a false value.
267  
268 Note that if you want to provide bind_params for the SQL you provide,
269 you need to make sure it has placeholders.
270  
271 =head2 has_many($table)
272  
273 =head2 has_many 'name' => ( table => $table, fk => $fk, cache => $bool, order_by => [ ... ] )
274  
275 The C<has_many()> function declares a relationship between the calling
276 class's table and another table, just like C<has_one()>. The method it
277 creates returns a L<Fey::Object::Iterator::FromSelect> of the foreign
278 table's objects.
279  
280 With the single-argument form, you can simply pass a single
281 L<Fey::Table> object. This works when there is a single foreign key
282 between the calling class's table and the table passed to
283 C<has_many()>.
284  
285 With a single argument, the generated attribute will be named as C<<
286 lc $has_one_table->name() >>, and caching will be turned off. There
287 will be no specific order to the results returned.
288  
289 If you want to change any of the defaults, you can use the
290 multi-argument form. In this case, the first argument is the name of
291 the attribute or method to add. Then you can specify various
292 parameters by name. You must specify a C<table>, of course.
293  
294 The C<fk> parameter is required when there is more than one foreign
295 key between the two tables. Finally, you can turn on caching by
296 setting C<cache> to a true value.
297  
298 When caching is enabled, the iterator returned is of the
299 L<Fey::Object::Iterator::FromSelect::Caching> class.
300  
301 Also, a private clearer method is created when caching is enabled, of
302 the form C<< $object->_clear_$name() >>.
303  
304 Note that you will always get an iterator object back from your
305 has_many methods and attributes, even if there are no matching rows in
306 the foreign table.
307  
308 You can also specify an C<order_by> parameter as an array
309 reference. This should be an array like you would pass to C<<
310 Fey::SQL::Select->order_by() >>.
311  
312 =head2 query $name => select => $select, bind_params => sub { ... }
313  
314 The C<query()> function declares a method based on the given query. This works
315 much like declaring an attribute with the C<FromSelect> metaclass, but the
316 value returned from the query is not stored in the object.
317  
318 =head2 transform $column1, $column2 => inflate { ... } => deflate { ... }
319  
320 The C<transform()> function declares an inflator, deflator, or both
321 for the specified column. The inflator will be used to wrap the normal
322 accessor for the column. You'd generally use this to turn a raw value
323 from the DBMS into an object, for example:
324  
325 transform 'creation_date' =>
326 inflate { DateTime::Format::Pg->parse_date( $_[1] ) };
327  
328 The inflator (and deflator) coderef you specify will be called as a
329 I<method> on the object (or class). This lets you get at other
330 attributes for the object if needed.
331  
332 When a column is inflated, a new attribute is created to allow you to
333 get at the raw data by suffixing the column name with "_raw". Given
334 the above inflator, a C<creation_date_raw()> attribute would be
335 created.
336  
337 If the column in question is nullable your inflator should be prepared
338 to handle an undef value for the column.
339  
340 Deflators are used to transform objects passed to C<update()> or
341 C<insert()> into values suitable for passing to the DBMS:
342  
343 transform 'creation_date' =>
344 deflate { defined $_[1] && ref $_[1]
345 ? DateTime::Format::Pg->format_date( $_[1] )
346 : $_[1] };
347  
348 Just as with an inflator, your deflator should be prepared to accept
349 an undef if the column is nullable.
350  
351 You can only declare one inflator and one deflator for each column.
352  
353 You can use the same inflator and deflator for more than one column at
354 once:
355  
356 transform 'creation_date', 'modification_date'
357 => inflate { ... }
358 => deflate { ... };
359  
360 =head2 inflate { .. }
361  
362 =head2 deflate { .. }
363  
364 These are sugar functions that accept a single coderef. They mostly
365 exist to prevent you from having to write this:
366  
367 # this is not valid code!
368 transform 'creation_date' =>
369 ( inflator => sub { ... },
370 deflator => sub { ... },
371 );
372  
373 =head2 handles ...
374  
375 This sugar function lets you add delegation to an inflated
376 attribute. It accepts anything that Moose accepts for an attribute's
377 C<handles> parameter.
378  
379 transform 'creation_date'
380 => inflate { ... }
381 => handles { creation_ymd => 'ymd',
382 creation_iso8601 => 'iso8601',
383 }
384 => deflate { ... };
385  
386 =head1 AUTHOR
387  
388 Dave Rolsky <autarch@urth.org>
389  
390 =head1 COPYRIGHT AND LICENSE
391  
392 This software is copyright (c) 2011 by Dave Rolsky.
393  
394 This is free software; you can redistribute it and/or modify it under
395 the same terms as the Perl 5 programming language system itself.
396  
397 =cut
« Meta/Role/Relationship/HasOne.pm ↑ Summary Meta/HasOne/ViaFK.pm »