| Handel documentation | Contained in the Handel distribution. |
Handel::Iterator::DBIC - Iterator class used for collection looping DBIC resultsets
my $resultset = $schema->resultset('Carts')->search;
my $iterator = Handel::Iterator::DBIC->new({
data => $resultset,
result_class => 'MyResult',
storage => $storage
});
while (my $result = $iterator->next) {
print $result->method;
};
Handel::Iterator::DBIC is a used to iterate through results stored in a resultset returned from DBIx::Class.
Creates a new iterator object. The following options are available:
my $resultset = $schema->resultset('Carts')->search;
my $iterator = Handel::Iterator::DBIC->new({
data => $resultset,
result_class => 'MyResult',
storage => $storage
});
my $result = $iterator->first;
print ref $result; # MyResult
The data to be iterated through. This should be a DBIx::Class::ResultSet.
The name of the class that each result should be inflated into.
The storage object that was used to create the results.
Returns all results from current iterator.
foreach my $result ($iterator->all) {
print $result->method;
};
Returns the number of results in the current iterator.
my $count = $iterator->count;
Returns a new result class object based on the specified result and storage
objects. If no storage object is specified, the storage object passed to new
will be used instead.
This method is used by methods like first and next to to create storage
result objects. There is probably no good reason to use this method directly.
Returns the first result or undef if there are no results.
my $first = $iterator->first;
Returns the last result or undef if there are no results.
my $last = $iterator->last;
Returns the next result or undef if there are no results.
while (my $result = $iterator->next) {
print $result->method;
};
Resets the current result position back to the first result.
while (my $result = $iterator->next) {
print $result->method;
};
$iterator->reset;
while (my $result = $iterator->next) {
print $result->method;
};
Christopher H. Laco
CPAN ID: CLACO
claco@chrislaco.com
http://today.icantfocus.com/blog/
| Handel documentation | Contained in the Handel distribution. |
# $Id$ ## no critic (ProhibitAmbiguousNames) package Handel::Iterator::DBIC; use strict; use warnings; use overload '0+' => \&count, 'bool' => \&count, '==' => \&count, fallback => 1; BEGIN { use base qw/Handel::Iterator/; use Handel::L10N qw/translate/; use Scalar::Util qw/blessed/; }; sub new { my $class = shift; no strict 'refs'; my $resultset = $_[0]->{'data'}; throw Handel::Exception::Argument( -details => translate('ITERATOR_DATA_NOT_RESULTSET') ) unless blessed($resultset) && $resultset->isa('DBIx::Class::ResultSet'); ## no critic return $class->SUPER::new(@_); }; sub all { my $self = shift; return map {$self->create_result($_)} $self->data->all; }; sub count { return shift->data->count; }; sub first { my $self = shift; my $result = $self->data->slice(0, 0)->first; return $result ? $self->create_result($result) : undef; }; sub last { my $self = shift; my $last = $self->count - 1; my $result = $self->data->slice($last, $last)->first; return $result ? $self->create_result($result) : undef; }; sub next { my $self = shift; my $result = $self->data->next; return $result ? $self->create_result($result) : undef; }; sub reset { return shift->data->reset; }; 1; __END__