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

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 24 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 102 SCAR 17.9
CC
2
Cov
0%
to_xml
6.0
is_many_to_many
2.0
is_many_to_one
2.0
CRAP 6.0
1 =head1 NAME
2  
3 InterMine::Model::Reference - represents a reference in an InterMine class
4  
5 =head1 SYNOPSIS
6  
7 use InterMine::Model::Reference
8 ...
9 my $field = InterMine::Model::Reference->new(name => 'protein',
10 model => $model,
11 referenced_type_name => $ref_type,
12 reverse_reference_name =>
13 $reverse_reference);
14 ...
15  
16 =head1 DESCRIPTION
17  
18 Objects of this class describe the references and collections of a class
19 in an InterMine model. Reference objects are generally part of
20 ClassDescriptor objects.
21  
22 =cut
23  
24  
25  
26 package InterMine::Model::Reference;
27 use Moose;
28 with (
29 'InterMine::Model::Role::Descriptor',
30 'InterMine::Model::Role::Field',
31 );
32  
33 use MooseX::Types::Moose qw(Str Maybe);
34 use InterMine::Model::Types qw(
35 ClassDescriptor MaybeClassDescriptor MaybeField
36 );
37  
38 =head1 CONSTANTS
39  
40 =head2 TAG_NAME
41  
42 the name for serialising references to xml
43  
44 =cut
45  
46 use constant TAG_NAME => "reference";
47  
48 =head1 ATTRIBUTES
49  
50 =head2 referenced_type_name (Str, ro)
51  
52 The name of the class of object this reference points to
53  
54 =cut
55  
56 has referenced_type_name => (
57 is => 'ro',
58 isa => Str,
59 required => 1,
60 );
61  
62 =head2 referenced_classdescriptor (ClassDescriptor, ro)
63  
64 The class of the object that this reference points to
65  
66 =cut
67  
68 has referenced_classdescriptor => (
69 is => 'ro',
70 isa => ClassDescriptor,
71 lazy => 1,
72 default => sub {
73 my $self = shift;
74 my $type_name = $self->referenced_type_name();
75 return $self->model->get_classdescriptor_by_name($type_name);
76 },
77 );
78  
79 =head2 rev_referenced_classdescriptor (ClassDescriptor, ro)
80  
81 The class that the other end of this reference points to.
82  
83 =cut
84  
85 has rev_referenced_classdescriptor => (
86 is => 'ro',
87 isa => MaybeClassDescriptor,
88 lazy => 1,
89 default => sub {
90 my $self = shift;
91 my $name = $self->name();
92 return $self->model->get_referenced_classdescriptor($name);
93 },
94 );
95  
96 =head2 reverse_reference (Reference, ro)
97  
98 The reference (if it exists) that points back to this one
99  
100 =head3 predicate: has_reverse_reference
101  
102 =cut
103  
104 has reverse_reference => (
105 is => 'ro',
106 isa => MaybeField,
107 lazy => 1,
108 default => sub {
109 my $self = shift;
110 return undef unless $self->has_reverse_reference;
111 my $referenced_cd = $self->referenced_classdescriptor();
112 my $reverse_reference_name = $self->reverse_reference_name();
113 return $referenced_cd->get_field_by_name($reverse_reference_name);
114 },
115 );
116  
117 =head2 reverse_reference_name (Str, ro)
118  
119 The name of the reference that points back to this
120  
121 =cut
122  
123 has reverse_reference_name => (
124 is => 'ro',
125 isa => Maybe[Str],
126 predicate => 'has_reverse_reference',
127 );
128  
129 =head1 METHODS
130  
131 =head2 is_many_to_many
132  
133 Return true if this reference is one end of a many-to-many relation,
134 ie. this end is a collection and the other end is a collection
135  
136 =cut
137  
138 sub is_many_to_many {
139 my $self = shift;
140 return (
141 $self->isa('InterMine::Model::Collection')
142 and $self->has_reverse_reference
143 and $self->reverse_reference->isa('InterMine::Model::Collection')
144 );
145 }
146  
147 =head2 is_many_to_one
148  
149 Return true if this is the reference end of a one-to-many relation,
150 ie. this end is a reference and the other end is a collection
151  
152 =cut
153  
154 sub is_many_to_one {
155 my $self = shift;
156 return (
157 not $self->isa('InterMine::Model::Collection')
158 and $self->has_reverse_reference()
159 and $self->reverse_reference()->isa('InterMine::Model::Collection')
160 );
161 }
162  
163 =head2 is_many_to_0
164  
165 Return true if this is a collection and there is no reverse reference
166  
167 =cut
168  
169 sub is_many_to_0 {
170 my $self = shift;
171 return (
172 $self->isa( 'InterMine::Model::Collection' )
173 and not $self->has_reverse_reference
174 );
175 }
176  
177 =head2 is_one_to_many
178  
179 Return true if this is the collection end of a one-to-many relation,
180 ie. this end is a collection and the other end is a reference
181  
182 =cut
183  
184 sub is_one_to_many {
185 my $self = shift;
186 return (
187 $self->isa('InterMine::Model::Collection')
188 and $self->has_reverse_reference
189 and not $self->reverse_reference->isa('InterMine::Model::Collection')
190 );
191 }
192  
193 =head2 is_one_to_0
194  
195 Return true if this is a reference and there is no reverse reference
196  
197 =cut
198  
199 sub is_one_to_0 {
200 my $self = shift;
201 return (
202 not $self->isa('InterMine::Model::Collection')
203 and not $self->has_reverse_reference);
204 }
205  
206 sub _get_moose_type {
207 my $self = shift;
208 return $self->referenced_type_name;
209 }
210  
211 around '_get_moose_options' => sub {
212 my $orig = shift;
213 my $self = shift;
214 my @ops = $self->$orig(@_);
215 push @ops, (coerce => 1);
216 return @ops;
217 };
218  
219 =head2 to_xml
220  
221 Return the xml representation of the attribute descriptor
222  
223 =cut
224  
225 sub to_xml {
226 my $self = shift;
227 return sprintf(qq{<%s name="%s" referenced-type="%s" %s/>},
228 $self->TAG_NAME,
229 $self->name,
230 $self->referenced_type_name,
231 ($self->reverse_reference_name
232 ? 'reverse-reference="' . $self->reverse_reference_name . '"'
233 : ''),
234 );
235 }
236  
237 __PACKAGE__->meta->make_immutable;
238 no Moose;
239  
240 1;
241  
242 __END__
243  
244 =head1 SEE ALSO
245  
246 =over 4
247  
248 =item L<InterMine::Model::ClassDescriptor>
249  
250 =item L<InterMine::Model::Role::Field>
251  
252 =item L<InterMine::Model::Role::Descriptor>
253  
254 =back
255  
256 =head1 AUTHOR
257  
258 FlyMine C<< <support@flymine.org> >>
259  
260 =head1 BUGS
261  
262 Please report any bugs or feature requests to C<support@flymine.org>.
263  
264 =head1 SUPPORT
265  
266 You can find documentation for this module with the perldoc command.
267  
268 perldoc InterMine::Model::Reference
269  
270 You can also look for information at:
271  
272 =over 4
273  
274 =item * FlyMine
275  
276 L<http://www.flymine.org>
277  
278 =back
279  
280 =head1 COPYRIGHT & LICENSE
281  
282 Copyright 2006,2007,2008,2009,2010,2011 FlyMine, all rights reserved.
283  
284 This program is free software; you can redistribute it and/or modify it
285 under the same terms as Perl itself.