Rose::DB::Object::Iterator - Iterate over a series of Rose::DB::Objects.


Rose-DB-Object documentation Contained in the Rose-DB-Object distribution.

Index


Code Index:

NAME

Top

Rose::DB::Object::Iterator - Iterate over a series of Rose::DB::Objects.

SYNOPSIS

Top

    $iterator = Rose::DB::Object::Manager->get_objects_iterator(...);

    while($object = $iterator->next)
    {
      # do stuff with $object...

      if(...) # bail out early
      {
        $iterator->finish;
        last;
      }
    }

    if($iterator->error)
    {
      print "There was an error: ", $iterator->error;
    }
    else
    {
      print "Total: ", $iterator->total;
    }

DESCRIPTION

Top

Rose::DB::Object::Iterator is an iterator object that traverses a database query, returning Rose::DB::Object-derived objects for each row. Rose::DB::Object::Iterator objects are created by calls to the get_objects_iterator method of Rose::DB::Object::Manager or one of its subclasses.

OBJECT METHODS

Top

error

Returns the text message associated with the last error, or false if there was no error.

finish

Prematurely stop the iteration (i.e., before iterating over all of the available objects).

next

Return the next Rose::DB::Object-derived object. Returns false (but defined) if there are no more objects to iterate over, or undef if there was an error.

total

Returns the total number of objects iterated over so far.

AUTHOR

Top

John C. Siracusa (siracusa@gmail.com)

LICENSE

Top

Copyright (c) 2010 by John C. Siracusa. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


Rose-DB-Object documentation Contained in the Rose-DB-Object distribution.

package Rose::DB::Object::Iterator;

use strict;

use Carp();

use Rose::Object;
our @ISA = qw(Rose::Object);

our $VERSION = '0.759';

use Rose::Object::MakeMethods::Generic
(
  scalar => 
  [
    'error',
    '_count',
    '_next_code',
    '_finish_code',
    '_destroy_code',
  ],

  'boolean' => 'active',
);

sub next
{
  my($self) = shift;
  my $ret = $self->_next_code->($self, @_);
  $self->active(0)  unless($ret);
  return $ret;
}

sub finish
{
  my($self) = shift;
  $self->active(0);
  $self->_next_code(sub { 0 });
  return $self->_finish_code->($self, @_);
}

sub DESTROY
{
  my($self) = shift;

  if($self->active)
  {
    $self->finish;
  }
  elsif(my $code = $self->_destroy_code)
  {
    $code->($self);
  }
}

sub total { shift->{'_count'} }

1;

__END__