File Coverage

blib/lib/Fey/Object/Iterator/FromArray.pm
Criterion Covered Total %
statement 21 25 84.0
branch n/a
condition n/a
subroutine 7 9 77.7
pod 1 1 100.0
total 29 35 82.8


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