| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
=head1 NAME |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Class::Persist::Proxy::Collection - Proxy for objects not loaded yet |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use qw( Class::Persist::Proxy::Collection ); |
|
8
|
|
|
|
|
|
|
my $proxy = Class::Persist::Proxy::Collection->new(); |
|
9
|
|
|
|
|
|
|
$proxy->class('Class::Persist::Address'); |
|
10
|
|
|
|
|
|
|
$proxy->owner( $contact ); |
|
11
|
|
|
|
|
|
|
$proxy->push($object1, $object2); |
|
12
|
|
|
|
|
|
|
$proxy->store(); |
|
13
|
|
|
|
|
|
|
$obj1 = $proxy->object_at_index(0); |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Replace several objects in the DB by a Proxy object. |
|
18
|
|
|
|
|
|
|
This allows delayed loading of objects. |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 INHERITANCE |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Class::Persist::Proxy EO::Array |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 METHODS |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=cut |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
package Class::Persist::Proxy::Collection; |
|
29
|
1
|
|
|
1
|
|
1814
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
34
|
|
|
30
|
1
|
|
|
1
|
|
5
|
use warnings::register; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
123
|
|
|
31
|
1
|
|
|
1
|
|
4
|
use base qw( Class::Persist::Proxy EO::Array ); |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
145
|
|
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head2 element |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
When called for the first time, create an array of proxies |
|
37
|
|
|
|
|
|
|
representing the real objects. |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=cut |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub element { |
|
42
|
|
|
|
|
|
|
my $self = shift; |
|
43
|
|
|
|
|
|
|
if (@_) { |
|
44
|
|
|
|
|
|
|
return $self->SUPER::element( @_ ); |
|
45
|
|
|
|
|
|
|
} |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
$self->load() unless $self->{element}; |
|
48
|
|
|
|
|
|
|
$self->SUPER::element; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head2 load() |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Replace all the element by proxies |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=cut |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub load { |
|
59
|
|
|
|
|
|
|
my $self = shift; |
|
60
|
|
|
|
|
|
|
my $class = $self->class or Class::Persist::Error::InvalidParameters->throw(text => "A class should be defined in proxy"); |
|
61
|
|
|
|
|
|
|
$self->loadModule( $class ) or return; |
|
62
|
|
|
|
|
|
|
my $owner = $self->owner or Class::Persist::Error::InvalidParameters->throw(text => "A owner should be defined in proxy"); |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my $ids = $class->oids_for_owner( $owner ); |
|
65
|
|
|
|
|
|
|
my @element; |
|
66
|
|
|
|
|
|
|
foreach my $id (@$ids) { |
|
67
|
|
|
|
|
|
|
my $proxy = Class::Persist::Proxy->new(); |
|
68
|
|
|
|
|
|
|
$proxy->real_id( $id ); |
|
69
|
|
|
|
|
|
|
$proxy->class( $class ); |
|
70
|
|
|
|
|
|
|
CORE::push @element, $proxy; |
|
71
|
|
|
|
|
|
|
} |
|
72
|
|
|
|
|
|
|
$self->element(\@element); |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head2 store() |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Store any non proxy element in the collection and proxy it |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=cut |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub store { |
|
83
|
|
|
|
|
|
|
my $self = shift; |
|
84
|
|
|
|
|
|
|
my $owner = $self->owner or Class::Persist::Error::InvalidParameters->throw(text => "A owner should be defined in proxy"); |
|
85
|
|
|
|
|
|
|
if (my $element = $self->{element}) { |
|
86
|
|
|
|
|
|
|
foreach my $elem (@$element) { |
|
87
|
|
|
|
|
|
|
next if $elem->isa('Class::Persist::Proxy'); |
|
88
|
|
|
|
|
|
|
$elem->owner( $owner ); |
|
89
|
|
|
|
|
|
|
$elem->store(); |
|
90
|
|
|
|
|
|
|
Class::Persist::Proxy->proxy( $elem ); |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
$self; |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub push { |
|
97
|
|
|
|
|
|
|
my $self = shift; |
|
98
|
|
|
|
|
|
|
my @elements = @_; |
|
99
|
|
|
|
|
|
|
$_->owner($self->owner) for @elements; |
|
100
|
|
|
|
|
|
|
return $self->SUPER::push(@elements); |
|
101
|
|
|
|
|
|
|
} |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub unshift { |
|
104
|
|
|
|
|
|
|
my $self = shift; |
|
105
|
|
|
|
|
|
|
my @elements = @_; |
|
106
|
|
|
|
|
|
|
$_->owner($self->owner) for @elements; |
|
107
|
|
|
|
|
|
|
return $self->SUPER::unshift(@elements); |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head2 delete( $index ) |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Without parameter, delete all the elements of the collection. |
|
115
|
|
|
|
|
|
|
If an index is given, deletes the related element. |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
sub delete { |
|
120
|
|
|
|
|
|
|
my $self = shift; |
|
121
|
|
|
|
|
|
|
my $index = shift; |
|
122
|
|
|
|
|
|
|
$self->{element} or $self->load() or return; |
|
123
|
|
|
|
|
|
|
if (defined($index)) { |
|
124
|
|
|
|
|
|
|
my $obj = $self->object_at_index($index) or return $self->record('Class::Persist::Error', "Cannot delete, element $index doesn't exist", 1); |
|
125
|
|
|
|
|
|
|
$obj->delete() or return; |
|
126
|
|
|
|
|
|
|
return $self->SUPER::delete($index); |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
else { |
|
129
|
|
|
|
|
|
|
if (my $element = $self->element) { |
|
130
|
|
|
|
|
|
|
foreach my $elem (@$element) { |
|
131
|
|
|
|
|
|
|
$elem->delete() or return; |
|
132
|
|
|
|
|
|
|
Class::Persist::Proxy->proxy( $elem ); |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
} |
|
135
|
|
|
|
|
|
|
} |
|
136
|
|
|
|
|
|
|
1; |
|
137
|
|
|
|
|
|
|
} |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
1; |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Class::Persist |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 AUTHOR |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Fotango |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=cut |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
# Local Variables: |
|
153
|
|
|
|
|
|
|
# mode:CPerl |
|
154
|
|
|
|
|
|
|
# cperl-indent-level: 2 |
|
155
|
|
|
|
|
|
|
# indent-tabs-mode: nil |
|
156
|
|
|
|
|
|
|
# End: |