blib/lib/Oryx/Association.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 125 CC 12 SCAR 20.4
CC
12
Cov
0%
update_backrefs
20.0
new
12.0
role
12.0
CRAP 7.7
1 package Oryx::Association;
2  
3 use base qw(Oryx::MetaClass);
4  
5 =head1 NAME
6  
7 Association - abstract base class for Association types
8  
9 =head1 SYNOPSIS
10  
11 my $assoc = Oryx::Association->new($meta, $source);
12
13 $assoc->source; # association from
14 $assoc->class; # association to
15 $assoc->role; # name of association accessor
16 $assoc->type; # Array, Hash, Reference etc.
17 $assoc->constraint; # Aggregate or Composition
18 $assoc->is_weak;
19 $assoc->update_backrefs;
20 $assoc->link_table;
21  
22 =head1 DESCRIPTION
23  
24 This module represents an abstract base class for Oryx association
25 types.
26  
27 =head1 METHODS
28  
29 =over
30  
31 =item new( $meta, $source )
32  
33 The constructor returns the correct instance of the correct
34 subclass based on the C<type> field of the C<$meta> hashref passed
35 as an argument. The C<$source> argument is the name of the class
36 in which this association is defined (see L<Oryx::Class>)
37  
38 =cut
39  
40 sub new {
41 my ($class, $meta, $source) = @_;
42  
43 my $type_class = $class.'::'.$meta->{type};
44 eval "use $type_class"; $class->_croak($@) if $@;
45  
46 my $self = $type_class->new({
47 meta => $meta,
48 source => $source,
49 });
50  
51 eval 'use '.$self->class;
52 $self->_croak($@) if $@;
53  
54 no strict 'refs';
55 *{$source.'::'.$self->role} = $self->_mk_accessor;
56  
57 return $self;
58 }
59  
60 =item create
61  
62 Abstract (see implementing subclasses)
63  
64 =item retrieve
65  
66 Abstract (see implementing subclasses)
67  
68 =item update
69  
70 Abstract (see implementing subclasses)
71  
72 =item delete
73  
74 Abstract (see implementing subclasses)
75  
76 =item search
77  
78 Abstract (see implementing subclasses)
79  
80 =item construct
81  
82 Abstract (see implementing subclasses)
83  
84 =cut
85  
86 sub create { $_[0]->_croak("abstract") }
87 sub retrieve { $_[0]->_croak("abstract") }
88 sub update { $_[0]->_croak("abstract") }
89 sub delete { $_[0]->_croak("abstract") }
90 sub search { $_[0]->_croak("abstract") }
91 sub construct { $_[0]->_croak("abstract") }
92  
93 sub _mk_accessor {
94 my $assoc = shift;
95 my $assoc_name = $assoc->role;
96 return sub {
97 my $self = shift;
98 $self->{$assoc_name} = shift if @_;
99 $self->{$assoc_name};
100 };
101 }
102  
103 =item source
104  
105 Simple accessor to the source class in which this association is
106 defined.
107  
108 =cut
109  
110 sub source {
111 my $self = shift;
112 $self->{source};
113 }
114  
115 =item class
116  
117 Simple accessor to the target class with which the source class has
118 an associtation.
119  
120 =cut
121  
122 sub class {
123 my $self = shift;
124 unless (defined $self->{class}) {
125 $self->{class} = $self->getMetaAttribute("class");
126 }
127 $self->{class};
128 }
129  
130 =item role
131  
132 Simple accessor to the association accessor name defined in the
133 source class. Defaults to the target class' table name.
134  
135 =cut
136  
137 sub role {
138 my $self = shift;
139 unless (defined $self->{role}) {
140 $self->{role} = $self->getMetaAttribute("role");
141 unless ($self->{role}) {
142 # set some sensible defaults for creating the accessor
143 $self->{role} = $self->class->table;
144 }
145 }
146 $self->{role};
147 }
148  
149 =item type
150  
151 Reference, Array or Hash... defaults to Reference.
152  
153 =cut
154  
155 sub type {
156 my $self = shift;
157 unless (defined $self->{type}) {
158 $self->{type} = $self->getMetaAttribute("type")
159 || 'Reference';
160 }
161 $self->{type};
162 }
163  
164 =item is_weak
165  
166 Simple accessor to the C<is_weak> meta-attribute. This is used
167 for stopping Reference association types from creating a column
168 in the target class for storing a reverse association.
169  
170 =cut
171  
172 sub is_weak { $_[0]->getMetaAttribute('is_weak') }
173  
174 =item constraint
175  
176 Simple accessor to the C<constraint> meta-attribute. Values are:
177 Aggregate or Composition ... Aggregate is the default,
178 Composition causes deletes to cascade.
179  
180 =cut
181  
182 sub constraint {
183 my $self = shift;
184 unless (defined $self->{constraint}) {
185 $self->{constraint} = $self->getMetaAttribute("constraint")
186 || 'Aggregate';
187 }
188 $self->{constraint};
189 }
190  
191 =item update_backrefs
192  
193 Updates reverse Reference associations.
194  
195 B<NOTE:> Currently, reverse associations are made up of two
196 unidirectional associations... link tables are therefore not shared.
197 This will be fixed.
198  
199 =cut
200  
201 sub update_backrefs {
202 my ($self, $obj, @things) = @_;
203 foreach my $rev_assoc (values %{$self->class->associations}) {
204 unless ($rev_assoc->type eq 'Reference') {
205 $self->_carp(
206 'weak associations not supported for non-Reference types'
207 );
208 next;
209 }
210 if ($rev_assoc->class eq $self->source) {
211 my $backref = $rev_assoc->role;
212 foreach my $target (@things) {
213 $target->$backref($obj);
214 $target->update unless $rev_assoc->is_weak;
215 }
216 }
217 }
218 }
219  
220 =item link_table
221  
222 Returns a name for the link table for this association. Not relevant
223 for Reference associations as these don't require a link table.
224  
225 This is just a shortcut for:
226  
227 $self->source->table.'_'.$self->role.'_'.$self->class->table
228  
229 Override for custom association types as needed.
230  
231 =cut
232  
233 sub link_table {
234 my $self = shift;
235 return $self->source->table.'_'.$self->role.'_'.$self->class->table;
236 }
237  
238 1;
239  
240 =back
241  
242 =head1 AUTHOR
243  
244 Richard Hundt <richard NO SPAM AT protea-systems.com>
245  
246 =head1 THANKS TO
247  
248 Andrew Sterling Hanencamp
249  
250 =head1 LICENCE
251  
252 This module is free software and may be used under the same terms as
253 Perl itself.
254  
255 =cut
256