Composite/Container.pm

filter: Statementstmt 85.5% 83 / 97 Branchbran 60.8% 28 / 46 Conditioncond 53.8% 7 / 13 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 86.9% 20 / 23 Podpod 100% 17 / 17 totaltotal 77.3% 157 / 203 CC 32 SCAR 13.9
CC
32
Cov
76%
elempointer
10.8
_addThis
6.0
previousElement
6.0
CRAP 4.0
↑ Summary Composite.pm »
1 =head1 NAME
2  
3 Class::Composite::Container - Collection of Class::Composite::Element
4  
5 =head1 SYNOPSIS
6  
7 use Class::Composite::Container;
8 my $c = Class::Composite::Container->new();
9 $c->nextElement();
10 $c->resetPointer();
11 $c->nextElement();
12 $obj = $c->getElement();
13 $list = $c->getElements(2, 5);
14  
15 =head1 DESCRIPTION
16  
17 I<Class::Composite::Container> acts as a collection of elements.
18  
19 =head1 INHERITANCE
20  
21 Class::Composite
22  
23 =cut
24 package Class::Composite::Container;
25  
26 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use Scalar::Util qw ( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 2 ▶use base qw( Class::Composite );
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 2 our $VERSION = 0.1;
33  
34  
35 =head1 METHODS
36  
37 =head2 init( %param )
38  
39 Parameters are 'elements' and 'elempointer'
40 init is useful for inheritance, you don't need to redefine new(), but you can redefined init().
41 See I<Base::Class> documentation.
42  
43 =cut
44 3 ▶sub init : method {
Subroutine init: called CC=3 SCAR=11.3
Pod documented
45 3 my $self = shift;
46 3 $self->SUPER::init( @_ );
47 3 ▶ $self->{elements} ||= [];
Condition50%1/2
$self->{"elements"} ||= []
l!l
03
MC/DC0%0/1
$self->{"elements"} ||= []
$self->{"elements"}
Truth table50%1/2
A
A= $self->{"elements"}
Aresult
11
00
48 3 ▶ $self->{elempointer} ||= 0;
Condition50%1/2
$self->{"elempointer"} ||= 0
l!l
03
MC/DC0%0/1
$self->{"elempointer"} ||= 0
$self->{"elempointer"}
Truth table50%1/2
A
A= $self->{"elempointer"}
Aresult
11
00
49 3 $self;
50 }
51  
52  
53 =head2 elements( ARRAY )
54  
55 Get/Set elements array
56  
57 =cut
58 1 ▶sub elements : method {
Subroutine elements: called CC=3 SCAR=11.3
Pod documented
59 1 my $self = shift;
60 1 ▶ my $elem = shift or return $self->{elements};
Branch: 1 branch
BranchTrueFalseTotal
my $elem = shift() or return $self->{"elements"} 1 0 1
61 1 ▶ ref $elem eq 'ARRAY' or return $self->_warn("Not an array ref: $elem");
Branch: 1 branch
BranchTrueFalseTotal
ref $elem eq "ARRAY" or return $self->_warn("Not an array ref: $elem") 1 0 1
62 1 $self->{elements} = $elem;
63 1 $self;
64 }
65  
66  
67 =head2 elempointer( integer )
68  
69 Gets or sets the elements pointer. The pointer is garenteed to be between 0 and the number of elements - 1
70  
71 =cut
72 1 ▶sub elempointer : method {
Subroutine elempointer: called CC=4 SCAR=23.7
Pod documented
73 1 my ($self, $elem) = @_;
74 1 ▶ defined $elem or return $self->{elempointer};
Branch: 1 branch
BranchTrueFalseTotal
defined $elem or return $self->{"elempointer"} 0 1 1
75 0 ▶ $elem = $self->nOfElements - 1 if $elem >= $self->nOfElements;
Branch: 1 branch
BranchTrueFalseTotal
if $elem >= $self->nOfElements 0 0 0
76 0 ▶ $elem = 0 if $elem < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $elem < 0 0 0 0
77 0 $self->{elempointer} = int $elem;
78 0 $self;
79 }
80  
81  
82 =head2 addElement( @elements )
83  
84 The I<addElement()> method adds elements to the collection.
85 Returns the collection if ok.
86  
87 =cut
88 6 ▶sub addElement : method {
Subroutine addElement: called CC=3 SCAR=11.0
Pod documented
89 6 my ($self, @args) = @_;
90  
91 6 foreach (@args) {
92 9 ▶ $self->_addThis(this => $_) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_addThis("this", $_) or return 8 1 9
93 }
94 5 $self;
95 }
96  
97  
98 =head2 addElementFlat( @elements )
99  
100 Same than addElement, but flatten everything before adding.
101 If you add a collection, it will add all elements individually, not the collection.
102 Returns the collection if ok.
103  
104 =cut
105 1 ▶sub addElementFlat : method {
Subroutine addElementFlat: called CC=3 SCAR=11.1
Pod documented
106 1 my ($self, @args) = @_;
107 1 foreach (@args) {
108 1 ▶ $self->_addThis( this => $_,
Branch: 1 branch
BranchTrueFalseTotal
$self->_addThis("this", $_, "flat", 1) or return 1 0 1
109 flat => 1 ) or return;
110 }
111 1 $self;
112 }
113  
114  
115 #
116 # _addThis(this => $id or $object, flat => 0/1)
117 #
118 10 ▶sub _addThis {
Subroutine _addThis: called CC=6 SCAR=18.0
119 10 my ($self, %args) = @_;
120 10 my $object = $args{this};
121 10 ▶ my $flat = $args{flat} || 0;
Condition100%2/2
$args{'flat'} || 0
l!l
19
MC/DC100%1/1
$args{'flat'} || 0
$args{'flat'}
Truth table100%2/2
A
A= $args{'flat'}
Aresult
11
00
122 10 ▶ $self->checkElement( $object ) or return $self->_warn("Element to add is not of type ".$self->elementType);
Branch: 1 branch
BranchTrueFalseTotal
$self->checkElement($object) or return $self->_warn('Element to add is not of type ' . $self->elementType) 9 1 10
123 9 ▶ if ($flat and ref($object)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flat and ref $object) { } 1 8 9
Condition66%2/3
$flat and ref $object
!ll&&!rl&&r
801
MC/DC50%1/2
$flat and ref $object
$flat ref $object
Truth table66%2/3
A and B
A= $flatB= ref $object
ABresult
0X0
100
111
124 1 ▶ my $objects = $object->isa('Class::Composite::Container') ? $object->getLeaves
Branch: 1 branch
BranchTrueFalseTotal
$object->isa('Class::Composite::Container') ? : 1 0 1
125 : [ $object ];
126 1 $self->_addTheseObj( $objects );
127 }
128 else {
129 8 $self->_addTheseObj( [$object] );
130 }
131 }
132  
133  
134 9 ▶sub _addTheseObj {
Subroutine _addTheseObj: called CC=1 SCAR=0.0
135 9 my ($self, $objects) = @_;
136 9 push @{$self->{elements}}, @$objects;
137 }
138  
139  
140 =head2 checkElement( $elem )
141  
142 Returns true if $elem can be added to the container.
143 The element must be of the same type than elementType() (see I<Class::Composite>) or is undef.
144 This method is called for each element added to the collection.
145  
146 =cut
147 10 ▶sub checkElement {
Subroutine checkElement: called CC=4 SCAR=13.9
Pod documented
148 10 my ($self, $elem) = @_;
149 10 ▶ my $type = $self->elementType or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $type = $self->elementType or return 1 10 0 10
150 10 ▶ return 1 unless defined $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elem 1 9 10
151 9 ▶ if (blessed($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($elem)) { } 8 1 9
152 8 return $elem->isa($type);
153 }
154 else {
155 1 return ref($elem) eq $type;
156 }
157 }
158  
159  
160 =head2 getElement( $index )
161  
162 Returns the element asked. If $index < 0 it backtracks from the last element.
163 If no index is given, returns the current element.
164  
165 =cut
166 5 ▶sub getElement {
Subroutine getElement: called CC=4 SCAR=13.9
Pod documented
167 5 my ($self, $id) = @_;
168 5 ▶ $id = $self->elempointer unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 1 4 5
169  
170 5 ▶ return unless exists $self->{elements};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"elements"} 0 5 5
171  
172 5 ▶ if (defined( $self->{elements}->[$id] )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'elements'}[$id]) { } 4 1 5
173 4 return $self->{elements}->[$id];
174 } else {
175 1 return undef;
176 }
177 }
178  
179  
180 =head2 getElements( $start, $end )
181  
182 Returns an array ref of elements.
183 $start and $end are indexes.
184 $end is optional - if not given all elements after $start are returned.
185 If neither $start and $end are given, returns all elements.
186  
187 =cut
188 7 ▶sub getElements {
Subroutine getElements: called CC=4 SCAR=14.3
Pod documented
189 7 my ($self, $start, $end) = @_;
190 7 ▶ $start ||= 0;
Condition50%1/2
$start ||= 0
l!l
07
MC/DC0%0/1
$start ||= 0
$start
Truth table50%1/2
A
A= $start
Aresult
11
00
191 7 ▶ return [] unless $self->{elements};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"elements"} 0 7 7
192 7 ▶ $end = @{$self->{elements}} - 1 unless defined $end;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $end 7 0 7
193 7 [ @{$self->{elements}}[$start .. $end] ];
194 }
195  
196  
197 =head2 removeElement( $index )
198  
199 If no $index is given, the current element is removed.
200 Rearrange the collection by shifting to the left the elements > $elem.
201 Returns the element removed if ok.
202  
203 =cut
204 1 ▶sub removeElement {
Subroutine removeElement: called CC=2 SCAR=7.0
Pod documented
205 1 my ($self, $index) = @_;
206 1 ▶ $index = $self->elempointer unless defined $index;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 0 1 1
207 1 splice @{ $self->{elements} }, $index, 1;
208 }
209  
210  
211 =head2 removeAll()
212  
213 Reset the collection and returns the collection.
214  
215 =cut
216 1 ▶sub removeAll {
Subroutine removeAll: called CC=1 SCAR=0.0
Pod documented
217 1 my $self = shift;
218 1 $self->{elements} = [];
219 1 $self->{elempointer} = 0;
220 1 $self;
221 }
222  
223  
224 =head2 nOfElements()
225  
226 Returns the number of elements
227  
228 =cut
229 7 ▶sub nOfElements {
Subroutine nOfElements: called CC=2 SCAR=6.9
Pod documented
230 7 ▶ scalar(@{$_[0]->{elements}}) || 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$_[0]{"elements"};} or 0 6 1 7
231 }
232  
233  
234 =head2 nextElement()
235  
236 Returns the current element and increments the internal index.
237 You can use in a while loop such as:
238  
239 while ( my $elem = $container->nextElement ) { ... }
240  
241 =cut
242 4 ▶sub nextElement {
Subroutine nextElement: called CC=1 SCAR=0.0
Pod documented
243 4 my $self = shift;
244 4 $self->getElement( $self->incrPointer() );
245 }
246  
247  
248 =head2 previousElement()
249  
250 Decrements the internal index and returns the element
251  
252 =cut
253 0 ▶sub previousElement {
Subroutine previousElement: not called CC=2 SCAR=17.9
Pod documented
254 0 my $self = shift;
255 0 ▶ $self->decrPointer or do { $self->resetPointer(); return };
Branch: 1 branch
BranchTrueFalseTotal
$self->decrPointer or do { $self->resetPointer; return } 0 0 0
256 0 $self->getElement();
257 }
258  
259  
260 =head2 incrPointer()
261  
262 Increments the internal index
263  
264 =cut
265 4 ▶sub incrPointer : method {
Subroutine incrPointer: called CC=1 SCAR=0.0
Pod documented
266 4 $_[0]->{elempointer}++;
267 }
268  
269  
270 =head2 decrPointer()
271  
272 Decrements the internal index
273  
274 =cut
275 0 ▶sub decrPointer : method {
Subroutine decrPointer: not called CC=1 SCAR=6.9
Pod documented
276 0 $_[0]->{elempointer}--;
277 }
278  
279  
280 =head2 resetPointer()
281  
282 Reset pointer to retrieve the first element again
283 Returns the pointer value before being reset
284  
285 =cut
286 0 ▶sub resetPointer : method {
Subroutine resetPointer: not called CC=2 SCAR=17.9
Pod documented
287 0 my $self = shift;
288 0 my $ret = $self->{elempointer};
289 0 ▶ $self->{elempointer} = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
290 0 $ret;
291 }
292  
293  
294 =head2 setPointer( $index )
295  
296 Set pointer to $index. The internal index will be set to 0 if $index < 0 and will be set to the number of elements - 1 if $index >= number of elements
297 Returns the pointer's former value
298  
299 =cut
300 1 ▶sub setPointer : method {
Subroutine setPointer: called CC=3 SCAR=11.2
Pod documented
301 1 my ($self, $i) = @_;
302 1 my $current = $self->{elempointer};
303 1 ▶ $i = $self->nOfElements - 1 if ($i >= $self->nOfElements);
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $self->nOfElements 0 1 1
304 1 ▶ $i = 0 if ($i < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $i < 0 0 1 1
305 1 $self->{elempointer} = $i;
306 1 $current;
307 }
308  
309  
310  
311 2 1;
312  
313 __END__
↑ Summary Composite.pm »