These documents are For the HEAD of the CVS repository on July 19, 2007
Api docs for previous releases
Modware
Iterator
Modware::Iterator - An Iterator parent class inherited by the results returned from Modware::Search::* methods.
|
No package variables defined. |
#### The following code shows how a search class method can return an iterator ## instead of an object array. This eliminates the overhead of returning one giant array.
use Modware::Iterator;
sub Search_term_in_all_ontologies_by_name {
my ($self, $term) = @_; my $dbh = new Modware::DBH(); my @results;
my $sth = $dbh->prepare(" SELECT CVTERM_ID FROM $ENV{'CHADO_USER'}.CVTERM WHERE UPPER(NAME) LIKE UPPER(?) ");
$sth->execute( $term );
### Creating a data array. ### The data array to be passed to the ietrator is an array of anonymous hashes. ### The key for the hash is the key used in the constructor of the class ### For eg: A Term object can be instantiated by calling ### my $term = new Modware::CV::Term( -CVTERM_DBID => "12345" ) ### Hence, in this schenario the key string for the hash would be CVTERM_DBID ### Example of an array of hashes [{ CVTERM_DBID => 122}, { CVTERM_DBID => 123}]
my @data; while ( my $row = $sth->fetchrow() ) { push(@data, {-CVTERM_DBID => $row}); } $sth->finish;
### Instantiate an Iterator object with the class name and the data array as arguments. my $itr = new Modware::Iterator(-CLASS => "Modware::CV::Term", -DATA => \@data);
#### The follwoing piece of code allws the method to return an array of objects or Itetrator return wantarray ? $itr->to_array() : $itr; }
### Manipulating the iterator object
my $iterator = Modware::Search::Ontology->Search_term_in_all_ontologies_by_name("mass%");
while(my $term = $iterator->next()) { print $term->name()."\n"; } print $iterator->first->name()."\n";
my @objs = $iterator->slice(2,4);
foreach my $obj (@objs) { print $obj->name()."\n"; }
### This returns an array of objects instead of the iterator my @objects = Modware::Search::Ontology->Search_term_in_all_ontologies_by_name("mass%");
|
This class was adapted from the Class::DBI iterator class
|
Methods description
Title : class
Usage : my $itr->class("Modware::CV::Term");
Function: get/set the type of the Class
Returns : A string
Args : Class type name
Title : count
Usage : my $count = $itr->count();
Function: Gives the count of elements in the array
Returns : number
Args : none
Title : data
Usage : $itr->data( \@data);
Function: get/set the data
Returns : Reference to an array of anonymous hashes [{ CVTERM_DBID => 122}, { CVTERM_DBID => 123}]
Args : Reference to an array of anonymous hashes
Title : first
Usage : my $obj = $itr->first();
Function: Returns the first element from the iterator
Returns : First Object of type class specified in the constructor
Args : none
Title : new
Usage : my $itr = new Modware::Iterator("Modware::CV::Term", \@data);
Function: Builds a new Modware::Iterator object
Returns : an instance of Modware::Iterator
Args : -CLASS This is the class type over which the ietraor is defined
-DATA This is a reference to an array of anonymous hashes.
[{ CVTERM_DBID => 122}, { CVTERM_DBID => 123}]
Title : next
Usage : my $obj = $itr->next();
Function: Returns the next element from the iterator
Returns : Object of type class specified in the constructor
Args : none
Title : reset
Usage : my $obj = $itr->reset();
Function: Turns the pointer of the iterator to the first object. Hence, when you call next methoid
on it, it starts from the first object.
Returns : None
Args : none
Title : slice
Usage : my @objs = $iterator->slice(2,4);
Function: Returns the objects specified between the range
Returns : An object array of type class
Args : range start
range end
Title : to_array
Usage : my @objs = $itr->to_array();
Function: This returns an array of the instantiated objects
Returns : An array of INstantiated objects
Args : none
Methods code
sub class
{ shift->{_class}
}
sub count
{ my $self = shift;
$self->{_count} ||= scalar $self->data;
}
sub data
{ @{ shift->{_data} }
}
sub first
{ my $self = shift;
$self->reset;
return $self->next;
}
sub new
{ my ( $self, @args ) = @_;
my ($class, $data) = $self->_rearrange( [qw( CLASS DATA )], @args );
bless {
_class => $class,
_data => $data,
_place => 0,
}, $self;
}
sub next
{ my $self = shift;
my $use = $self->{_data}->[ $self->{_place}++ ] or return;
$self->_load_module( $self->class() );
return $self->class(%$use);
}
sub reset
{ shift->{_place} = 0
}
sub slice
{ my ($self, $start, $end) = @_;
$end ||= $start;
$self->{_place} = $start;
my @return;
while ($self->{_place} <= $end) {
push @return, $self->next || last;
}
return @return;
}
sub to_array
{my $self = shift;
my @results;
while(my $obj = $self->next()) {
push(@results, $obj);
}
return @results;
}
General documentation
Copyright © 2006, Northwestern University
All rights reserved.
|
|