blib/lib/InterMine/Model/ClassDescriptor.pm untested

filter: Statementstmt 0.0% 0 / 139 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 21 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 227 SCAR 50.5
CC
12
Cov
0%
_make_fields_into_attributes
30.0
add_field
12.0
sub_class_of
12.0
CRAP 156.0
1 package InterMine::Model::ClassDescriptor;
2  
3 =head1 NAME
4  
5 InterMine::Model::ClassDescriptor - represents a class in an InterMine model
6  
7 =head1 SYNOPSIS
8  
9 use InterMine::Model::ClassDescriptor;
10  
11 ...
12 my $cd = InterMine::Model::ClassDescriptor->create(
13 "Gene" => (
14 model => $model,
15 parents => ["BioEntity"]
16 )
17 );
18  
19 =head1 DESCRIPTION
20  
21 Objects of this class contain the metadata that describes a class in an
22 InterMine model. Each class has a name, parent classes/interfaces and any
23 number of attributes, references and collections.
24  
25 InterMine class descriptors are sub classes of L<Moose::Meta::Class>,
26 and thus L<Class::MOP::Class>. Please refer to these packages for further
27 documentation.
28  
29 =cut
30  
31 use Moose;
32 extends qw/Moose::Meta::Class/;
33 with 'InterMine::Model::Role::Descriptor';
34  
35 use InterMine::Model::Types qw(
36 FieldHash ClassDescriptorList ClassDescriptor BigInt
37 );
38 use MooseX::Types::Moose qw(ArrayRef Str Bool);
39 use Moose::Util::TypeConstraints;
40 use Scalar::Util qw(refaddr blessed);
41  
42 use Carp qw(cluck);
43  
44 =head1 CLASS METHODS
45  
46 =head2 create( $name | $name, %attributes | $name, \%attributes | \%attributes )
47  
48 The class constructor inherited from L<Moose::Meta::Class>. Creates a new
49 ClassDescriptor metaclass.
50  
51 my $cd = InterMine::Model::ClassDescriptor->create(
52 "Gene" => (
53 model => $model,
54 parents => ["BioEntity"]
55 )
56 );
57  
58 Params:
59 =over 4
60 =item model - the InterMine::Model that this class is a part of
61 =item name - the class name
62 =item parents - a list of the classes and interfaces that this classes extends
63 =back
64  
65 In most normal use cases, the typical user should NOT need
66 to call this method. It is used internally when parsing the
67 model to build up the list of classes.
68  
69 =cut
70  
71 override create => sub {
72 my $class = shift;
73 my $ret = super;
74 $ret->superclasses($ret->superclasses(), 'InterMine::Model::Object');
75 return $ret;
76 };
77  
78 =head1 INSTANCE ATTRIBUTES
79  
80 =head2 name | package
81  
82 =over 4
83  
84 =item * unqualified_name
85  
86 returns the (unqualified) name of the class this class descriptor represents.
87  
88 $gene_meta->unqualified_name
89 # "Gene"
90  
91 =item * package
92  
93 This is the attribute inherited from Moose::Meta::Class, and returns the full
94 qualified class name that perl refers to the class internally as.
95  
96 $gene_meta->name
97 # InterMine::genomic::FlyMine::Gene
98  
99 =back
100  
101 =cut
102  
103 has unqualified_name => (
104 isa => 'Str',
105 is => 'ro',
106 init_arg => undef,
107 lazy_build => 1,
108 );
109  
110 sub _build_unqualified_name {
111 my $self = shift;
112 my $p = $self->name;
113 $p =~ s/.*:://;
114 return $p;
115 }
116  
117 has is_interface => (
118 isa => 'Bool',
119 is => 'ro',
120 default => 1,
121 );
122  
123 =head2 own_fields
124  
125 Fields that belong to this class directly.
126  
127 =head3 add_own_field($field)
128  
129 Add a field to the list
130  
131 =head3 get_own_fields
132  
133 Get the full list of fields declared in this class.
134  
135 =cut
136  
137 sub add_own_field {
138 my $self = shift;
139 my $field = shift;
140 $self->set_field($field->name, $field);
141 $field->field_class($self);
142 }
143  
144 sub get_own_fields {
145 my $self = shift;
146 return grep {$_->field_class eq $self} $self->fields;
147 }
148  
149 =head3 own_attributes
150  
151 Return the fields that are instances of L<InterMine::Model::Attribute>.
152 This is not to be confused with L<Class::MOP>'s C<get_all_attributes>.
153  
154 =cut
155  
156 sub own_attributes {
157 my $self = shift;
158 return grep {$_->isa('InterMine::Model::Attribute')} $self->get_own_fields;
159 }
160  
161 =head3 own_references
162  
163 Return all the fields that are instances of L<InterMine::Model::Reference>,
164 but not the subclass L<InterMine::Model::Collection>.
165  
166 =cut
167  
168 sub own_references {
169 my $self = shift;
170 return grep {
171 $_->isa('InterMine::Model::Reference') && ! $_->isa('InterMine::Model::Collection')
172 } $self->get_own_fields;
173 }
174  
175 =head3 own_collections
176  
177 Return all the fields that are instances of L<InterMine::Model::Collection>.
178  
179 =cut
180  
181 sub own_collections {
182 my $self = shift;
183 return grep {$_->isa('InterMine::Model::Collection')} $self->get_own_fields;
184 }
185  
186 =head2 fieldhash
187  
188 The map of fields for this class, including inherited fields.
189 It has the following accessors:
190  
191 =head3 set_field($name, $field)
192  
193 Set a field in the map
194  
195 =head3 get_field_by_name($name)
196  
197 Retrieve the named field.
198  
199 =head3 fields
200  
201 Retrieve all fields as a list
202  
203 =head3 valid_field($name)
204  
205 Returns true if there is a field of this name
206  
207 =cut
208  
209 has fieldhash => (
210 traits => [qw/Hash/],
211 is => 'ro',
212 isa => FieldHash,
213 default => sub { {} },
214 handles => {
215 set_field => 'set',
216 get_field_by_name => 'get',
217 fields => 'values',
218 valid_field => 'defined',
219 },
220 );
221  
222 =head3 attributes
223  
224 Return the fields that are instances of L<InterMine::Model::Attribute>.
225 This is not to be confused with L<Class::MOP>'s C<get_all_attributes>.
226  
227 =cut
228  
229 sub attributes {
230 my $self = shift;
231 return grep {$_->isa('InterMine::Model::Attribute')} $self->fields;
232 }
233  
234 =head3 references
235  
236 Return all the fields that are instances of L<InterMine::Model::Reference>,
237 but not the subclass L<InterMine::Model::Collection>.
238  
239 =cut
240  
241 sub references {
242 my $self = shift;
243 return grep {
244 $_->isa('InterMine::Model::Reference') && ! $_->isa('InterMine::Model::Collection')
245 } $self->fields;
246 }
247  
248 =head3 collections
249  
250 Return all the fields that are instances of L<InterMine::Model::Collection>.
251  
252 =cut
253  
254 sub collections {
255 my $self = shift;
256 return grep {$_->isa('InterMine::Model::Collection')} $self->fields;
257 }
258  
259  
260 =head2 parents
261  
262 The names of the immediate ancestors of this class.
263  
264 =cut
265  
266 has parents => (
267 is => 'ro',
268 isa => ArrayRef[Str],
269 traits => ['Array'],
270 auto_deref => 1,
271 handles => {
272 has_parents => 'count',
273 },
274  
275 );
276  
277 =head2 parental_class_descriptors
278  
279 return a list of the ClassDescriptor objects for the
280 classes/interfaces that this class directly extends
281  
282 my @parent_cds = $cd->parental_class_descriptors();
283  
284 Calling this method retrives the parents from the model
285 and also sets up superclass relationships
286 in Moose. It should not be called until the Model is completely
287 parsed. It is called automatically once the model has been
288 parsed.
289  
290 =cut
291  
292 has parental_class_descriptors => (
293 is => 'ro',
294 isa => ClassDescriptorList,
295 lazy => 1,
296 auto_deref => 1,
297 default => sub {
298 my $self = shift;
299 $self->superclasses($self->parents);
300 return [ map {$self->model->get_classdescriptor_by_name($_)}
301 $self->parents ];
302 },
303 );
304  
305 has _is_ready => (
306 is => 'ro',
307 isa => Bool,
308 default => 0,
309 writer => '_set_fixed',
310 );
311  
312 =head1 INSTANCE METHODS
313  
314 =head2 new_object
315  
316 The instantiation method inherited from L<Moose::Meta::Class>.
317 You should not normally need to use this directly. Instead call
318 the C<make_new> method in L<InterMine::Model>.
319  
320 =cut
321  
322 # sanitize input by removing undef attributes from the list
323  
324 around new_object => sub {
325 my $orig = shift;
326 my $self = shift;
327 my $args = (ref $_[0] eq 'HASH') ? $_[0] : {@_};
328 for my $key (keys %$args) {
329 my $value = $args->{$key};
330 delete $args->{$key} unless (defined $value);
331 # Horrible hacky solution to unnecessary warnings
332 # THIS SHOULD BE DELETED WHEN A BETTER COERCION
333 # SOLUTION CAN BE FOUND!!!!
334 if (blessed $value and $value->isa('JSON::Boolean')) {
335 $args->{$key} = $$value;
336 }
337 }
338 return $self->$orig($args);
339 };
340  
341 # and make name a synonym for package here.
342  
343  
344 =head2 get_ancestors
345  
346 The full inheritance list, including all ancestors in the model.
347  
348 =cut
349  
350 # Implemented as a method to avoid memory leaks
351 sub get_ancestors {
352 my $self = shift;
353 my @inheritance_path = ($self,);
354 my @classes = $self->parental_class_descriptors();
355 for my $class (@classes) {
356 push @inheritance_path, $class->get_ancestors;
357 }
358 return @inheritance_path;
359 }
360  
361 =head2 add_field(FieldDescriptor $field, Bool own)
362  
363 Add a field to the class. If there is already a field
364 of the same name, it will not be added twice. Setting the boolean
365 flag "own" marks this field as originating in this class
366  
367 See also: InterMine::Model->_fix_class_descriptors
368  
369 =cut
370  
371 sub add_field {
372 my ($self, $field, $own) = @_;
373  
374 return if defined $self->get_field_by_name($field->name);
375  
376 if ($own) {
377 $self->add_own_field($field);
378 } else {
379 $self->set_field($field->name, $field);
380 }
381 }
382  
383 use Moose::Util::TypeConstraints;
384  
385 sub _make_fields_into_attributes {
386 my $self = shift;
387 my @fields = $self->fields;
388  
389 $self->add_attribute("__fetched_fields", {
390 isa => 'HashRef',
391 traits => ['Hash'],
392 is => 'ro',
393 default => sub { {} },
394 handles => {__has_fetched => 'get'},
395 });
396  
397 for my $field (@fields) {
398 my $suffix = ucfirst($field->name);
399 my $get = $field->_type_is(Bool) ? 'is' : 'get';
400 my $options = {
401 reader => $get . $suffix,
402 writer => "set" . $suffix,
403 predicate => "has" . $suffix,
404 $field->_get_moose_options,
405 };
406  
407 my $isa = Moose::Util::TypeConstraints::find_type_constraint($options->{isa});
408 unless ($isa) {
409 $self->model->get_classdescriptor_by_name($field->referenced_type_name);
410 }
411  
412 $self->add_attribute($field->name, $options);
413 $self->add_method($field->name, sub { my $obj = shift;
414 my $reader = $get . $suffix;
415 my $writer = "set" . $suffix;
416 my $is_empty = $field->name . "_is_empty";
417 if (not ($obj->__has_fetched($field->name))
418 and (
419 ($field->isa("InterMine::Model::Collection") and ($obj->$is_empty))
420 or (not defined $obj->$reader)
421 )) {
422 my $fetched = $self->model->lazy_fetch($self, $field, $obj);
423 $obj->$writer($fetched) if $fetched;
424 $obj->__fetched_fields->{$field->name} = 1;
425 }
426 $obj->$reader;
427 });
428 }
429 }
430  
431 =head2 sub_class_of
432  
433 Returns true if and only if this class is a sub-class
434 of the given class or is the same class
435  
436 if ($cld->sub_class_of($other_cld)) { ... }
437  
438 =cut
439  
440 sub sub_class_of {
441 my $self = shift;
442 my $other_class_desc = shift;
443  
444 if ($self eq $other_class_desc) {
445 return 1;
446 } else {
447 for my $parent ($self->parental_class_descriptors()) {
448 if ($parent->sub_class_of($other_class_desc)) {
449 return 1;
450 }
451 }
452 }
453 return 0;
454 }
455  
456 =head2 superclass_of($other)
457  
458 Returns true if this class is a superclass of the other one, or if it
459 is the same class.
460  
461 =cut
462  
463 sub superclass_of {
464 my $self = shift;
465 my $other = shift;
466  
467 if (blessed $other and $other->isa(__PACKAGE__)) {
468 return $other->sub_class_of($self);
469 } else {
470 return $self->model->get_classdescriptor_by_name($other)->sub_class_of($self);
471 }
472 }
473  
474 =head2 to_string
475  
476 The stringification of a class-descriptor. By default, it stringifies to its
477 unqualified_name.
478  
479 =cut
480  
481 sub to_string {
482 my $self = shift;
483 return $self->unqualified_name;
484 }
485  
486 =head2 to_xml
487  
488 Returns a string containing an XML representation of the descriptor
489  
490 =cut
491  
492 sub to_xml {
493 my $self = shift;
494 my $xml = sprintf(qq{<class name="%s"%s is-interface="%s">\n},
495 $self->unqualified_name,
496 ($self->has_parents
497 ? ' extends="' . join(q[ ], $self->parental_class_descriptors) . '"'
498 : ''),
499 ($self->is_interface ? "true" : "false")
500 );
501 for my $field (
502 sort($self->own_attributes),
503 sort($self->own_references),
504 sort($self->own_collections)) {
505 $xml .= q[ ] x 4 . $field->to_xml . "\n";
506 }
507 $xml .= " </class>";
508 return $xml;
509 }
510  
511 1;
512  
513 =head1 SEE ALSO
514  
515 =over 4
516  
517 =item * L<Moose::Meta::Class>
518  
519 =back
520  
521 =head1 AUTHOR
522  
523 FlyMine C<< <support@flymine.org> >>
524  
525 =head1 BUGS
526  
527 Please report any bugs or feature requests to C<support@flymine.org>.
528  
529 =head1 SUPPORT
530  
531 You can find documentation for this module with the perldoc command.
532  
533 perldoc InterMine::Model::ClassDescriptor
534  
535 You can also look for information at:
536  
537 =over 4
538  
539 =item * FlyMine
540  
541 L<http://www.flymine.org>
542  
543 =back
544  
545 =head1 COPYRIGHT & LICENSE
546  
547 Copyright 2006,2007,2008,2009 FlyMine, all rights reserved.
548  
549 This program is free software; you can redistribute it and/or modify it
550 under the same terms as Perl itself.
551