blib/lib/Oryx/DBM/Association/Hash.pm untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 133 CC 10 SCAR 27.1
CC
10
Cov
0%
update
42.0
delete
12.0
construct
6.0
CRAP 15.0
1 package Oryx::DBM::Association::Hash;
2  
3 use Oryx::DBM::Association::Reference;
4  
5 use base qw(Oryx::Association::Hash);
6 use Data::Dumper;
7 use Carp qw(carp);
8  
9 our $DEBUG = 1;
10  
11 sub create {
12 my ($self, $proto, $param) = @_;
13 }
14  
15 sub retrieve {
16 my ($self, $proto, $id) = @_;
17 }
18  
19 sub update {
20 my ($self, $proto, $obj) = @_;
21 my $accessor = $self->role;
22 my $value = $obj->{$accessor} || { };
23  
24 $proto->{$accessor} = { };
25 @{ $proto->{$accessor} }{ keys %$value } = map { $_->id } values %$value;
26 $DEBUG && $self->_carp("[update]: proto => '.$proto.' value => ".Dumper($proto->{$accessor}));
27  
28 if (%{tied(%$value)->deleted}) {
29 while (my ($key, $thing) = each %{tied(%$value)->deleted}) {
30 delete($proto->{$accessor}->{$key});
31 }
32 tied(%$value)->deleted({});
33 }
34 if (%{tied(%$value)->created}) {
35 while (my ($key, $thing) = each %{tied(%$value)->created}) {
36 $proto->{$accessor}->{$key} = defined $thing ? $thing->id : undef;
37 }
38 tied(%$value)->created({});
39 }
40 if (%{tied(%$value)->updated}) {
41 while (my ($key, $thing) = each %{tied(%$value)->updated}) {
42 $proto->{$accessor}->{$key} = defined $thing ? $thing->id : undef;
43 }
44 tied(%$value)->updated({});
45 }
46  
47 $self->update_backrefs($obj, values %$value);
48  
49 $obj->dbh->commit;
50 }
51  
52 sub delete {
53 my $self = shift;
54 my ($query, $obj) = @_;
55 my $accessor = $self->role;
56 my $value = $obj->$accessor;
57  
58 if ($self->constraint eq 'Composition') {
59 # composition, so cascade the delete
60 foreach my $thing (values %$value) {
61 $thing->delete;
62 }
63 } elsif ($self->constraint eq 'Aggregation') {
64 # aggregation so just clear the Hash
65 %$value = ();
66 }
67  
68 $self->update(@_);
69 }
70  
71 sub search {
72  
73 }
74  
75 sub construct {
76 my ($self, $obj) = @_;
77 my $assoc_name = $self->role;
78 my @args = ($self, $obj);
79  
80 $obj->{$assoc_name} = { } unless $obj->{$assoc_name};
81 tie %{$obj->{$assoc_name}}, __PACKAGE__, @args;
82  
83 $DEBUG && $self->_carp("constructed $obj, accessor => $assoc_name, returns => ".Dumper($obj->{$assoc_name}));
84 }
85  
86 sub load {
87 my ($self, $owner) = @_;
88  
89 my $Hash = { $owner->{$self->role} ? %{ $owner->{$self->role} } : () };
90 my @args;
91 foreach (keys(%$Hash)) {
92 @args = ($self, $Hash->{$_});
93 tie $Hash->{$_}, 'Oryx::DBM::Association::Reference', @args;
94 }
95 warn "Hash load => ".Dumper($Hash);
96 return $Hash;
97 }
98  
99 sub fetch {
100 my ($self, $thing, $owner) = @_;
101 return $thing;
102 }
103  
104 sub store {
105 my ($self, $thing, $owner) = @_;
106 return $thing;
107 }
108  
109 1;
110 __END__