Persist/Proxy/Collection.pm

filter: Statementstmt 17.8% 10 / 56 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 3 / 9 Podpod 100% 6 / 6 totaltotal 18.2% 19 / 104 CC 21 SCAR 37.0
CC
21
Cov
11%
delete
90.0
load
30.0
store
30.0
CRAP 40.4
« Persist/Base.pm ↑ Summary Persist/Proxy.pm »
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use base qw( Class::Persist::Proxy EO::Array );
Subroutine BEGIN: called CC=1 SCAR=0.0
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 0 ▶sub element {
Subroutine element: not called CC=3 SCAR=24.8
Pod documented
42 0 my $self = shift;
43 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
44 0 return $self->SUPER::element( @_ );
45 }
46  
47 0 ▶ $self->load() unless $self->{element};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"element"} 0 0 0
48 0 $self->SUPER::element;
49 }
50  
51  
52 =head2 load()
53  
54 Replace all the element by proxies
55  
56 =cut
57  
58 0 ▶sub load {
Subroutine load: not called CC=5 SCAR=34.0
Pod documented
59 0 my $self = shift;
60 0 ▶ my $class = $self->class or Class::Persist::Error::InvalidParameters->throw(text => "A class should be defined in proxy");
Branch: 1 branch
BranchTrueFalseTotal
my $class = $self->class or "Class::Persist::Error::InvalidParameters"->throw("text", "A class should be defined in proxy") 0 0 0
61 0 ▶ $self->loadModule( $class ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->loadModule($class) or return 0 0 0
62 0 ▶ my $owner = $self->owner or Class::Persist::Error::InvalidParameters->throw(text => "A owner should be defined in proxy");
Branch: 1 branch
BranchTrueFalseTotal
my $owner = $self->owner or "Class::Persist::Error::InvalidParameters"->throw("text", "A owner should be defined in proxy") 0 0 0
63  
64 0 my $ids = $class->oids_for_owner( $owner );
65 0 my @element;
66 0 foreach my $id (@$ids) {
67 0 my $proxy = Class::Persist::Proxy->new();
68 0 $proxy->real_id( $id );
69 0 $proxy->class( $class );
70 0 CORE::push @element, $proxy;
71 }
72 0 $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 0 ▶sub store {
Subroutine store: not called CC=5 SCAR=34.0
Pod documented
83 0 my $self = shift;
84 0 ▶ my $owner = $self->owner or Class::Persist::Error::InvalidParameters->throw(text => "A owner should be defined in proxy");
Branch: 1 branch
BranchTrueFalseTotal
my $owner = $self->owner or "Class::Persist::Error::InvalidParameters"->throw("text", "A owner should be defined in proxy") 0 0 0
85 0 ▶ if (my $element = $self->{element}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $element = $self->{"element"}) 0 0 0
86 0 foreach my $elem (@$element) {
87 0 ▶ next if $elem->isa('Class::Persist::Proxy');
Branch: 1 branch
BranchTrueFalseTotal
if $elem->isa("Class::Persist::Proxy") 0 0 0
88 0 $elem->owner( $owner );
89 0 $elem->store();
90 0 Class::Persist::Proxy->proxy( $elem );
91 }
92 }
93 0 $self;
94 }
95  
96 0 ▶sub push {
Subroutine push: not called CC=2 SCAR=17.9
Pod documented
97 0 my $self = shift;
98 0 my @elements = @_;
99 0 $_->owner($self->owner) for @elements;
100 0 return $self->SUPER::push(@elements);
101 }
102  
103 0 ▶sub unshift {
Subroutine unshift: not called CC=2 SCAR=17.9
Pod documented
104 0 my $self = shift;
105 0 my @elements = @_;
106 0 $_->owner($self->owner) for @elements;
107 0 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 0 ▶sub delete {
Subroutine delete: not called CC=9 SCAR=45.0
Pod documented
120 0 my $self = shift;
121 0 my $index = shift;
122 0 ▶ $self->{element} or $self->load() or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"element"} or $self->load or return 0 0 0
Condition0%0/3
$self->{"element"} or $self->load
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"element"} or $self->load
$self->{"element"} $self->load
Truth table0%0/3
A or B
A= $self->{"element"}B= $self->load
ABresult
1X1
011
000
123 0 ▶ if (defined($index)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index) { } 0 0 0
124 0 ▶ my $obj = $self->object_at_index($index) or return $self->record('Class::Persist::Error', "Cannot delete, element $index doesn't exist", 1);
Branch: 1 branch
BranchTrueFalseTotal
my $obj = $self->object_at_index($index) or return $self->record("Class::Persist::Error", "Cannot delete, element $index doesn't exist", 1) 0 0 0
125 0 ▶ $obj->delete() or return;
Branch: 1 branch
BranchTrueFalseTotal
$obj->delete or return 0 0 0
126 0 return $self->SUPER::delete($index);
127 }
128 else {
129 0 ▶ if (my $element = $self->element) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $element = $self->element) 0 0 0
130 0 foreach my $elem (@$element) {
131 0 ▶ $elem->delete() or return;
Branch: 1 branch
BranchTrueFalseTotal
$elem->delete or return 0 0 0
132 0 Class::Persist::Proxy->proxy( $elem );
133 }
134 }
135 }
136 0 1;
137 }
138  
139  
140 1 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:
« Persist/Base.pm ↑ Summary Persist/Proxy.pm »