File Coverage

blib/lib/Persistence/Relationship.pm
Criterion Covered Total %
statement 39 130 30.0
branch 0 48 0.0
condition 0 18 0.0
subroutine 13 26 50.0
pod 13 13 100.0
total 65 235 27.6


line stmt bran cond sub pod time code
1             package Persistence::Relationship;
2              
3 17     17   99 use strict;
  17         36  
  17         744  
4 17     17   91 use warnings;
  17         29  
  17         598  
5              
6 17     17   86 use vars qw($VERSION);
  17         31  
  17         849  
7 17     17   1333 use vars qw(@EXPORT_OK %EXPORT_TAGS $VERSION);
  17         27  
  17         929  
8              
9 17     17   137 use Abstract::Meta::Class ':all';
  17         36  
  17         2804  
10 17     17   15009 use Persistence::Fetchable ':all';
  17         50  
  17         2182  
11 17     17   104 use base qw(Exporter Persistence::Fetchable);
  17         32  
  17         1713  
12 17     17   89 use Carp 'confess';
  17         33  
  17         832  
13              
14 17     17   138 use constant NONE => 0;
  17         31  
  17         2171  
15 17     17   91 use constant ALL => 1;
  17         38  
  17         717  
16 17     17   87 use constant ON_INSERT => 2;
  17         27  
  17         1092  
17 17     17   83 use constant ON_UPDATE => 3;
  17         26  
  17         690  
18 17     17   86 use constant ON_DELETE => 4;
  17         38  
  17         37930  
