File Coverage

blib/lib/Fey/Object/Iterator/FromArray.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Fey::Object::Iterator::FromArray;
2             {
3             $Fey::Object::Iterator::FromArray::VERSION = '0.46';
4             }
5              
6 1     1   22494 use strict;
  1         3  
  1         38  
7 1     1   6 use warnings;
  1         2  
  1         29  
8 1     1   3837 use namespace::autoclean;
  1         47604  
  1         8  
9              
10 1     1   1041 use Fey::ORM::Types qw( IterableArrayRef );
  0            
  0            
11              
12             use Moose;
13             use MooseX::SemiAffordanceAccessor;
14             use MooseX::StrictConstructor;
15              
16             with 'Fey::ORM::Role::Iterator';
17              
18             has '_objects' => (
19             is => 'ro',
20             isa => IterableArrayRef,
21             coerce => 1,
22             required => 1,
23             init_arg => 'objects',
24             );
25              
26             sub _get_next_result {
27             my $self = shift;
28              
29             return $self->_objects()->[ $self->index() ];
30             }
31              
32             sub reset {
33             my $self = shift;
34              
35             $self->_reset_index();
36             }
37              
38             __PACKAGE__->meta()->make_immutable();
39              
40             1;
41              
42             # ABSTRACT: An iterator which iterates over an array of objects
43              
44             __END__
45              
46             =pod
47              
48             =head1 NAME
49              
50             Fey::Object::Iterator::FromArray - An iterator which iterates over an array of objects
51              
52             =head1 VERSION
53              
54             version 0.46
55              
56             =head1 SYNOPSIS
57              
58             use Fey::Object::Iterator::FromArray;
59              
60             my $iter = Fey::Object::Iterator::FromArray->new(
61             classes => 'MyApp::User',
62             objects => \@users,
63             );
64              
65             my $iter2 = Fey::Object::Iterator::FromArray->new(
66             classes => [ 'MyApp::User', 'MyApp::Group' ],
67             objects => [ [ $user1, $group1 ], [ $user2, $group1 ] ],
68             );
69              
70             print $iter->index(); # 0
71              
72             while ( my $user = $iter->next() ) {
73             print $iter->index(); # 1, 2, 3, ...
74             print $user->username();
75             }
76              
77             # will return cached objects now
78             $iter->reset();
79              
80             =head1 DESCRIPTION
81              
82             This class provides an object which does the
83             C<Fey::ORM::Role::Iterator> role, but gets its data from an array
84             reference. This lets you provide a single API that accepts data from
85             L<Fey::ORM>-created iterators, or existing data sets.
86              
87             =head1 METHODS
88              
89             This class provides the following methods:
90              
91             =head2 $iterator->new()
92              
93             The constructor requires two parameters, C<classes> and
94             C<objects>. The C<classes> parameter can be a single class name, or an
95             array reference of names.
96              
97             The C<objects> parameter should be an array reference. That reference
98             can contain a list of objects, or an a list of array references, each
99             of which contains objects.
100              
101             In either case, the objects must be subclasses of
102             L<Fey::Object::Table>.
103              
104             =head2 $iterator->reset()
105              
106             Resets the iterator so that the next call to C<< $iterator->next() >>
107             returns the first object(s).
108              
109             =head1 ROLES
110              
111             This class does the L<Fey::ORM::Role::Iterator> role.
112              
113             =head1 AUTHOR
114              
115             Dave Rolsky <autarch@urth.org>
116              
117             =head1 COPYRIGHT AND LICENSE
118              
119             This software is copyright (c) 2011 by Dave Rolsky.
120              
121             This is free software; you can redistribute it and/or modify it under
122             the same terms as the Perl 5 programming language system itself.
123              
124             =cut