CatalystX::CRUD::Iterator - generic iterator wrapper for CXCM iterator() results


CatalystX-CRUD documentation Contained in the CatalystX-CRUD distribution.

Index


Code Index:

NAME

Top

CatalystX::CRUD::Iterator - generic iterator wrapper for CXCM iterator() results

SYNOPSIS

Top

 package MyApp::Model::MyModel;
 use CatalystX::CRUD::Iterator;
 use MyModel;
 __PACKAGE__->config->{object_class} = 'MyModel::Object';

 sub iterator {
     my ($self, $query) = @_;

     my $iterator = MyModel->search_for_something;

     # $iterator must have a next() method

     return CatalystX::CRUD::Iterator->new(
                                        $iterator,
                                        $self->object_class
                                        );
 }

DESCRIPTION

Top

CatalystX::CRUD::Iterator is a general iterator class that wraps a real iterator and blesses return results into a specified class. CatalystX::CRUD::Iterator is a glue that provides for a similar level of abstraction across all kinds of CXCM classes.

METHODS

Top

new( iterator, class_name )

Returns a CatalystX::CRUD::Iterator instance.

iterator must have a next() method and (optionally) a finish() method.

See next().

next

Calls the next() method on the internal iterator object, stashing the result in an object returned by class_name->new under the method_name accessor.

finish

If the internal iterator object has a finish() method, this will call and return it. Otherwise returns true (1).

AUTHOR

Top

Peter Karman, <perl at peknet.com>

BUGS

Top

Please report any bugs or feature requests to bug-catalystx-crud at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CatalystX-CRUD. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc CatalystX::CRUD

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/CatalystX-CRUD

* CPAN Ratings

http://cpanratings.perl.org/d/CatalystX-CRUD

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=CatalystX-CRUD

* Search CPAN

http://search.cpan.org/dist/CatalystX-CRUD

ACKNOWLEDGEMENTS

Top

COPYRIGHT & LICENSE

Top


CatalystX-CRUD documentation Contained in the CatalystX-CRUD distribution.
package CatalystX::CRUD::Iterator;
use strict;
use warnings;
use Carp;
use base qw( CatalystX::CRUD );

our $VERSION = '0.51';

# hasa a CXCM iterator() result and calls its next() method,
# wrapping the result in the Iterator's CXCO class instance

sub new {
    my $class      = shift;
    my $iterator   = shift or $class->throw_error("need an iterator object");
    my $cxco_class = shift
        or $class->throw_error("need the name of a CXCO class");

    # sanity checks
    unless ( $iterator->can('next') ) {
        $class->throw_error("iterator $iterator has no next() method");
    }

    unless ( $cxco_class->can('new') ) {
        $class->throw_error("no new() method defined for $cxco_class");
    }

    unless ( $cxco_class->isa('CatalystX::CRUD::Object') ) {
        $class->throw_error(
            "$cxco_class does not inherit from CatalystX::CRUD::Object");
    }

    return bless(
        {   iterator => $iterator,
            cxco     => $cxco_class
        },
        $class
    );
}

sub next {
    my $self = shift;
    my $next = $self->{iterator}->next;
    return unless $next;

    my $obj = $self->{cxco}->new;
    $obj->{delegate} = $next;
    return $obj;
}

sub finish {
    my $self = shift;
    if ( $self->{iterator}->can('finish') ) {
        return $self->{iterator}->finish;
    }
    return 1;
}

1;

__END__