| 1 | package Oryx::DBM::Association::Array; | ||
| 2 | |||
| 3 | use Oryx::DBM::Association::Reference; | ||
| 4 | use Data::Dumper; | ||
| 5 | |||
| 6 | use base qw(Oryx::Association::Array); | ||
| 7 | |||
| 8 | our $DEBUG = 0; | ||
| 9 | |||
| 10 | sub create { | ||
| 11 | my ($self, $proto, $param) = @_; | ||
| 12 | } | ||
| 13 | |||
| 14 | sub retrieve { | ||
| 15 | my ($self, $proto, $id) = @_; | ||
| 16 | $DEBUG && $self->_carp('[retrieve]: '.Dumper($proto)); | ||
| 17 | } | ||
| 18 | |||
| 19 | sub update { | ||
| 20 | my ($self, $proto, $obj) = @_; | ||
| 21 | my $accessor = $self->role; | ||
| 22 | my $value = $obj->{$accessor} || [ ]; | ||
| 23 | |||
| 24 | $proto->{$accessor} = [ map { $_->{id} } @$value ]; | ||
| 25 | $DEBUG && $self->_carp("[update]: proto => '.$proto.' value => ".Dumper($proto->{$accessor})); | ||
| 26 | |||
| 27 | if (%{tied(@$value)->deleted}) { | ||
| 28 | while (my ($index, $thing) = each %{tied(@$value)->deleted}) { | ||
| 29 | delete($proto->{$accessor}->[$index]); | ||
| 30 | } | ||
| 31 | tied(@$value)->deleted({}); | ||
| 32 | } | ||
| 33 | if (%{tied(@$value)->updated}) { | ||
| 34 | while (my ($index, $thing) = each %{tied(@$value)->updated}) { | ||
| 35 | $proto->{$accessor}->[$index] = defined $thing ? $thing->id : undef; | ||
| 36 | } | ||
| 37 | tied(@$value)->updated({}); | ||
| 38 | } | ||
| 39 | if (%{tied(@$value)->created}) { | ||
| 40 | while (my ($index, $thing) = each %{tied(@$value)->created}) { | ||
| 41 | $proto->{$accessor}->[$index] = defined $thing ? $thing->id : undef; | ||
| 42 | } | ||
| 43 | tied(@$value)->created({}); | ||
| 44 | } | ||
| 45 | |||
| 46 | $self->update_backrefs($obj, @$value); | ||
| 47 | } | ||
| 48 | |||
| 49 | sub delete { | ||
| 50 | my $self = shift; | ||
| 51 | my ($proto, $obj) = @_; | ||
| 52 | my $accessor = $self->role; | ||
| 53 | my $value = $obj->$accessor; | ||
| 54 | |||
| 55 | if ($self->constraint eq 'Composition') { | ||
| 56 | # cascade the delete | ||
| 57 | while (my $thing = pop @$value) { | ||
| 58 | $thing->delete; | ||
| 59 | } | ||
| 60 | } elsif ($self->constraint eq 'Aggregation') { | ||
| 61 | # just clear the Array | ||
| 62 | @$value = (); | ||
| 63 | } | ||
| 64 | |||
| 65 | $self->update(@_); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub search { | ||
| 69 | my ($self, $query) = @_; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub construct { | ||
| 73 | my ($self, $obj) = @_; | ||
| 74 | my $assoc_name = $self->role; | ||
| 75 | my @args = ($self, $obj); | ||
| 76 | |||
| 77 | $obj->{$assoc_name} = [ ] unless $obj->{$assoc_name}; | ||
| 78 | tie @{$obj->{$assoc_name}}, __PACKAGE__, @args; | ||
| 79 | |||
| 80 | $DEBUG && $self->_carp("constructed $obj, accessor => $assoc_name, returns => ".Dumper($obj->{$assoc_name})); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub load { | ||
| 84 | my ($self, $owner) = @_; | ||
| 85 | |||
| 86 | warn "IS THE ARRAY TIED? => ".tied($owner->{$self->role}); | ||
| 87 | # take a copy of the DBM array | ||
| 88 | my $assoc_name = $self->role; | ||
| 89 | my $Array = [ $owner->{$self->role} ? @{ $owner->{$self->role} } : () ]; | ||
| 90 | |||
| 91 | $DEBUG && $self->_carp('load: array => '.Dumper($Array).' owner => '.$owner); | ||
| 92 | |||
| 93 | my @args; | ||
| 94 | for (my $x = 0; $x < @$Array; $x++) { | ||
| 95 | @args = ($self, $Array->[$x]); | ||
| 96 | $Array->[$x] = Oryx::DBM::Association::Reference->TIESCALAR(@args); | ||
| 97 | } | ||
| 98 | |||
| 99 | return $Array; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub fetch { | ||
| 103 | my ($self, $thing, $owner) = @_; | ||
| 104 | if (ref $thing eq 'Oryx::DBM::Association::Reference') { | ||
| 105 | return $thing->FETCH(); | ||
| 106 | } | ||
| 107 | return $thing; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub store { | ||
| 111 | my ($self, $thing, $owner) = @_; | ||
| 112 | return $thing; | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; | ||
| 116 | __END__ |