blib/lib/Oryx/Association/Array.pm untested

filter: Statementstmt 0.0% 0 / 139 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 236 CC 20 SCAR 22.8
CC
20
Cov
0%
SPLICE
30.0
STORESIZE
20.0
STORE
12.0
CRAP 9.8
1 package Oryx::Association::Array;
2  
3 use base qw(Oryx::Association);
4  
5 our $DEBUG = 0;
6  
7 =head1 NAME
8  
9 Oryx::Association::Array - Abstract Array association meta-type for Oryx
10  
11 =head1 DESCRIPTION
12  
13 Abstract Array association meta-type for Oryx.
14  
15 The actual array which is kept internally is a list of L<Oryx::Association::Reference>
16 instances.
17  
18 This module implements a private C<tie> interface for Arrays
19 which is shared accross all Array association
20 implementation classes (L<Oryx::DBI::Association::Array>, for example)
21 as well as an abstract public interface used when subclassing.
22  
23 =head1 PUBLIC INTERFACE
24  
25 =over
26  
27 =item load
28  
29 loads the data from storage - with DBI style storage, this does a select
30 on the link table to build up the array of L<Oryx::Association::Reference>
31 instances.
32  
33 =item fetch
34  
35 used by FETCH (see L<perltie>)
36  
37 =item store
38  
39 used by STORE (see L<perltie>)
40  
41 =back
42  
43 =cut
44  
45 sub new {
46 my ($class, $proto) = @_;
47 return bless $proto, $class;
48 }
49  
50 sub load { $_[0]->_croak('abstract') }
51 sub fetch { $_[0]->_croak('abstract') }
52 sub store { $_[0]->_croak('abstract') }
53  
54 #=============================================================================
55 # TIE MAGIC
56 sub TIEARRAY {
57 my ($class, $meta, $owner) = @_;
58  
59 my $self = bless {
60 meta => $meta,
61 owner => $owner, # the object instance which owns this Value
62 created => { },
63 deleted => { },
64 updated => { },
65 }, $class;
66  
67 return $self;
68 }
69  
70 sub ARRAY {
71 my ($self) = @_;
72 unless (defined $self->{ARRAY}) {
73 $self->{ARRAY} = $self->{meta}->load($self->{owner});
74 }
75 $self->{ARRAY};
76 }
77  
78 sub FETCH {
79 my ($self, $index) = @_;
80 if ($index >= 0) {
81 return $self->{meta}->fetch($self->ARRAY->[$index], $self->{owner});
82 } else {
83 return undef;
84 }
85 }
86  
87 sub STORE {
88 my ($self, $index, $thing) = @_;
89  
90 $DEBUG && $self->_carp("STORE $index : ".$self->FETCHSIZE." : $thing");
91 if ($index >= $self->FETCHSIZE()) {
92 $self->_set_created($index, $thing);
93 if ($index > $self->FETCHSIZE()) {
94 $self->EXTEND($index);
95 }
96 } else {
97 $self->_set_updated($index, $thing);
98 }
99  
100 $self->ARRAY->[$index] = $self->{meta}->store($thing, $self->{owner});
101 }
102  
103 sub FETCHSIZE {
104 my $self = shift;
105 return scalar @{$self->ARRAY};
106 }
107  
108 sub STORESIZE {
109 my $self = shift;
110 my $count = shift;
111 if ($count > $self->FETCHSIZE()) {
112 foreach ($count - $self->FETCHSIZE() .. $count) {
113 $self->STORE($_-1, undef) unless defined $self->ARRAY->[$_-1];
114 }
115 } elsif ($count < $self->FETCHSIZE()) {
116 foreach (0 .. $self->FETCHSIZE() - $count - 2) {
117 $self->POP();
118 }
119 }
120 }
121  
122 sub EXTEND {
123 my $self = shift;
124 my $count = shift;
125 $self->STORESIZE( $count );
126 }
127  
128 sub EXISTS {
129 my $self = shift;
130 my $index = shift;
131 return exists $self->ARRAY->[$index];
132 }
133  
134 sub DELETE {
135 my $self = shift;
136 my $index = shift;
137 $self->_set_deleted($index, $self->ARRAY->[$index]);
138 delete $self->ARRAY->[$index];
139 return $self->FETCH($index);
140 }
141  
142 sub CLEAR {
143 my $self = shift;
144 my $index = 0;
145 while (@{$self->ARRAY}) {
146 $self->DELETE($index);
147 shift @{$self->ARRAY};
148 $index++;
149 }
150 }
151  
152 sub PUSH {
153 my $self = shift;
154 my @list = @_;
155 my $last = $self->FETCHSIZE();
156 $self->STORE( $last + $_, $list[$_] ) foreach 0 .. $#list;
157 return $self->FETCHSIZE();
158 }
159  
160 sub POP {
161 my $self = shift;
162 my $index = $self->FETCHSIZE() - 1;
163 my $thing = $self->FETCH($index);
164 $self->_set_deleted($index, $thing);
165 pop @{$self->ARRAY};
166 return $thing;
167 }
168  
169 sub SHIFT {
170 my $self = shift;
171 my $thing = $self->FETCH(0);
172 for (my $x = 1; $x < $self->FETCHSIZE(); $x++) {
173 $self->STORE($x - 1, $self->ARRAY->[$x]);
174 }
175 $self->POP();
176 return $thing;
177 }
178  
179 sub UNSHIFT {
180 my $self = shift;
181 my @list = @_;
182 my $size = scalar( @list );
183 my $old_size = $self->FETCHSIZE();
184  
185 # make room for our list
186 $self->STORESIZE($old_size + $size);
187  
188 # shift everything up from the end
189 for (my $x = $#{$self->ARRAY}; $x >= $size; $x--) {
190 $self->STORE($x, $self->ARRAY->[$x - $size]);
191 }
192  
193 # store the new values
194 $self->STORE($_, $list[$_]) foreach 0 .. $#list;
195 }
196  
197 sub SPLICE {
198 my $self = shift;
199 my $offset = shift || 0;
200 my $length = defined $_[0] ? shift : $self->FETCHSIZE() - $offset;
201  
202 my @list = @_;
203  
204 my @removed;
205  
206 # new total length ==
207 # current size + length of inserted list - length of splice
208 my $old_size = $self->FETCHSIZE();
209 my $new_size = $old_size + scalar(@list) - $length;
210  
211 # grab any removed items
212 push @removed, $self->FETCH($_) foreach ($offset .. $offset + $length);
213  
214 # if the new array is longer than the current size, move existing
215 # elements right by the delta starting at the end
216 my $delta = $new_size - $old_size;
217 if ($delta > 0) {
218 $self->STORESIZE($new_size);
219 for (my $x = $new_size - 1; $x > $offset; $x--) {
220 $self->STORE($x, $self->ARRAY->[$x - $delta]);
221 }
222 }
223 # else if new array is shorter, move existing elements left
224 # starting at offset + length and remove unused slots from the end
225 elsif ($delta < 0) {
226 for (my $x = $length + $delta; $x < $old_size; $x++) {
227 $self->STORE($x, $self->ARRAY->[$x - $delta]);
228 }
229 $self->STORESIZE($new_size - 1);
230 }
231  
232 # store the inserted list starting at offset
233 $self->STORE($_ + $offset, $list[$_]) foreach 0 .. $#list;
234  
235 # update all if the length has changed
236 if (abs($delta)) {
237 $self->_set_updated($_, $self->ARRAY->[$_])
238 foreach 0 .. $#{$self->ARRAY};
239 }
240  
241 return @removed;
242 }
243  
244 sub created { $_[0]->{created} = $_[1] if $_[1]; $_[0]->{created} };
245 sub updated { $_[0]->{updated} = $_[1] if $_[1]; $_[0]->{updated} };
246 sub deleted { $_[0]->{deleted} = $_[1] if $_[1]; $_[0]->{deleted} };
247  
248 # try to keep the database operations to a minimum...
249 sub _set_deleted {
250 my ($self, $index, $thing) = @_;
251 delete $self->updated->{$index} if $self->updated->{$index};
252 if ($self->created->{$index}) {
253 delete $self->created->{$index};
254 } else {
255 $self->deleted->{$index} = $thing;
256 }
257 }
258  
259 sub _set_created {
260 my ($self, $index, $thing) = @_;
261 if ($self->deleted->{$index}) {
262 $self->updated->{$index} = $thing;
263 delete $self->deleted->{$index};
264 } else {
265 $self->created->{$index} = $thing;
266 }
267 }
268  
269 sub _set_updated {
270 my ($self, $index, $thing) = @_;
271 delete $self->deleted->{$index} if $self->deleted->{$index};
272 if ($self->created->{$index}) {
273 $self->created->{$index} = $thing;
274 } else {
275 $self->updated->{$index} = $thing;
276 }
277 }
278  
279 1;
280  
281 =head1 SEE ALSO
282  
283 L<Oryx>, L<Oryx::Class>, L<Oryx::Association>
284  
285 =head1 AUTHOR
286  
287 Copyright (C) 2005 Richard Hundt <richard NO SPAM AT protea-systems.com>
288  
289 =head1 LICENSE
290  
291 This library is free software and may be used under the same terms as Perl itself.
292  
293 =cut