RDF::Trine::Iterator::Bindings::Materialized - Materialized bindings class


RDF-Trine documentation Contained in the RDF-Trine distribution.

Index


Code Index:

NAME

Top

RDF::Trine::Iterator::Bindings::Materialized - Materialized bindings class

VERSION

Top

This document describes RDF::Trine::Iterator::Bindings::Materialized version 0.135

SYNOPSIS

Top

    use RDF::Trine::Iterator;

    my $iterator = RDF::Trine::Iterator::Bindings::Materialized->new( \@data, \@names );
    while (my $row = $iterator->next) {
    	my @vars	= keys %$row;
    	# do something with @vars
    }

    my $iterator = RDF::Trine::Iterator::Bindings->new( \&code, \@names );
    my $miter = $iterator->materialize;
    while (my $row = $miter->next) {
    	my @vars	= keys %$row;
    	# do something with @vars
    }
    $miter->reset; # start the iteration again
    while (my $row = $miter->next) {
        # ...
    }

METHODS

Top

Beyond the methods documented below, this class inherits methods from the RDF::Trine::Iterator::Bindings class.

new ( \@results, \@names, %args )

Returns a new materialized bindings interator. Results must be a reference to an array containing individual results.

reset

Returns the iterator to its starting position.

next

Returns the next item in the iterator.

length

Returns the number of elements in the iterator.

AUTHOR

Top

Gregory Todd Williams <gwilliams@cpan.org>

COPYRIGHT

Top


RDF-Trine documentation Contained in the RDF-Trine distribution.
# RDF::Trine::Iterator::Bindings::Materialized
# -----------------------------------------------------------------------------

package RDF::Trine::Iterator::Bindings::Materialized;

use strict;
use warnings;
no warnings 'redefine';
use Data::Dumper;
use base qw(RDF::Trine::Iterator::Bindings);

use Scalar::Util qw(blessed reftype);

our ($VERSION);
BEGIN {
	$VERSION	= '0.135';
}

sub new {
	my $class	= shift;
	my $data	= shift || [];
	my $names	= shift || [];
	Carp::confess unless (scalar(@_) % 2 == 0);
	my %args	= @_;
	
	if (reftype($data) eq 'CODE') {
		my @rows;
		while (my $row = $data->()) {
			push(@rows, $row);
		}
		$data	= \@rows;
	}
	
	Carp::confess "not an ARRAY: " . Dumper($data) unless (reftype($data) eq 'ARRAY');
	
	my $type	= 'bindings';
	my $index	= 0;
	my $stream	= sub {
		my $data	= $data->[ $index++ ];
		unless (defined($data)) {
			$index	= 0;
		}
		return $data;
	};
	my $self	= $class->SUPER::new( $stream, $names, %args );
	$self->{_data}	= $data;
	$self->{_index}	= \$index;
	
	return $self;
}

sub reset {
	my $self	= shift;
	${ $self->{_index} }	= 0;
}

sub next {
	my $self	= shift;
	my $data	= $self->SUPER::next;
	unless (defined($data)) {
		$self->{_finished}	= 0;
	}
	return $data;
}

sub length {
	my $self	= shift;
	return scalar(@{ $self->{ _data } });
}

1;

__END__