| 1 | package Template::Flute::Iterator; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Template::Flute::Iterator - Generic iterator class for Template::Flute | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | $cart = [{isbn => '978-0-2019-1622-4', | ||
| 13 | title => 'The Pragmatic Programmer', | ||
| 14 | quantity => 1}, | ||
| 15 | {isbn => '978-1-4302-1833-3', | ||
| 16 | title => 'Pro Git', | ||
| 17 | quantity => 1}, | ||
| 18 | ]; | ||
| 19 | |||
| 20 | $iter = new Template::Flute::Iterator($cart); | ||
| 21 | |||
| 22 | print "Count: ", $iter->count(), "\n"; | ||
| 23 | |||
| 24 | while ($record = $iter->next()) { | ||
| 25 | print "Title: ", $record->title(), "\n"; | ||
| 26 | } | ||
| 27 | |||
| 28 | $iter->reset(); | ||
| 29 | |||
| 30 | $iter->seed({isbn => '978-0-9779201-5-0', | ||
| 31 | title => 'Modern Perl', | ||
| 32 | quantity => 10}); | ||
| 33 | |||
| 34 | =head1 CONSTRUCTOR | ||
| 35 | |||
| 36 | =head2 new | ||
| 37 | |||
| 38 | Creates a Template::Flute::Iterator object. The elements of the | ||
| 39 | iterator are hash references. They can be passed to the constructor | ||
| 40 | as array or array reference. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | # Constructor | ||
| 45 | sub new { | ||
| 46 | my ($proto, @args) = @_; | ||
| 47 | my ($class, $self); | ||
| 48 | |||
| 49 | $class = ref($proto) || $proto; | ||
| 50 | |||
| 51 | $self = {}; | ||
| 52 | |||
| 53 | bless $self, $class; | ||
| 54 | |||
| 55 | $self->seed(@args); | ||
| 56 | |||
| 57 | return $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | =head1 METHODS | ||
| 61 | |||
| 62 | =head2 next | ||
| 63 | |||
| 64 | Returns next record or undef. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub next { | ||
| 69 | my ($self) = @_; | ||
| 70 | |||
| 71 | |||
| 72 | if ($self->{INDEX} <= $self->{COUNT}) { | ||
| 73 | return $self->{DATA}->[$self->{INDEX}++]; | ||
| 74 | } | ||
| 75 | |||
| 76 | return; | ||
| 77 | }; | ||
| 78 | |||
| 79 | =head2 count | ||
| 80 | |||
| 81 | Returns number of elements. | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | sub count { | ||
| 86 | my ($self) = @_; | ||
| 87 | |||
| 88 | return $self->{COUNT}; | ||
| 89 | } | ||
| 90 | |||
| 91 | =head2 reset | ||
| 92 | |||
| 93 | Resets iterator. | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | # Reset method - rewind index of iterator | ||
| 98 | sub reset { | ||
| 99 | my ($self) = @_; | ||
| 100 | |||
| 101 | $self->{INDEX} = 0; | ||
| 102 | |||
| 103 | return $self; | ||
| 104 | } | ||
| 105 | |||
| 106 | =head2 seed | ||
| 107 | |||
| 108 | Seeds iterator. | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub seed { | ||
| 113 | my ($self, @args) = @_; | ||
| 114 | |||
| 115 | if (ref($args[0]) eq 'ARRAY') { | ||
| 116 | $self->{DATA} = $args[0]; | ||
| 117 | } | ||
| 118 | else { | ||
| 119 | $self->{DATA} = \@args; | ||
| 120 | } | ||
| 121 | |||
| 122 | $self->{INDEX} = 0; | ||
| 123 | $self->{COUNT} = scalar(@{$self->{DATA}}); | ||
| 124 | |||
| 125 | return $self->{COUNT}; | ||
| 126 | } | ||
| 127 | |||
| 128 | =head2 sort | ||
| 129 | |||
| 130 | Sorts records of the iterator. | ||
| 131 | |||
| 132 | Parameters are: | ||
| 133 | |||
| 134 | =over 4 | ||
| 135 | |||
| 136 | =item $sort | ||
| 137 | |||
| 138 | Field used for sorting. | ||
| 139 | |||
| 140 | =item $unique | ||
| 141 | |||
| 142 | Whether results should be unique (optional). | ||
| 143 | |||
| 144 | =back | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | sub sort { | ||
| 149 | my ($self, $sort, $unique) = @_; | ||
| 150 | my (@data, @tmp); | ||
| 151 | |||
| 152 | @data = sort {lc($a->{$sort}) cmp lc($b->{$sort})} @{$self->{DATA}}; | ||
| 153 | |||
| 154 | if ($unique) { | ||
| 155 | my $sort_value = ''; | ||
| 156 | |||
| 157 | for my $record (@data) { | ||
| 158 | next if $record->{$sort} eq $sort_value; | ||
| 159 | $sort_value = $record->{$sort}; | ||
| 160 | push (@tmp, $record); | ||
| 161 | } | ||
| 162 | |||
| 163 | $self->{DATA} = \@tmp; | ||
| 164 | } | ||
| 165 | else { | ||
| 166 | $self->{DATA} = \@data; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | =head1 AUTHOR | ||
| 171 | |||
| 172 | Stefan Hornburg (Racke), <racke@linuxia.de> | ||
| 173 | |||
| 174 | =head1 LICENSE AND COPYRIGHT | ||
| 175 | |||
| 176 | Copyright 2010-2019 Stefan Hornburg (Racke) <racke@linuxia.de>. | ||
| 177 | |||
| 178 | This program is free software; you can redistribute it and/or modify it | ||
| 179 | under the terms of either: the GNU General Public License as published | ||
| 180 | by the Free Software Foundation; or the Artistic License. | ||
| 181 | |||
| 182 | See http://dev.perl.org/licenses/ for more information. | ||
| 183 | |||
| 184 | =head1 SEE ALSO | ||
| 185 | |||
| 186 | L<Template::Flute::Iterator::JSON> | ||
| 187 | |||
| 188 | =cut | ||
| 189 | |||
| 190 | 1; |