oFono/Helpers/Container.pm untested

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 168 CC 6 SCAR 14.1
CC
6
Cov
0%
new
12.0
_remove_from
6.0
remove
6.0
CRAP 4.1
1 package Net::Radio::oFono::Helpers::Container;
2  
3 use strict;
4 use warnings;
5  
6 use 5.010;
7  
8 use List::Util qw(first);
9 use List::MoreUtils qw(firstidx);
10 use Params::Util qw(_INSTANCE _ARRAY0 _CODELIKE);
11 use Scalar::Util qw(blessed refaddr);
12  
13 =head1 NAME
14  
15 Net::Radio::oFono::Helpers::Container - simple container providing typical container functions
16  
17 =head1 DESCRIPTION
18  
19 This package implements a class which will act as a container for items of
20 any type. The container can be forced to accept only classes of a specific
21 type.
22  
23 =head1 INHERITANCE
24  
25 Net::Radio::oFono::Helpers::Container
26  
27 =head1 METHODS
28  
29 =head2 new
30  
31 Instantiates a new container object.
32  
33 B<Parameters>:
34  
35 All parameters are optional.
36  
37 =over 4
38  
39 =item I<array-ref>
40  
41 Reference to an array for managing items. B<Use with caution>, it can be
42 dangerous when two containers access the same list.
43  
44 =item I<class-name>
45  
46 Name of classes to restrict the type which can be added. Adding items which
47 are not of specified type (or derived), an exception is thrown via L<Assert>.
48  
49 =back
50  
51 =cut
52  
53 sub new
54 {
55 my $className = shift;
56 my $elements = shift // [];
57 my $restrictedType = shift;
58  
59 my $self = blessed($className) ? $className : bless( {}, $className );
60  
61 $elements = [] unless ( _ARRAY0($elements) );
62 $self->{elements} = $elements;
63 $self->{restrictedType} = $restrictedType, $self->{iteratorIndex} = 0;
64  
65 return $self;
66 }
67  
68 sub _affirm_type
69 {
70 my ( $self, $elem ) = @_;
71 return affirm { _INSTANCE( $elem, $self->{restrictedType} ) }
72 "parameter 0 violates type restriction to '" . $self->{restrictedType};
73 }
74  
75 sub _add_to
76 {
77 my ( $self, $elem, $ary ) = @_;
78 my $refelem = refaddr($elem);
79 defined( first( sub { refaddr($_) == $refelem; }, @{$ary} ) )
80 and croak("element at parameter 0 is already hold");
81 push( @{ _ARRAY0($ary) }, $elem );
82 return;
83 }
84  
85 =head2 add
86  
87 Adds a new item to the container.
88  
89 B<Parameters>:
90  
91 =over 4
92  
93 =item I<item>
94  
95 List of items to add to the container.
96  
97 =back
98  
99 =cut
100  
101 sub add
102 {
103 my ( $self, $elem ) = @_;
104  
105 defined( $self->{restrictedType} ) and $self->_affirm_type($elem);
106  
107 $self->_add_to( $elem, $self->{elements} );
108  
109 return $self;
110 }
111  
112 sub _remove_from
113 {
114 my ( $self, $elem, $ary ) = @_;
115 my $refelem = refaddr($elem);
116  
117 my $idx = firstidx( sub { refaddr($_) == $refelem; }, @{$ary} );
118 0 <= $idx and splice( @{$ary}, $idx, 1 );
119  
120 return wantarray ? $elem : ( $elem, $idx );
121 }
122  
123 sub _move_between
124 {
125 my ( $self, $elem, $sary, $dary ) = @_;
126  
127 $self->_remove_from( $elem, $sary );
128 $self->_add_to( $elem, $dary );
129  
130 return $elem;
131 }
132  
133 =head2 remove
134  
135 Removes an item from the container.
136  
137 B<Parameters>:
138  
139 =over 4
140  
141 =item I<item>
142  
143 Item to remove from the container.
144  
145 =back
146  
147 B<Returns>:
148  
149 The removed element in scalar mode and and array containing the removed
150 element at position 0 and it's index in the managed list at index 1 in
151 array mode.
152  
153 =cut
154  
155 sub remove
156 {
157 my ( $self, $elem ) = @_;
158  
159 defined( $self->{restrictedType} ) and $self->_affirm_type($elem);
160  
161 my $idx;
162 ( undef, $idx ) = $self->_remove_from( $elem, $self->{elements} );
163  
164 return wantarray ? ( $elem, $idx ) : $elem;
165 }
166  
167 sub _where
168 {
169 my ( $self, $elem, $ary ) = @_;
170 my $refelem = refaddr($elem);
171  
172 return firstidx( sub { refaddr($_) == $refelem; }, @{$ary} );
173 }
174  
175 =head2 contains
176  
177 Returns whether this container manages specified item or not, compared by
178 the item address (L<Scalar::Util::refaddr>).
179  
180 B<Parameters>:
181  
182 =over 4
183  
184 =item I<item>
185  
186 The item to search for being in this container or not
187  
188 =back
189  
190 B<Returns>:
191  
192 Boolean value - either it's in there or not.
193  
194 =cut
195  
196 sub contains
197 {
198 my ( $self, $elem ) = @_;
199  
200 defined( $self->{restrictedType} ) and $self->_affirm_type($elem);
201  
202 return 0 <= $self->_where($elem);
203 }
204  
205 =head2 clear
206  
207 Clears this container, resets iterator. For each item, explicitely remove
208 is called for clearing.
209  
210 =cut
211  
212 sub clear()
213 {
214 my $self = $_[0];
215  
216 while ( 0 < scalar( @{ $self->{elements} } ) )
217 {
218 my $elem = $self->{elements}->[-1];
219  
220 # seems we have a destruction problem and there're remaining undefs ...
221 # maybe odg (http://search.cpan.org/dist/ogd/) is a way out
222 if ( defined($elem) )
223 {
224 $self->remove($elem);
225 }
226 else
227 {
228 pop( @{ $self->{elements} } );
229 }
230 }
231 $self->{iteratorIndex} = 0;
232 $self->{inIteration} = 0;
233  
234 return $self;
235 }
236  
237 =head2 is_empty
238  
239 Returns true when the container is empty - false otherwise.
240  
241 =head2 n_elements
242  
243 Returns the number of elements contained.
244  
245 =cut
246  
247 sub is_empty() { 0 == scalar( @{ $_[0]->{elements} } ); }
248 sub n_elements() { scalar( @{ $_[0]->{elements} } ); }
249  
250 =head2 for_each
251  
252 Calls given sub for each item once, with the item as only argument. It's not
253 recommended to use this method - it's here for compatibility reasons only.
254 A better way to do sth. for each item is to use the iterator operators.
255  
256 =cut
257  
258 sub for_each(&)
259 {
260 my ( $self, $sub ) = @_;
261  
262 affirm { _CODELIKE($sub) } "Parameter at index 0 isn't coderef";
263  
264 foreach my $child ( @{ $self->{elements} } )
265 {
266 &{$sub}($child);
267 }
268  
269 return $self;
270 }
271  
272 sub DESTROY { $_[0]->clear(); }
273  
274 =head1 BUGS
275  
276 Please report any bugs or feature requests to C<bug-net-radio-ofono at rt.cpan.org>, or through
277 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Radio-oFono>. I will be notified, and then you'll
278 automatically be notified of progress on your bug as I make changes.
279  
280 If you think you've found a bug then please read "How to Report Bugs
281 Effectively" by Simon Tatham:
282 L<http://www.chiark.greenend.org.uk/~sgtatham/bugs.html>.
283  
284 =head1 SUPPORT
285  
286 You can find documentation for this module with the perldoc command.
287  
288 perldoc Net::Radio::oFono
289  
290 You can also look for information at:
291  
292 =over 4
293  
294 =item * RT: CPAN's request tracker (report bugs here)
295  
296 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Radio-oFono>
297  
298 If you think you've found a bug then please read "How to Report Bugs
299 Effectively" by Simon Tatham:
300 L<http://www.chiark.greenend.org.uk/~sgtatham/bugs.html>.
301  
302 =item * AnnoCPAN: Annotated CPAN documentation
303  
304 L<http://annocpan.org/dist/Net-Radio-oFono>
305  
306 =item * CPAN Ratings
307  
308 L<http://cpanratings.perl.org/d/Net-Radio-oFono>
309  
310 =item * Search CPAN
311  
312 L<http://search.cpan.org/dist/Net-Radio-oFono/>
313  
314 =back
315  
316 =head2 Where can I go for help with a concrete version?
317  
318 Bugs and feature requests are accepted against the latest version
319 only. To get patches for earlier versions, you need to get an
320 agreement with a developer of your choice - who may or not report the
321 issue and a suggested fix upstream (depends on the license you have
322 chosen).
323  
324 =head2 Business support and maintenance
325  
326 For business support you can contact Jens via his CPAN email
327 address rehsackATcpan.org. Please keep in mind that business
328 support is neither available for free nor are you eligible to
329 receive any support based on the license distributed with this
330 package.
331  
332 =head1 ACKNOWLEDGEMENTS
333  
334 =head1 AUTHOR
335  
336 Jens Rehsack, C<< <rehsack at cpan.org> >>
337  
338 =head1 LICENSE AND COPYRIGHT
339  
340 Copyright 2012 Jens Rehsack.
341  
342 This program is free software; you can redistribute it and/or modify it
343 under the terms of either: the GNU General Public License as published
344 by the Free Software Foundation; or the Artistic License.
345  
346 See http://dev.perl.org/licenses/ for more information.
347  
348 =cut
349  
350 #
351 1; # Packages must always end like this