Meta/Class/Table.pm

filter: Statementstmt 78.9% 203 / 257 Branchbran 75.8% 47 / 62 Conditioncond 73.3% 11 / 15 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 72.4% 42 / 58 Podpod 100% 7 / 7 totaltotal 76.7% 313 / 408 SCAR 40.4
CC
40
Cov
78%
_search_cache
20.0
_write_to_cache
12.0
_associate_table
8.1
CRAP 56.7
« Object/Schema.pm ↑ Summary ORM/Role/Iterator.pm »
1 package Fey::Meta::Class::Table;
2 {
3 10 $Fey::Meta::Class::Table::VERSION = '0.46';
4 }
5  
6 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use Fey::Exceptions qw( param_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Fey::Hash::ColumnsKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Fey::Object::Policy;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Fey::Meta::Attribute::FromInflator;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 ▶use Fey::Meta::Attribute::FromColumn;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use Fey::Meta::Attribute::FromSelect;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use Fey::Meta::Class::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶use Fey::Meta::HasOne::ViaFK;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶use Fey::Meta::HasOne::ViaSelect;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 ▶use Fey::Meta::HasMany::ViaFK;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use Fey::Meta::HasMany::ViaSelect;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶use Fey::Meta::Method::Constructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 10 ▶use Fey::Meta::Method::FromSelect;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 use Fey::ORM::Types
24 10 ▶ qw( Bool ClassName CodeRef DoesHasMany DoesHasOne HashRef Object );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 10 ▶use List::AllUtils qw( all );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 10 ▶use Try::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 10 ▶use MooseX::ClassAttribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 10 ▶use MooseX::SemiAffordanceAccessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 10 extends 'Moose::Meta::Class';
33  
34 class_has '_ClassToTableMap' => (
35 traits => ['Hash'],
36 is => 'ro',
37 isa => HashRef ['Fey::Table'],
38 8 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 10 lazy => 1,
40 handles => {
41 TableForClass => 'get',
42 _SetTableForClass => 'set',
43 _ClassHasTable => 'exists',
44 },
45 );
46  
47 10 has '_object_cache_is_enabled' => (
48 is => 'rw',
49 isa => Bool,
50 lazy => 1,
51 default => 0,
52 );
53  
54 has '_object_cache' => (
55 is => 'ro',
56 isa => HashRef [Object],
57 lazy => 1,
58 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 10 clearer => '_clear_object_cache',
60 );
61  
62 10 has 'table' => (
63 is => 'rw',
64 isa => 'Fey::Table',
65 writer => '_set_table',
66 predicate => '_has_table',
67 );
68  
69 has 'inflators' => (
70 traits => ['Hash'],
71 is => 'ro',
72 isa => HashRef [CodeRef],
73 3 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
74 10 lazy => 1,
75 handles => {
76 _add_inflator => 'set',
77 has_inflator => 'exists',
78 },
79 );
80  
81 has 'deflators' => (
82 traits => ['Hash'],
83 is => 'ro',
84 isa => HashRef [CodeRef],
85 3 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
86 10 lazy => 1,
87 handles => {
88 deflator_for => 'get',
89 _add_deflator => 'set',
90 has_deflator => 'exists',
91 },
92 );
93  
94 has 'schema_class' => (
95 is => 'ro',
96 isa => ClassName,
97 lazy => 1,
98 default => sub {
99 2 ▶ Fey::Meta::Class::Schema->ClassForSchema( $_[0]->table()->schema() );
Subroutine __ANON__: called CC=1 SCAR=0.0
100 },
101 10 );
102  
103 has 'policy' => (
104 is => 'rw',
105 isa => 'Fey::Object::Policy',
106 53 ▶ default => sub { Fey::Object::Policy->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
107 10 );
108  
109 has '_has_ones' => (
110 traits => ['Hash'],
111 is => 'ro',
112 isa => HashRef [DoesHasOne],
113 1 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
114 10 lazy => 1,
115 handles => {
116 _has_one => 'get',
117 _add_has_one => 'set',
118 _has_has_one => 'exists',
119 has_ones => 'values',
120 _remove_has_one => 'delete',
121 },
122 );
123  
124 has '_has_manies' => (
125 traits => ['Hash'],
126 is => 'ro',
127 isa => HashRef [DoesHasMany],
128 2 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
129 10 lazy => 1,
130 handles => {
131 _has_many => 'get',
132 _add_has_many => 'set',
133 _has_has_many => 'exists',
134 has_manies => 'values',
135 _remove_has_many => 'delete',
136 },
137 );
138  
139 has '_select_sql_cache' => (
140 is => 'ro',
141 isa => 'Fey::Hash::ColumnsKey',
142 lazy => 1,
143 0 ▶ default => sub { Fey::Hash::ColumnsKey->new() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
144 10 );
145  
146 has '_sql_string_cache' => (
147 is => 'ro',
148 isa => HashRef [HashRef],
149 lazy => 1,
150 default => sub {
151 0 ▶ { {} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
152 },
153 10 );
154  
155 has '_select_by_pk_sql' => (
156 is => 'ro',
157 isa => 'Fey::SQL::Select',
158 lazy => 1,
159 0 ▶ default => sub { return $_[0]->name()->_MakeSelectByPKSQL() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 10 );
161  
162 10 has '_count_sql' => (
163 is => 'ro',
164 isa => 'Fey::SQL::Select',
165 lazy => 1,
166 builder => '_build_count_sql',
167 );
168  
169 sub ClassForTable {
170 27 ▶ my $class = shift;
Subroutine ClassForTable: called CC=2 SCAR=6.9
Pod documented
171  
172 return @_ == 1
173 ? $class->_ClassForTable(@_)
174 27 ▶ : map { $class->_ClassForTable($_) } @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 26 1 27
175 }
176  
177 sub _ClassForTable {
178 28 ▶ my $class = shift;
Subroutine _ClassForTable: called CC=4 SCAR=13.9
179 28 my $table = shift;
180  
181 28 my $map = $class->_ClassToTableMap();
182  
183 28 for my $class_name ( keys %{$map} ) {
184 27 my $potential_table = $map->{$class_name};
185  
186 27 ▶ return $class_name
Branch: 1 branch
BranchTrueFalseTotal
if $potential_table->name eq $table->name and $potential_table->schema->name eq $table->schema->name 4 23 27
Condition100%3/3
$potential_table->name eq $table->name and $potential_table->schema->name eq $table->schema->name
!ll&&!rl&&r
2214
MC/DC100%2/2
$potential_table->name eq $table->name and $potential_table->schema->name eq $table->schema->name
$potential_table->name eq $table->name $potential_table->schema->name eq $table->schema->name
Truth table100%3/3
A and B
A= $potential_table->name eq $table->nameB= $potential_table->schema->name eq $table->schema->name
ABresult
0X0
100
111
187 if $potential_table->name() eq $table->name()
188 && $potential_table->schema()->name() eq
189 $table->schema()->name();
190 }
191  
192 24 return;
193 }
194  
195 sub _search_cache {
196 0 ▶ my $self = shift;
Subroutine _search_cache: not called CC=4 SCAR=30.0
197 0 my $p = shift;
198  
199 0 my $cache = $self->_object_cache();
200  
201 0 for my $key ( @{ $self->table()->candidate_keys() } ) {
202 0 my @names = map { $_->name() } @{$key};
203 0 ▶ next unless all { defined $p->{$_} } @names;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &all(sub { defined $p->{$_}; } , @names) 0 0 0
204  
205 0 my $cache_key = join "\0", map { $_, $p->{$_} } sort @names;
206  
207 return $cache->{$cache_key}
208 0 ▶ if exists $cache->{$cache_key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $cache->{$cache_key} 0 0 0
209 }
210 }
211  
212 sub _write_to_cache {
213 0 ▶ my $self = shift;
Subroutine _write_to_cache: not called CC=3 SCAR=24.8
214 0 my $object = shift;
215  
216 0 my $cache = $self->_object_cache();
217  
218 0 for my $key ( @{ $self->table()->candidate_keys() } ) {
219 0 my @names = map { $_->name() } @{$key};
220  
221 0 my @pieces = map { $_, $object->$_() } sort @names;
222  
223 0 ▶ next unless all {defined} @pieces;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &all(sub { defined $_; } , @pieces) 0 0 0
224  
225 0 my $cache_key = join "\0", @pieces;
226  
227 0 $cache->{$cache_key} = $object;
228 }
229 }
230  
231 sub _associate_table {
232 26 ▶ my $self = shift;
Subroutine _associate_table: called CC=8 SCAR=20.9
233 26 my $table = shift;
234 26 my $context = shift;
235  
236 26 my $caller = $self->name();
237  
238 26 ▶ param_error 'Cannot call has_table() more than once per class'
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_table 1 25 26
239 if $self->_has_table();
240  
241 25 ▶ param_error 'Cannot associate the same table with multiple classes'
Branch: 1 branch
BranchTrueFalseTotal
if $self->ClassForTable($table) 1 24 25
242 if $self->ClassForTable($table);
243  
244 24 ▶ param_error 'A table object passed to has_table() must have a schema'
Branch: 1 branch
BranchTrueFalseTotal
unless $table->has_schema 1 23 24
245 unless $table->has_schema();
246  
247 23 my $class = Fey::Meta::Class::Schema->ClassForSchema( $table->schema() );
248  
249 23 ▶ param_error 'You must load your schema class before calling has_table()'
Branch: 1 branch
BranchTrueFalseTotal
unless $class and $class->can("meta") and $class->meta->_has_schema 0 22 22
Condition66%4/6
$class and $class->can("meta") and $class->meta->_has_schema
!ll&&!rl&&r
1022
$class and $class->can("meta")
!ll&&!rl&&r
1022
MC/DC33%1/3
$class and $class->can("meta") and $class->meta->_has_schema
$class $class->can("meta") $class->meta->_has_schema
Truth table50%2/4
A and B and C
A= $classB= $class->can("meta")C= $class->meta->_has_schema
ABCresult
0XX0
10X0
1100
1111
250 unless $class
251 && $class->can('meta')
252 && $class->meta()->_has_schema();
253  
254 param_error
255 'A table object passed to has_table() must have at least one key'
256 22 ▶ unless @{ $table->primary_key() };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$table->primary_key;} 1 21 22
257  
258 21 $self->_SetTableForClass( $self->name() => $table );
259  
260 21 $self->_set_table($table);
261  
262 21 $self->_make_column_attributes($context);
263 }
264  
265 sub _make_column_attributes {
266 21 ▶ my $self = shift;
Subroutine _make_column_attributes: called CC=4 SCAR=14.5
267 21 my $context = shift;
268  
269 21 my $table = $self->table();
270  
271 21 for my $column ( $table->columns() ) {
272 78 my $name = $column->name();
273  
274 78 ▶ next if $self->has_method($name);
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_method($name) 1 77 78
275  
276 my %attr_p = (
277 metaclass => 'Fey::Meta::Attribute::FromColumn',
278 is => 'rw',
279 isa => $self->_type_for_column($column),
280 lazy => 1,
281 0 ▶ default => sub { $_[0]->_get_column_value($name) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
282 77 column => $column,
283 writer => q{_set_} . $name,
284 clearer => q{_clear_} . $name,
285 predicate => q{has_} . $name,
286 definition_context => $context,
287 );
288  
289 77 $self->add_attribute( $name, %attr_p );
290  
291 77 ▶ if ( my $transform = $self->policy()->transform_for_column($column) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $transform = $self->policy->transform_for_column($column)) 0 77 77
292 {
293 0 $self->_add_transform( $name, {}, %{$transform} );
294 }
295 }
296 }
297  
298 # XXX - can this be overridden or customized? should it account for
299 # per-column/policy-level transforms?
300 {
301 10 my %FeyToMoose = (
302 text => 'Str',
303 blob => 'Str',
304 integer => 'Int',
305 float => 'Num',
306 datetime => 'Str',
307 date => 'Str',
308 time => 'Str',
309 boolean => 'Bool',
310 other => 'Value',
311 );
312  
313 sub _type_for_column {
314 77 ▶ my $self = shift;
Subroutine _type_for_column: called CC=2 SCAR=6.9
315 77 my $column = shift;
316  
317 77 my $type = $FeyToMoose{ $column->generic_type() };
318  
319 77 ▶ $type .= q{ | Undef}
Branch: 1 branch
BranchTrueFalseTotal
if $column->is_nullable 15 62 77
320 if $column->is_nullable();
321  
322 77 return $type;
323 }
324 }
325  
326 10 sub _add_transform {
327 7 ▶ my $self = shift;
Subroutine _add_transform: called CC=5 SCAR=16.1
328 7 my $name = shift;
329 7 my $context = shift;
330 7 my %p = @_;
331  
332 7 my $attr = $self->get_attribute($name);
333  
334 7 ▶ param_error "The column $name does not exist as an attribute"
Branch: 1 branch
BranchTrueFalseTotal
unless $attr 1 6 7
335 unless $attr;
336  
337 $self->_add_inflator_to_attribute(
338 $name,
339 $context,
340 $attr,
341 $p{inflate},
342 $p{handles}
343 6 ▶ ) if $p{inflate};
Branch: 1 branch
BranchTrueFalseTotal
if $p{"inflate"} 5 1 6
344  
345 5 ▶ if ( $p{deflate} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{"deflate"}) 5 0 5
346 5 ▶ param_error
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_deflator($name) 1 4 5
347 "Cannot provide more than one deflator for a column ($name)"
348 if $self->has_deflator($name);
349  
350 4 $self->_add_deflator( $name => $p{deflate} );
351 }
352 }
353  
354 sub _add_inflator_to_attribute {
355 5 ▶ my $self = shift;
Subroutine _add_inflator_to_attribute: called CC=3 SCAR=11.0
356 5 my $name = shift;
357 5 my $context = shift;
358 5 my $attr = shift;
359 5 my $inflator = shift;
360 5 my $handles = shift;
361  
362 5 ▶ param_error "Cannot provide more than one inflator for a column ($name)"
Branch: 1 branch
BranchTrueFalseTotal
if $attr->isa("Fey::Meta::Attribute::FromInflator") 1 4 5
363 if $attr->isa('Fey::Meta::Attribute::FromInflator');
364  
365 4 $self->remove_attribute($name);
366  
367 4 my $raw_name = $name . q{_raw};
368  
369 # XXX - should the private writer invoke the deflator?
370 4 my $raw_attr = $attr->clone(
371 name => $raw_name,
372 reader => $raw_name,
373 definition_context => $context,
374 );
375  
376 4 $self->add_attribute($raw_attr);
377  
378 4 my $inflated_predicate = q{_has_inflated_} . $name;
379 4 my $inflated_clear = q{_clear_inflated_} . $name;
380  
381 my $default = sub {
382 4 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
383  
384 4 return $self->$inflator( $self->$raw_name() );
385 4 };
386  
387 4 ▶ my %handles = $handles ? ( handles => $handles ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$handles ? : 1 3 4
388  
389 4 $self->add_attribute(
390 $name,
391 metaclass => 'Fey::Meta::Attribute::FromInflator',
392 is => 'ro',
393 lazy => 1,
394 default => $default,
395 predicate => $inflated_predicate,
396 clearer => $inflated_clear,
397 init_arg => undef,
398 raw_attribute => $raw_attr,
399 inflator => $inflator,
400 definition_context => $context,
401 %handles,
402 );
403  
404 my $clear_inflated = sub {
405 2 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
406  
407 2 $self->$inflated_clear();
408 4 };
409  
410 4 $self->add_after_method_modifier( $raw_attr->clearer(), $clear_inflated );
411 4 $self->add_after_method_modifier( $raw_attr->writer(), $clear_inflated );
412  
413 4 $self->_add_inflator( $name => $inflator );
414 }
415  
416 sub add_has_one {
417 12 ▶ my $self = shift;
Subroutine add_has_one: called CC=7 SCAR=19.6
Pod documented
418 12 my %p = @_;
419  
420 12 ▶ param_error 'You must call has_table() before calling has_one().'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_table 0 12 12
421 unless $self->_has_table();
422  
423 param_error
424 'You cannot pass both a select and fk parameter when creating a has-one relationship'
425 12 ▶ if $p{select} && $p{fk};
Branch: 1 branch
BranchTrueFalseTotal
if $p{"select"} and $p{"fk"} 0 12 12
Condition66%2/3
$p{"select"} and $p{"fk"}
!ll&&!rl&&r
1020
MC/DC0%0/2
$p{"select"} and $p{"fk"}
$p{"select"} $p{"fk"}
Truth table66%2/3
A and B
A= $p{"select"}B= $p{"fk"}
ABresult
0X0
100
111
426  
427 my $class
428 = $p{select}
429 12 ▶ ? 'Fey::Meta::HasOne::ViaSelect'
Branch: 1 branch
BranchTrueFalseTotal
$p{'select'} ? : 2 10 12
430 : 'Fey::Meta::HasOne::ViaFK';
431  
432 12 $p{foreign_table} = delete $p{table};
433  
434 $p{is_cached} = delete $p{cache}
435 12 ▶ if exists $p{cache};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{"cache"} 3 9 12
436 $p{allows_undef} = delete $p{undef}
437 12 ▶ if exists $p{undef};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{"undef"} 1 11 12
438  
439 12 my $has_one = $class->new(
440 table => $self->table(),
441 namer => $self->policy()->has_one_namer(),
442 %p,
443 );
444  
445 11 $has_one->attach_to_class($self);
446  
447 9 $self->_add_has_one( $has_one->name() => $has_one );
448 }
449  
450 sub remove_has_one {
451 8 ▶ my $self = shift;
Subroutine remove_has_one: called CC=2 SCAR=7.0
Pod documented
452 8 my $name = shift;
453  
454 8 ▶ return unless $self->_has_has_one($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_has_one($name) 0 8 8
455  
456 8 my $has_one = $self->_has_one($name);
457  
458 8 $has_one->detach_from_class();
459  
460 8 $self->_remove_has_one( $has_one->name() );
461 }
462  
463 sub add_has_many {
464 10 ▶ my $self = shift;
Subroutine add_has_many: called CC=6 SCAR=18.1
Pod documented
465 10 my %p = @_;
466  
467 10 ▶ param_error 'You must call has_table() before calling has_many().'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_table 0 10 10
468 unless $self->_has_table();
469  
470 param_error
471 'You cannot pass both a select and fk parameter when creating a has-many relationship'
472 10 ▶ if $p{select} && $p{fk};
Branch: 1 branch
BranchTrueFalseTotal
if $p{"select"} and $p{"fk"} 0 10 10
Condition66%2/3
$p{"select"} and $p{"fk"}
!ll&&!rl&&r
820
MC/DC0%0/2
$p{"select"} and $p{"fk"}
$p{"select"} $p{"fk"}
Truth table66%2/3
A and B
A= $p{"select"}B= $p{"fk"}
ABresult
0X0
100
111
473  
474 my $class
475 = $p{select}
476 10 ▶ ? 'Fey::Meta::HasMany::ViaSelect'
Branch: 1 branch
BranchTrueFalseTotal
$p{'select'} ? : 2 8 10
477 : 'Fey::Meta::HasMany::ViaFK';
478  
479 10 $p{foreign_table} = delete $p{table};
480  
481 $p{is_cached} = delete $p{cache}
482 10 ▶ if exists $p{cache};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{"cache"} 2 8 10
483  
484 10 my $has_many = $class->new(
485 table => $self->table(),
486 namer => $self->policy()->has_many_namer(),
487 %p,
488 );
489  
490 9 $has_many->attach_to_class($self);
491  
492 7 $self->_add_has_many( $has_many->name() => $has_many );
493 }
494  
495 sub remove_has_many {
496 5 ▶ my $self = shift;
Subroutine remove_has_many: called CC=2 SCAR=6.9
Pod documented
497 5 my $name = shift;
498  
499 5 ▶ return unless $self->_has_has_many($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_has_many($name) 1 4 5
500  
501 4 my $has_many = $self->_has_many($name);
502  
503 4 $has_many->detach_from_class();
504  
505 4 $self->_remove_has_many( $has_many->name() );
506 }
507  
508 sub _build_count_sql {
509 0 ▶ my $self = shift;
Subroutine _build_count_sql: not called CC=1 SCAR=6.9
510  
511 0 my $table = $self->table();
512  
513 0 my $select = $self->schema_class()->SQLFactoryClass()->new_select();
514  
515 0 $select->select( Fey::Literal::Function->new( 'COUNT', '*' ) )
516 ->from($table);
517  
518 0 return $select;
519 }
520  
521 sub add_query_method {
522 0 ▶ my $self = shift;
Subroutine add_query_method: not called CC=1 SCAR=6.9
Pod documented
523  
524 0 my $method = Fey::Meta::Method::FromSelect->new(
525 package_name => $self->name(),
526 @_,
527 );
528  
529 0 $self->add_method( $method->name() => $method );
530  
531 0 return;
532 }
533  
534 sub make_immutable {
535 shift->SUPER::make_immutable(
536 0 ▶ @_,
Subroutine make_immutable: not called CC=1 SCAR=6.9
Pod documented
537 constructor_class => 'Fey::Meta::Method::Constructor',
538 );
539 }
540  
541 10 ▶if ( $Moose::VERSION >= 1.9900 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Moose::VERSION >= "1.99") 10 0 10
542 # XXX - can we refactor Moose/CMOP core to make overriding (and copying)
543 # all of this unnecessary?
544 override _inline_new_object => sub {
545 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
546  
547 #<<<
548 return (
549 0 'my $class = shift;',
550 '$class = Scalar::Util::blessed($class) || $class;',
551 $self->_inline_fallback_constructor('$class'),
552 $self->_inline_params( '$params', '$class' ),
553 $self->_inline_search_cache(),
554 'my $instance;',
555 '$class->_ClearConstructorError();',
556 'my @args = @_;',
557 'Try::Tiny::try {',
558 '@_ = @args;',
559 $self->_inline_generate_instance( '$instance', '$class' ),
560 $self->_inline_slot_initializers,
561 $self->_inline_preserve_weak_metaclasses,
562 $self->_inline_extra_init,
563 '}',
564 'Try::Tiny::catch {',
565 'die $_ unless Scalar::Util::blessed($_) && $_->isa(q{Fey::Exception::NoSuchRow});',
566 '$class->_SetConstructorError($_);',
567 'undef $instance;',
568 '};',
569 'return unless $instance;',
570 $self->_inline_write_to_cache(),
571 'return $instance',
572 );
573 #>>>
574 10 };
575  
576 # The default version of this sticks a "my" in front of the declaration,
577 # but we want to declare the $instance var earlier.
578 override _inline_generate_instance => sub {
579 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
580 0 my ( $inst, $class ) = @_;
581 return (
582 0 $inst . ' = ' . $self->_inline_create_instance($class) . ';',
583 );
584 10 };
585 }
586  
587 sub _inline_search_cache {
588 0 ▶ my $self = shift;
Subroutine _inline_search_cache: not called CC=1 SCAR=6.9
589  
590 #<<<
591 return
592 0 ( 'if ( $metaclass->_object_cache_is_enabled() ) {',
593 'my $cached = $metaclass->_search_cache($params);',
594 'return $cached if $cached;',
595 '}'
596 );
597 #>>>
598 }
599  
600 sub _inline_write_to_cache {
601 0 ▶ my $self = shift;
Subroutine _inline_write_to_cache: not called CC=1 SCAR=6.9
602  
603 0 return '$metaclass->_write_to_cache($instance) if $metaclass->_object_cache_is_enabled();';
604 }
605  
606 10 __PACKAGE__->meta()->make_immutable();
607  
608 10 1;
609  
610 # ABSTRACT: A metaclass for table classes
611  
612 __END__
613  
614 =pod
615  
616 =head1 NAME
617  
618 Fey::Meta::Class::Table - A metaclass for table classes
619  
620 =head1 VERSION
621  
622 version 0.46
623  
624 =head1 SYNOPSIS
625  
626 package MyApp::User;
627  
628 use Fey::ORM::Table;
629  
630 print __PACKAGE__->meta()->ClassForTable($table);
631  
632 =head1 DESCRIPTION
633  
634 This is the metaclass for table classes. When you use
635 L<Fey::ORM::Table> in your class, it uses this class to do all the
636 heavy lifting.
637  
638 =head1 METHODS
639  
640 This class provides the following methods:
641  
642 =head2 Fey::Meta::Class::Table->ClassForTable( $table1, $table2 )
643  
644 Given one or more L<Fey::Table> objects, this method returns the name
645 of the class which "has" that table, if any.
646  
647 =head2 Fey::Meta::Class::Table->TableForClass($class)
648  
649 Given a class, this method returns the L<Fey::Table> object associated
650 with that class, if any.
651  
652 =head2 $meta->table()
653  
654 Returns the L<Fey::Table> for the metaclass's class.
655  
656 =head2 $meta->add_has_one(%options)
657  
658 Creates a new L<Fey::Meta::HasOne::ViaFK> or
659 L<Fey::Meta::HasOne::ViaSelect> object and adds it to the
660 metaclass. Internally, this will call C<attach_to_class()> on the
661 C<HasOne> meta-object.
662  
663 =head2 $meta->has_ones()
664  
665 Returns a list of the C<HasOne> objects added to this metaclass.
666  
667 =head2 $meta->remove_has_one($name)
668  
669 Removes the named C<HasOne> meta-object. Internally, this will call
670 C<detach_from_class()> on the C<HasOne> meta-object.
671  
672 =head2 $meta->add_has_many(%options)
673  
674 Creates a new L<Fey::Meta::HasMany::ViaFK> or
675 L<Fey::Meta::HasMany::ViaSelect> object and adds it to the
676 metaclass. Internally, this will call C<attach_to_class()> on the
677 C<HasMany> meta-object.
678  
679 =head2 $meta->has_manies()
680  
681 Returns a list of the C<HasMany> objects added to this metaclass.
682  
683 =head2 $meta->remove_has_many($name)
684  
685 Removes the named C<HasMany> meta-object. Internally, this will call
686 C<detach_from_class()> on the C<HasMany> meta-object.
687  
688 =head2 $meta->has_inflator($name)
689  
690 Returns a boolean indicating whether or not there is an inflator
691 defined for the named column.
692  
693 =head2 $meta->has_deflator($name)
694  
695 Returns a boolean indicating whether or not there is an inflator
696 defined for the named column.
697  
698 =head2 $meta->add_query_method(%options)
699  
700 Adds a new L<Fey::Meta::Method::FromSelect> method to the class. The
701 C<%options> passed to this method will be passed to the
702 L<Fey::Meta::Method::FromSelect> constructor.
703  
704 =head2 $meta->make_immutable()
705  
706 This class overrides C<< Moose::Meta::Class->make_immutable() >> in
707 order to do its own optimizations for immutability.
708  
709 =head1 AUTHOR
710  
711 Dave Rolsky <autarch@urth.org>
712  
713 =head1 COPYRIGHT AND LICENSE
714  
715 This software is copyright (c) 2011 by Dave Rolsky.
716  
717 This is free software; you can redistribute it and/or modify it under
718 the same terms as the Perl 5 programming language system itself.
719  
720 =cut
« Object/Schema.pm ↑ Summary ORM/Role/Iterator.pm »