Fey::Object::Iterator::FromArray - An iterator which iterates over an array of objects


Fey-ORM documentation Contained in the Fey-ORM distribution.

Index


Code Index:

NAME

Top

Fey::Object::Iterator::FromArray - An iterator which iterates over an array of objects

VERSION

Top

version 0.43

SYNOPSIS

Top

  use Fey::Object::Iterator::FromArray;

  my $iter = Fey::Object::Iterator::FromArray->new(
      classes => 'MyApp::User',
      objects => \@users,
  );

  my $iter2 = Fey::Object::Iterator::FromArray->new(
      classes => [ 'MyApp::User', 'MyApp::Group' ],
      objects => [ [ $user1, $group1 ], [ $user2, $group1 ] ],
  );

  print $iter->index();    # 0

  while ( my $user = $iter->next() ) {
      print $iter->index();    # 1, 2, 3, ...
      print $user->username();
  }

  # will return cached objects now
  $iter->reset();

DESCRIPTION

Top

This class provides an object which does the Fey::ORM::Role::Iterator role, but gets its data from an array reference. This lets you provide a single API that accepts data from Fey::ORM-created iterators, or existing data sets.

METHODS

Top

This class provides the following methods:

$iterator->new()

The constructor requires two parameters, classes and objects. The classes parameter can be a single class name, or an array reference of names.

The objects parameter should be an array reference. That reference can contain a list of objects, or an a list of array references, each of which contains objects.

In either case, the objects must be subclasses of Fey::Object::Table.

$iterator->reset()

Resets the iterator so that the next call to $iterator->next() returns the first object(s).

ROLES

Top

This class does the Fey::ORM::Role::Iterator role.

AUTHOR

Top

Dave Rolsky <autarch@urth.org>

COPYRIGHT AND LICENSE

Top


Fey-ORM documentation Contained in the Fey-ORM distribution.

package Fey::Object::Iterator::FromArray;
BEGIN {
  $Fey::Object::Iterator::FromArray::VERSION = '0.43';
}

use strict;
use warnings;
use namespace::autoclean;

use Fey::ORM::Types qw( IterableArrayRef );

use Moose;
use MooseX::SemiAffordanceAccessor;
use MooseX::StrictConstructor;

with 'Fey::ORM::Role::Iterator';

has '_objects' => (
    is       => 'ro',
    isa      => IterableArrayRef,
    coerce   => 1,
    required => 1,
    init_arg => 'objects',
);

sub _get_next_result {
    my $self = shift;

    return $self->_objects()->[ $self->index() ];
}

sub reset {
    my $self = shift;

    $self->_reset_index();
}

__PACKAGE__->meta()->make_immutable();

1;

# ABSTRACT: An iterator which iterates over an array of objects




__END__