| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | package Template::Pure::Iterator; | ||
| 5 | |||
| 6 | ## Please not everything here is internal code... it will likely change a | ||
| 7 | ## lot so please rely only on the public interface. | ||
| 8 | |||
| 9 | use Scalar::Util 'blessed'; | ||
| 10 | |||
| 11 | sub from_proto { | ||
| 12 | my ($class, $proto, $sort_cb, $filter_cb, $options) = @_; | ||
| 13 | if(blessed $proto) { | ||
| 14 | return $class->from_object($proto, $sort_cb, $filter_cb, $options); | ||
| 15 | } else { | ||
| 16 | my $type = 'from_' .lc ref $proto; | ||
| 17 | return $class->$type($proto, $sort_cb, $filter_cb, $options); | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | sub from_object { | ||
| 22 | my ($class, $obj, $sort_cb, $filter_cb, $options) = @_; | ||
| 23 | my ($index, $current) = (0); | ||
| 24 | |||
| 25 | if( | ||
| 26 | (my $next = $obj->can('next')) && | ||
| 27 | (my $all = $obj->can('all')) && | ||
| 28 | (my $reset = $obj->can('reset')) && | ||
| 29 | (my $count = $obj->can('count')) | ||
| 30 | ) { | ||
| 31 | |||
| 32 | $obj = $filter_cb->($obj) if defined $filter_cb; | ||
| 33 | $obj = $sort_cb->($obj) if defined $sort_cb; | ||
| 34 | |||
| 35 | return bless +{ | ||
| 36 | _index => sub { return $index }, | ||
| 37 | _current_value => sub { return $current }, | ||
| 38 | _max_index => sub { return $count->() - 1 }, | ||
| 39 | _count => sub { return $count->() }, | ||
| 40 | _next => sub { | ||
| 41 | if(my $next = $next->()) { | ||
| 42 | $current = $next; | ||
| 43 | $index++; | ||
| 44 | return $next; | ||
| 45 | } else { | ||
| 46 | return undef; | ||
| 47 | } | ||
| 48 | }, | ||
| 49 | _reset => sub { $reset->() }, | ||
| 50 | _all => sub { return $all->() }, | ||
| 51 | _is_first => sub { return (($index - 1) == 0 ? 1:0) }, | ||
| 52 | _is_last => sub { return $index == $count->() ? 1:0 }, | ||
| 53 | _is_even => sub { return $index % 2 ? 0:1 }, | ||
| 54 | _is_odd => sub { return $index % 2 ? 1:0 }, | ||
| 55 | }, $class; | ||
| 56 | } else { | ||
| 57 | my %hash; | ||
| 58 | if(my $fields = $obj->can($options->{fields_method} ||'display_fields')) { | ||
| 59 | %hash = map { $_ => $obj->$_ } ($fields->()); | ||
| 60 | } else { | ||
| 61 | %hash = %{$obj}; | ||
| 62 | } | ||
| 63 | |||
| 64 | return $class->from_hash(\%hash, $sort_cb, $filter_cb); | ||
| 65 | } | ||
| 66 | |||
| 67 | } | ||
| 68 | |||
| 69 | sub from_hash { | ||
| 70 | my ($class, $hashref, $sort_cb, $filter_cb) = @_; | ||
| 71 | my @keys = defined $filter_cb ? grep { $filter_cb->($_, $hashref->{$_}) ? $_ : undef } keys %$hashref : keys %$hashref; | ||
| 72 | |||
| 73 | if(defined $sort_cb) { | ||
| 74 | @keys = sort { $sort_cb->($hashref, $a, $b) } @keys; | ||
| 75 | } | ||
| 76 | |||
| 77 | my $index = 0; | ||
| 78 | my $current; | ||
| 79 | my $current_key = $hashref->{$keys[0]}; | ||
| 80 | return bless +{ | ||
| 81 | _index => sub { return $current_key }, | ||
| 82 | _current_value => sub { return $current }, | ||
| 83 | _max_index => sub { return undef; }, | ||
| 84 | _count => sub { return scalar @keys }, | ||
| 85 | _next => sub { | ||
| 86 | return undef if $index > $#keys; | ||
| 87 | $current_key = $keys[$index]; | ||
| 88 | my $value = $hashref->{$current_key}; | ||
| 89 | $index++; | ||
| 90 | $current = $value; | ||
| 91 | return $value; | ||
| 92 | }, | ||
| 93 | _reset => sub { $index = 0 }, | ||
| 94 | _all => sub { return %{$hashref} }, | ||
| 95 | _is_first => sub { return $index-1 == 0 ? 1:0 }, | ||
| 96 | _is_last => sub { return $index-1 == $#keys ? 1:0 }, | ||
| 97 | _is_even => sub { return $index % 2 ? 0:1 }, | ||
| 98 | _is_odd => sub { return $index % 2 ? 1:0 }, | ||
| 99 | }, $class; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub from_array { | ||
| 103 | my ($class, $arrayref, $sort_cb, $filter_cb) = @_; | ||
| 104 | my @array = defined $filter_cb ? grep { $filter_cb->($_) } @$arrayref : @$arrayref; | ||
| 105 | |||
| 106 | if(defined $sort_cb) { | ||
| 107 | @array = sort { $sort_cb->($arrayref, $a, $b) } @array; | ||
| 108 | } | ||
| 109 | |||
| 110 | my $index = 0; | ||
| 111 | my $current; | ||
| 112 | return bless +{ | ||
| 113 | _index => sub { return $index }, | ||
| 114 | _current_value => sub { return $current }, | ||
| 115 | _max_index => sub { return $#array }, | ||
| 116 | _count => sub { return scalar @array }, | ||
| 117 | _next => sub { | ||
| 118 | return undef if $index > $#array; | ||
| 119 | my $value = $array[$index]; | ||
| 120 | $index++; | ||
| 121 | $current = $value; | ||
| 122 | return $value; | ||
| 123 | }, | ||
| 124 | _reset => sub { $index = 0 }, | ||
| 125 | _all => sub { return @array }, | ||
| 126 | _is_first => sub { return $index-1 == 0 ? 1:0 }, | ||
| 127 | _is_last => sub { return $index-1 == $#array ? 1:0 }, | ||
| 128 | _is_even => sub { return $index % 2 ? 0:1 }, | ||
| 129 | _is_odd => sub { return $index % 2 ? 1:0 }, | ||
| 130 | }, $class; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub current_value { | ||
| 134 | my ($self) = @_; | ||
| 135 | return $self->{_current_value}->($self); | ||
| 136 | } | ||
| 137 | |||
| 138 | sub next { | ||
| 139 | my ($self) = @_; | ||
| 140 | return $self->{_next}->($self); | ||
| 141 | } | ||
| 142 | |||
| 143 | sub reset { | ||
| 144 | my ($self) = @_; | ||
| 145 | return $self->{_reset}->($self); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub all { | ||
| 149 | my ($self) = @_; | ||
| 150 | return $self->{_all}->($self); | ||
| 151 | } | ||
| 152 | |||
| 153 | sub count { | ||
| 154 | my ($self) = @_; | ||
| 155 | return $self->{_count}->($self); | ||
| 156 | } | ||
| 157 | |||
| 158 | sub index { | ||
| 159 | my ($self) = @_; | ||
| 160 | return $self->{_index}->($self); | ||
| 161 | } | ||
| 162 | |||
| 163 | sub max_index { | ||
| 164 | my ($self) = @_; | ||
| 165 | return $self->{_max_index}->($self); | ||
| 166 | } | ||
| 167 | |||
| 168 | sub is_first { $_[0]->{_is_first}->($_[0]) } | ||
| 169 | sub is_last { $_[0]->{_is_last}->($_[0]) } | ||
| 170 | sub is_even { $_[0]->{_is_even}->($_[0]) } | ||
| 171 | sub is_odd { $_[0]->{_is_odd}->($_[0]) } | ||
| 172 | |||
| 173 | sub is_paged { } | ||
| 174 | |||
| 175 | sub pager { } | ||
| 176 | |||
| 177 | sub page { } | ||
| 178 | |||
| 179 | sub is_ordered { } | ||
| 180 | |||
| 181 | 1; |