Fey::ORM::Role::Iterator - A role for things that iterate over Fey::Object::Table objects


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

Index


Code Index:

NAME

Top

Fey::ORM::Role::Iterator - A role for things that iterate over Fey::Object::Table objects

VERSION

Top

version 0.43

SYNOPSIS

Top

  package My::Iterator;

  use Moose;

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

DESCRIPTION

Top

This role provides some common methods used by Fey::Object::Iterator classes, as well as defining a consistent interface for iterators.

REQUIRED METHODS

Top

Classes which consume this role must provide _get_next_result() and reset() methods.

PROVIDED ATTRIBUTES

Top

This role provides the following attributes.

$iterator->classes()

An array reference of class names. Each class must be a subclass of Fey::Object::Table.

$iterator->index()

The current iterator index. Also provides _inc_index() and _reset_index() methods.

PROVIDED METHODS

Top

This role provides the following methods. These methods are documented in Fey::Object::Iterator::FromSelect.

$iterator->next

$iterator->next_as_hash()

$iterator->all()

$iterator->all_as_hashes()

$iterator->remaining()

$iterator->remaining_as_hashes()

AUTHOR

Top

Dave Rolsky <autarch@urth.org>

COPYRIGHT AND LICENSE

Top


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

package Fey::ORM::Role::Iterator;
BEGIN {
  $Fey::ORM::Role::Iterator::VERSION = '0.43';
}

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

use Fey::ORM::Types qw( ArrayRefOfClasses Bool Int );
use List::AllUtils qw( pairwise );

use Moose::Role;

requires qw( _get_next_result reset );

has classes => (
    is       => 'ro',
    isa      => ArrayRefOfClasses,
    coerce   => 1,
    required => 1,
);

has index => (
    traits   => ['Counter'],
    is       => 'ro',
    isa      => Int,
    default  => 0,
    init_arg => undef,
    handles  => {
        _inc_index   => 'inc',
        _reset_index => 'reset',
    },
);

has _can_make_hashes => (
    is       => 'ro',
    isa      => Bool,
    lazy     => 1,
    init_arg => undef,
    default  => sub {
        List::AllUtils::all { $_->can('Table') } @{ $_[0]->classes() };
    },
);

sub next {
    my $self = shift;

    my $result = $self->_get_next_result();

    return unless $result;

    $self->_inc_index();

    return wantarray ? @{$result} : $result->[0];
}

sub next_as_hash {
    my $self = shift;

    die 'Cannot make a hash unless all classes have a Table() method'
        unless $self->_can_make_hashes();

    my @result = $self->next();

    return unless @result;

    return pairwise { $a->Table()->name() => $b } @{ $self->classes() },
        @result;
}

sub all {
    my $self = shift;

    $self->reset() if $self->index();

    return $self->remaining();
}

sub all_as_hashes {
    my $self = shift;

    $self->reset() if $self->index();

    return $self->remaining_as_hashes();
}

sub remaining {
    my $self = shift;

    my @result;
    while ( my @r = $self->next() ) {
        push @result, @r == 1 ? @r : \@r;
    }

    return @result;
}

sub remaining_as_hashes {
    my $self = shift;

    my @result;
    while ( my %r = $self->next_as_hash() ) {
        push @result, \%r;
    }

    return @result;
}

1;

# ABSTRACT: A role for things that iterate over Fey::Object::Table objects




__END__