@@ -, +, @@ resultant objects on an as needed basis --- Koha/Objects.pm | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) --- a/Koha/Objects.pm +++ a/Koha/Objects.pm @@ -54,6 +54,8 @@ sub new { my ($class) = @_; my $self = {}; + $self->{_iterator} = 0; + bless( $self, $class ); } @@ -67,6 +69,8 @@ sub _new_from_dbic { my ( $class, $resultset ) = @_; my $self = { _resultset => $resultset }; + $self->{_iterator} = 0; + bless( $self, $class ); } @@ -149,14 +153,27 @@ Returns undef if there are no more objects to return. sub next { my ( $self ) = @_; - my $result = $self->_resultset()->next(); - return unless $result; + my $object = $self->_objects()->[ $self->{_iterator}++ ]; - my $object = $self->object_class()->_new_from_dbic( $result ); + $self->reset() unless $object; return $object; } +=head3 Koha::Objects->first(); + +my $object = Koha::Objects->first(); + +Returns the first object that is part of this set. + +=cut + +sub first { + my ( $self ) = @_; + + return $self->_objects()->[0]; +} + =head3 Koha::Objects->reset(); Koha::Objects->reset(); @@ -169,7 +186,7 @@ with the first object in a set. sub reset { my ( $self ) = @_; - $self->_resultset()->reset(); + $self->{_iterator} = 0; return $self; } @@ -185,11 +202,23 @@ Returns an arrayref of the objects in this set. sub as_list { my ( $self ) = @_; - my @dbic_rows = $self->_resultset()->all(); + my $objects = $self->_objects(); + + return wantarray ? @$objects : $objects; +} + +=head3 Koha::Objects->_objects + +Returns an arrayref of all the objects that are part of this Objects set + +=cut + +sub _objects { + my ( $self ) = @_; - my @objects = $self->_wrap(@dbic_rows); + $self->{_objects} ||= $self->_wrap( $self->_resultset()->all() ); - return wantarray ? @objects : \@objects; + return $self->{_objects}; } =head3 Koha::Objects->unblessed @@ -215,7 +244,7 @@ sub _wrap { my @objects = map { $self->object_class()->_new_from_dbic( $_ ) } @dbic_rows; - return @objects; + return wantarray ? @objects : \@objects;; } =head3 Koha::Objects->_resultset --