| 1 | package Template::Flute::Iterator::Cache; | ||
| 2 | |||
| 3 | use Moo; | ||
| 4 | use Types::Standard qw/ArrayRef InstanceOf Int/; | ||
| 5 | |||
| 6 | use base 'Template::Flute::Iterator'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Template::Flute::Iterator::Cache - Iterator caching class | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | This iterator is used for caching another iterator which is used multiple | ||
| 15 | times in a list. We can safely use reset method on the caching iterator, | ||
| 16 | but not always on the original iterator. | ||
| 17 | |||
| 18 | =head1 ATTRIBUTES | ||
| 19 | |||
| 20 | =head2 iterator | ||
| 21 | |||
| 22 | Original iterator (required). | ||
| 23 | |||
| 24 | =head2 index | ||
| 25 | |||
| 26 | Current position. | ||
| 27 | |||
| 28 | =head2 data | ||
| 29 | |||
| 30 | Cached data. | ||
| 31 | |||
| 32 | =head2 filled | ||
| 33 | |||
| 34 | Whether cache is filled or not. | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | has iterator => ( | ||
| 39 | isa => InstanceOf ['Template::Flute::Iterator'], | ||
| 40 | required => 1, | ||
| 41 | is => 'ro', | ||
| 42 | ); | ||
| 43 | |||
| 44 | has index => ( | ||
| 45 | isa => Int, | ||
| 46 | default => 0, | ||
| 47 | is => 'rwp', | ||
| 48 | ); | ||
| 49 | |||
| 50 | has data => ( | ||
| 51 | isa => ArrayRef, | ||
| 52 | is => 'rwp', | ||
| 53 | default => sub {[]}, | ||
| 54 | ); | ||
| 55 | |||
| 56 | has filled => ( | ||
| 57 | is => 'rwp', | ||
| 58 | default => 0, | ||
| 59 | ); | ||
| 60 | |||
| 61 | =head1 METHODS | ||
| 62 | |||
| 63 | =head2 count | ||
| 64 | |||
| 65 | Returns count of (original) iterator. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | sub count { | ||
| 70 | return $_[0]->iterator->count; | ||
| 71 | } | ||
| 72 | |||
| 73 | =head2 next | ||
| 74 | |||
| 75 | Returns next record, either from original iterator or our cache. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub next { | ||
| 80 | my ($self, $index, $record); | ||
| 81 | |||
| 82 | $self = shift; | ||
| 83 | $index = $self->index; | ||
| 84 | |||
| 85 | if ($self->filled) { | ||
| 86 | # grab record from cache | ||
| 87 | if ($index < $self->count) { | ||
| 88 | $self->_set_index($index + 1); | ||
| 89 | return $self->data->[$index]; | ||
| 90 | } | ||
| 91 | return undef; | ||
| 92 | } | ||
| 93 | |||
| 94 | # grab record from original iterator and store it | ||
| 95 | if ($record = $self->iterator->next) { | ||
| 96 | push @{$self->data}, $record; | ||
| 97 | $self->_set_index($index + 1); | ||
| 98 | return $record; | ||
| 99 | } | ||
| 100 | |||
| 101 | $self->_set_filled(1); | ||
| 102 | return undef; | ||
| 103 | } | ||
| 104 | |||
| 105 | =head2 reset | ||
| 106 | |||
| 107 | Resets the iterator. | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub reset { | ||
| 112 | my $self = shift; | ||
| 113 | |||
| 114 | $self->_set_index(0); | ||
| 115 | } | ||
| 116 | |||
| 117 | =head1 AUTHOR | ||
| 118 | |||
| 119 | Stefan Hornburg (Racke), <racke@linuxia.de> | ||
| 120 | |||
| 121 | =head1 LICENSE AND COPYRIGHT | ||
| 122 | |||
| 123 | Copyright 2014-2019 Stefan Hornburg (Racke) <racke@linuxia.de>. | ||
| 124 | |||
| 125 | This program is free software; you can redistribute it and/or modify it | ||
| 126 | under the terms of either: the GNU General Public License as published | ||
| 127 | by the Free Software Foundation; or the Artistic License. | ||
| 128 | |||
| 129 | See http://dev.perl.org/licenses/ for more information. | ||
| 130 | |||
| 131 | =cut | ||
| 132 | |||
| 133 | 1; |