19              
20              
21             $VERSION = 0.03;
22              
23             @EXPORT_OK = qw(LAZY EAGER NONE ALL ON_INSERT ON_UPDATE ON_DELETE);
24             %EXPORT_TAGS = (all => \@EXPORT_OK);
25              
26             =head1 NAME
27              
28             Persistence::Relationship - Object relationship mapping
29              
30             =head1 CLASS HIERARCHY
31              
32             Persistence::Fetchable
33             |
34             +----Persistence::Relationship
35              
36             =head1 SYNOPSIS
37              
38             use Persistence::Relationship ':all';
39              
40             =head1 DESCRIPTION
41              
42             Represents a base class for object relationship.
43              
44             =head1 EXPORT
45              
46             LAZY EAGER NONE ALL ON_INSERT ON_UPDATE ON_DELETE method by ':all' tag.
47              
48             =head2 ATTRIBUTES
49              
50             =over
51              
52             =item name
53              
54             Relationship name
55              
56             =cut
57              
58             has '$.name' => (required => 1);
59              
60              
61             =item attribute
62              
63             =cut
64              
65             has '$.attribute' => (required => 1);
66              
67              
68             =item attribute_name
69              
70             Attribute name
71              
72             =cut
73              
74             has '$.attribute_name';
75              
76              
77             =item fetch_method
78              
79             LAZY, EAGER
80              
81             =cut
82              
83             has '$.fetch_method' => (default => LAZY);
84              
85              
86             =item cascade
87              
88             NONE, ALL ON_UPDATE, ON_DELETE, ON_INSERT
89              
90             =cut
91              
92             has '$.cascade' => (default => NONE);
93              
94              
95             =item orm
96              
97             =cut
98              
99             has '$.orm' => (associated_class => 'Persistence::ORM', the_other_end => 'lobs');
100              
101              
102             =back
103              
104             =head2 METHODS
105              
106             =over
107              
108             =cut
109              
110             =item add_relationship
111              
112             Adds relationship to meta data cache,
113             Takes package name of persisitence mapping, name of relationsship, reelationship constructor parameters.
114              
115             =cut
116              
117              
118             sub add_relationship {
119 0     0 1   my ($class, $package, $name, %args) = (@_);
120 0           my $orm = Persistence::ORM::mapping_meta($package);
121 0           my $attribute_class = $orm->mop_attribute_adapter;
122 0           my $attribute = $args{attribute};
123 0 0         $attribute = $args{attribute} = $attribute_class->new(attribute => $attribute, column_name => $name)
124             unless $attribute->isa('Persistence::Attribute');
125 0           my $relation = $class->new(%args, name => $name);
126 0           $relation->set_attribute_name($attribute->name);
127 0 0         $attribute->associated_class
128             or confess "associated class must be defined for attribute: " . $attribute->name;
129 0           $orm->add_relationships($relation);
130 0 0         $relation->install_fetch_interceptor($attribute)
131             if ($relation->fetch_method eq LAZY);
132 0           $relation;
133             }
134              
135              
136             =item relationships
137              
138             =cut
139              
140             sub relationships {
141 0     0 1   my ($class, $package) = @_;
142 0           my $orm = Persistence::ORM::mapping_meta($package);
143 0           my $relationships = $orm->relationships;
144 0           $relationships;
145             }
146              
147              
148             =item insertable_to_many_relations
149              
150             Returns all to many relation where insert applies.
151              
152             =cut
153              
154             sub insertable_to_many_relations {
155 0     0 1   my ($class, $obj_class) = @_;
156 0 0         my $relations = $class->relationships($obj_class) or return;
157 0           my @result;
158 0           foreach my $attribute_name (keys %$relations) {
159 0           my $relation = $relations->{$attribute_name};
160 0 0         next if ref($relation) eq 'Persistence::Relationship::ToOne';
161 0           my $cascade = $relation->cascade;
162 0 0 0       next if($cascade ne ALL && $cascade ne ON_INSERT);
163 0           push @result, $relation;
164             }
165 0           @result;
166             }
167              
168              
169             =item insertable_to_one_relations
170              
171             Returns all to one relation where insert applies.
172              
173             =cut
174              
175             sub insertable_to_one_relations {
176 0     0 1   my ($class, $obj_class) = @_;
177 0 0         my $relations = $class->relationships($obj_class) or return;
178 0           my @result;
179 0           foreach my $attribute_name (keys %$relations) {
180 0           my $relation = $relations->{$attribute_name};
181 0 0         next unless ref($relation) eq 'Persistence::Relationship::ToOne';
182 0           my $cascade = $relation->cascade;
183 0 0 0       next if($cascade ne ALL && $cascade ne ON_INSERT);
184 0           push @result, $relation;
185             }
186 0           @result;
187             }
188              
189              
190             =item updatable_to_many_relations
191              
192             Returns all relation where insert applies.
193              
194             =cut
195              
196             sub updatable_to_many_relations {
197 0     0 1   my ($class, $obj_class) = @_;
198 0 0         my $relations = $class->relationships($obj_class) or return;
199 0           my @result;
200 0           foreach my $attribute_name (keys %$relations) {
201 0           my $relation = $relations->{$attribute_name};
202 0 0         next if ref($relation) eq 'Persistence::Relationship::ToOne';
203 0           my $cascade = $relation->cascade;
204 0 0 0       next if($cascade ne ALL && $cascade ne ON_UPDATE);
205 0           push @result, $relation;
206             }
207 0           @result;
208             }
209              
210              
211             =item updatable_to_one_relations
212              
213             Returns all relation where insert applies.
214              
215             =cut
216              
217             sub updatable_to_one_relations {
218 0     0 1   my ($class, $obj_class) = @_;
219 0 0         my $relations = $class->relationships($obj_class) or return;
220 0           my @result;
221 0           foreach my $attribute_name (keys %$relations) {
222 0           my $relation = $relations->{$attribute_name};
223 0 0         next if ref($relation) ne 'Persistence::Relationship::ToOne';
224 0           my $cascade = $relation->cascade;
225 0 0 0       next if($cascade ne ALL && $cascade ne ON_UPDATE);
226 0           push @result, $relation;
227             }
228 0           @result;
229             }
230              
231              
232             =item deleteable_to_many_relations
233              
234             Returns all to many relation where insert applies.
235              
236             =cut
237              
238             sub deleteable_to_many_relations {
239 0     0 1   my ($class, $obj_class) = @_;
240 0 0         my $relations = $class->relationships($obj_class) or return;
241 0           my @result;
242 0           foreach my $attribute_name (keys %$relations) {
243 0           my $relation = $relations->{$attribute_name};
244 0 0         next if ref($relation) eq 'Persistence::Relationship::ToOne';
245 0           my $cascade = $relation->cascade;
246 0 0 0       next if($cascade ne ALL && $cascade ne ON_DELETE);
247 0           push @result, $relation;
248             }
249 0           @result;
250             }
251              
252              
253             =item deleteable_to_one_relations
254              
255             Returns all to one relation where insert applies.
256              
257             =cut
258              
259             sub deleteable_to_one_relations {
260 0     0 1   my ($class, $obj_class) = @_;
261 0 0         my $relations = $class->relationships($obj_class) or return;
262 0           my @result;
263 0           foreach my $attribute_name (keys %$relations) {
264 0           my $relation = $relations->{$attribute_name};
265 0 0         next if ref($relation) ne 'Persistence::Relationship::ToOne';
266 0           my $cascade = $relation->cascade;
267 0 0 0       next if($cascade ne ALL && $cascade ne ON_DELETE);
268 0           push @result, $relation;
269             }
270 0           @result;
271             }
272              
273              
274             =item eager_fetch_relations
275              
276             =cut
277              
278             sub eager_fetch_relations {
279 0     0 1   my ($class, $obj_class) = @_;
280 0 0         my $relations = $class->relationships($obj_class) or return;
281 0           $class->eager_fetch_filter($relations);
282             }
283              
284              
285             =item lazy_fetch_relations
286              
287             =cut
288              
289             sub lazy_fetch_relations {
290 0     0 1   my ($class, $obj_class) = @_;
291 0 0         my $relations = $class->relationships($obj_class) or return;
292 0           $class->lazy_fetch_filter($relations);
293             }
294              
295              
296             =item install_fetch_interceptor
297              
298             =cut
299              
300             sub install_fetch_interceptor {
301 0     0 1   my ($self) = @_;
302 0           my $attribute = $self->attribute;
303 0           $attribute->install_fetch_interceptor($self->lazy_fetch_handler($self->attribute));
304             }
305              
306              
307              
308             =item values
309              
310             Returns relations values as array ref, takes object as parameter
311              
312             =cut
313              
314             sub values {
315 0     0 1   my ($self, $object) = @_;
316 0           my $values = $self->value($object);
317 0 0         ref($values) eq 'HASH' ? [values %$values] : $values;
318             }
319              
320              
321             =item value
322              
323             Returns relations value
324              
325             =cut
326              
327             sub value {
328 0     0 1   my ($self, $object) = @_;
329 0           my $attribute = $self->attribute;
330 0           my $accessor = $attribute->accessor;
331 0           $object->$accessor;
332             }
333              
334              
335              
336             1;
337              
338             __END__