From a790b1507ac71eae844fd0a3f933e8ceacd883c8 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 17 Feb 2015 10:10:28 -0500 Subject: [PATCH] Store list of objects as needed http://bugs.koha-community.org/show_bug.cgi?id=13019 --- Koha/Objects.pm | 40 +++++++++++++++++++++++----------------- 1 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Koha/Objects.pm b/Koha/Objects.pm index a364d25..2805035 100644 --- a/Koha/Objects.pm +++ b/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 ); } @@ -138,12 +142,7 @@ 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->object_class()->_new_from_dbic( $result ); - - return $object; + return $self->_objects()->[ $self->{_iterator}++ ]; } =head3 Koha::Objects->first(); @@ -157,12 +156,7 @@ Returns the first object that is part of this set. sub first { my ( $self ) = @_; - my $result = $self->_resultset()->first(); - return unless $result; - - my $object = $self->object_class()->_new_from_dbic( $result ); - - return $object; + return $self->_objects()->[0]; } =head3 Koha::Objects->reset(); @@ -177,7 +171,7 @@ with the first object in a set. sub reset { my ( $self ) = @_; - $self->_resultset()->reset(); + $self->{_iterator} = 0; return $self; } @@ -193,11 +187,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->_wrap @@ -211,7 +217,7 @@ sub _wrap { my @objects = map { $self->object_class()->_new_from_dbic( $_ ) } @dbic_rows; - return @objects; + return wantarray ? @objects : \@objects;; } =head3 Koha::Objects->_resultset -- 1.7.2.5