Commit | Line | Data |
f2fef590 |
1 | package Reaction::UI::WidgetClass::_OVER; |
2 | |
3 | use Reaction::Class; |
4 | |
81393881 |
5 | use namespace::clean -except => [ qw(meta) ]; |
f2fef590 |
6 | |
f2fef590 |
7 | |
81393881 |
8 | has 'collection' => (is => 'ro', required => 1); |
9 | sub BUILD { |
10 | my ($self, $args) = @_; |
11 | my $coll = $args->{collection}; |
12 | unless (ref $coll eq 'ARRAY' || (blessed($coll) && $coll->can('next'))) { |
13 | confess _OVER."->new collection arg ${coll} is neither" |
14 | ." arrayref nor implements next()"; |
15 | } |
16 | }; |
17 | sub each { |
18 | my ($self, $do) = @_; |
19 | my $coll = $self->collection; |
20 | if (ref $coll eq 'ARRAY') { |
21 | foreach my $el (@$coll) { |
22 | $do->($el); |
f2fef590 |
23 | } |
81393881 |
24 | } else { |
25 | $coll->reset if $coll->can('reset'); |
26 | while (my $el = $coll->next) { |
27 | $do->($el); |
f2fef590 |
28 | } |
81393881 |
29 | } |
f2fef590 |
30 | }; |
81393881 |
31 | __PACKAGE__->meta->make_immutable; |
32 | |
f2fef590 |
33 | |
34 | 1; |