| 1 | package Net::Radio::oFono::Helpers::IteratableContainer; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use 5.010; | ||
| 7 | |||
| 8 | use overload | ||
| 9 | q(-=) => \&decrease_by, | ||
| 10 | q(+=) => \&increase_by, | ||
| 11 | q(@{}) => \&as_array, | ||
| 12 | q(${}) => \&curr_item, | ||
| 13 | q(<>) => \&iterate, | ||
| 14 | q(bool) => sub { 1 }, | ||
| 15 | q(0+) => \&Net::Radio::oFono::Helpers::Container::n_elements; | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Net::Radio::oFono::Helpers::IteratableContainer - simple container allows iterating over contained elements | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This package implements a class which will act as a container for items of | ||
| 24 | any type. In addition to Net::Radio::oFono::Helpers::Container it allows iterating | ||
| 25 | over it's content. | ||
| 26 | |||
| 27 | Per default following operators to the container instances are overloaded: | ||
| 28 | |||
| 29 | =over 4 | ||
| 30 | |||
| 31 | =item C<@{}> | ||
| 32 | |||
| 33 | Gives plain access to the managed list of items. | ||
| 34 | |||
| 35 | =item C<bool> | ||
| 36 | |||
| 37 | Returns a boolean value (always true). | ||
| 38 | |||
| 39 | =item C<0+> | ||
| 40 | |||
| 41 | Returns the number of managed elements. | ||
| 42 | |||
| 43 | =item C<E<lt>E<gt>> | ||
| 44 | |||
| 45 | Iterates over all items and returns the currently selected on or undef, | ||
| 46 | if end of list is reached. | ||
| 47 | |||
| 48 | =item C<-=> | ||
| 49 | |||
| 50 | Reduces the iteration pointer by count. | ||
| 51 | |||
| 52 | =item C<+=> | ||
| 53 | |||
| 54 | Increases the iteration pointer by count. | ||
| 55 | |||
| 56 | =item C<${}> | ||
| 57 | |||
| 58 | Returns the currently selected item in an iteration. | ||
| 59 | |||
| 60 | =back | ||
| 61 | |||
| 62 | =head1 INHERITANCE | ||
| 63 | |||
| 64 | Net::Radio::oFono::Helpers::IteratableContainer | ||
| 65 | ISA Net::Radio::oFono::Helpers::Container | ||
| 66 | |||
| 67 | =head1 METHODS | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | sub _adjust_iter_idx | ||
| 72 | { | ||
| 73 | my ( $self, $idx ) = @_; | ||
| 74 | |||
| 75 | # ensure <> operator will take correctly next element | ||
| 76 | if ( $self->{inIteration} && ( $self->{iteratorIndex} == $idx ) ) | ||
| 77 | { | ||
| 78 | --$self->{iteratorIndex}; | ||
| 79 | } | ||
| 80 | |||
| 81 | return; | ||
| 82 | } | ||
| 83 | |||
| 84 | =head2 remove | ||
| 85 | |||
| 86 | Removes an item from the container. | ||
| 87 | |||
| 88 | B<Parameters>: | ||
| 89 | |||
| 90 | =over 4 | ||
| 91 | |||
| 92 | =item I<item> | ||
| 93 | |||
| 94 | Item to remove from the container. | ||
| 95 | |||
| 96 | =back | ||
| 97 | |||
| 98 | B<Returns>: | ||
| 99 | |||
| 100 | The removed element in scalar mode and and array containing the removed | ||
| 101 | element at position 0 and it's index in the managed list at index 1 in | ||
| 102 | array mode. | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub remove | ||
| 107 | { | ||
| 108 | my ( $self, $elem ) = @_; | ||
| 109 | |||
| 110 | my $idx; | ||
| 111 | ( undef, $idx ) = $self->SUPER::remove($elem); | ||
| 112 | $self->_adjust_iter_idx($idx); | ||
| 113 | |||
| 114 | return wantarray ? ( $elem, $idx ) : $elem; | ||
| 115 | } | ||
| 116 | |||
| 117 | =head2 decrease_by | ||
| 118 | |||
| 119 | Invoked via overloaded -= operator. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | sub decrease_by | ||
| 124 | { | ||
| 125 | my $self = shift; | ||
| 126 | $self->{iteratorIndex} -= $_[0]; | ||
| 127 | $self->{iteratorIndex} < 0 and $self->{iteratorIndex} = 0; | ||
| 128 | return $self; | ||
| 129 | } | ||
| 130 | |||
| 131 | =head2 increase_by | ||
| 132 | |||
| 133 | Invoked via overloaded += operator. | ||
| 134 | |||
| 135 | =cut | ||
| 136 | |||
| 137 | sub increase_by | ||
| 138 | { | ||
| 139 | my $self = shift; | ||
| 140 | $self->{iteratorIndex} += $_[0]; | ||
| 141 | $self->{iteratorIndex} > scalar( @{ $self->{elements} } ) | ||
| 142 | and $self->{iteratorIndex} = scalar( @{ $self->{elements} } ); | ||
| 143 | return $self; | ||
| 144 | } | ||
| 145 | |||
| 146 | =head2 as_array | ||
| 147 | |||
| 148 | Invoked by overloaded @{} operator. | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | sub as_array() { $_[0]->{elements}; } | ||
| 153 | |||
| 154 | =head2 iterate | ||
| 155 | |||
| 156 | Invoked by overloaded <> operator. | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | sub iterate() | ||
| 161 | { | ||
| 162 | my $self = shift; | ||
| 163 | my $elem; | ||
| 164 | |||
| 165 | unless ( $self->{inIteration} ) | ||
| 166 | { | ||
| 167 | ++$self->{inIteration}; | ||
| 168 | $self->{iteratorIndex} = -1; | ||
| 169 | } | ||
| 170 | |||
| 171 | if ( ++$self->{iteratorIndex} < scalar( @{ $self->{elements} } ) ) | ||
| 172 | { | ||
| 173 | $elem = $self->{elements}->[ $self->{iteratorIndex} ]; | ||
| 174 | } | ||
| 175 | else | ||
| 176 | { | ||
| 177 | --$self->{inIteration}; | ||
| 178 | } | ||
| 179 | |||
| 180 | return $elem; | ||
| 181 | } | ||
| 182 | |||
| 183 | =head2 n_elements | ||
| 184 | |||
| 185 | Returns the number of elements contained. | ||
| 186 | |||
| 187 | =head2 curr_item | ||
| 188 | |||
| 189 | Invoked by overloaded ${} operator. | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | sub curr_item() | ||
| 194 | { | ||
| 195 | $_[0]->{inIteration} and return $_[0]->{elements}->[ $_[0]->{iteratorIndex} ]; | ||
| 196 | return; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub DESTROY { $_[0]->clear(); } | ||
| 200 | |||
| 201 | =head1 BUGS | ||
| 202 | |||
| 203 | Please report any bugs or feature requests to C<bug-net-radio-ofono at rt.cpan.org>, or through | ||
| 204 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Radio-oFono>. I will be notified, and then you'll | ||
| 205 | automatically be notified of progress on your bug as I make changes. | ||
| 206 | |||
| 207 | If you think you've found a bug then please read "How to Report Bugs | ||
| 208 | Effectively" by Simon Tatham: | ||
| 209 | L<http://www.chiark.greenend.org.uk/~sgtatham/bugs.html>. | ||
| 210 | |||
| 211 | =head1 SUPPORT | ||
| 212 | |||
| 213 | You can find documentation for this module with the perldoc command. | ||
| 214 | |||
| 215 | perldoc Net::Radio::oFono | ||
| 216 | |||
| 217 | You can also look for information at: | ||
| 218 | |||
| 219 | =over 4 | ||
| 220 | |||
| 221 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 222 | |||
| 223 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Radio-oFono> | ||
| 224 | |||
| 225 | If you think you've found a bug then please read "How to Report Bugs | ||
| 226 | Effectively" by Simon Tatham: | ||
| 227 | L<http://www.chiark.greenend.org.uk/~sgtatham/bugs.html>. | ||
| 228 | |||
| 229 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 230 | |||
| 231 | L<http://annocpan.org/dist/Net-Radio-oFono> | ||
| 232 | |||
| 233 | =item * CPAN Ratings | ||
| 234 | |||
| 235 | L<http://cpanratings.perl.org/d/Net-Radio-oFono> | ||
| 236 | |||
| 237 | =item * Search CPAN | ||
| 238 | |||
| 239 | L<http://search.cpan.org/dist/Net-Radio-oFono/> | ||
| 240 | |||
| 241 | =back | ||
| 242 | |||
| 243 | =head2 Where can I go for help with a concrete version? | ||
| 244 | |||
| 245 | Bugs and feature requests are accepted against the latest version | ||
| 246 | only. To get patches for earlier versions, you need to get an | ||
| 247 | agreement with a developer of your choice - who may or not report the | ||
| 248 | issue and a suggested fix upstream (depends on the license you have | ||
| 249 | chosen). | ||
| 250 | |||
| 251 | =head2 Business support and maintenance | ||
| 252 | |||
| 253 | For business support you can contact Jens via his CPAN email | ||
| 254 | address rehsackATcpan.org. Please keep in mind that business | ||
| 255 | support is neither available for free nor are you eligible to | ||
| 256 | receive any support based on the license distributed with this | ||
| 257 | package. | ||
| 258 | |||
| 259 | =head1 ACKNOWLEDGEMENTS | ||
| 260 | |||
| 261 | =head1 AUTHOR | ||
| 262 | |||
| 263 | Jens Rehsack, C<< <rehsack at cpan.org> >> | ||
| 264 | |||
| 265 | =head1 LICENSE AND COPYRIGHT | ||
| 266 | |||
| 267 | Copyright 2012 Jens Rehsack. | ||
| 268 | |||
| 269 | This program is free software; you can redistribute it and/or modify it | ||
| 270 | under the terms of either: the GNU General Public License as published | ||
| 271 | by the Free Software Foundation; or the Artistic License. | ||
| 272 | |||
| 273 | See http://dev.perl.org/licenses/ for more information. | ||
| 274 | |||
| 275 | =cut | ||
| 276 | |||
| 277 | # | ||
| 278 | 1; # Packages must always end like this |