[jifty-devel] Re: [Jifty-commit] r3472 - in Jifty-DBI/trunk: .
lib/Jifty/DBI
Jesse Vincent
jesse at bestpractical.com
Wed Jun 13 23:45:34 EDT 2007
Cool!
Can you perhaps unify the logic in peek and next to reduce code
duplication?
On Jun 13, 2007, at 11:35 PM, jifty-commit at lists.jifty.org wrote:
> Author: sartak
> Date: Wed Jun 13 23:35:28 2007
> New Revision: 3472
>
> Modified:
> Jifty-DBI/trunk/ (props changed)
> Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm
> Jifty-DBI/trunk/t/01searches.t
>
> Log:
> r20651 at caladan: sartak | 2007-06-13 23:34:17 -0400
> Add a peek to Jifty::DBI::Collection which returns the next item
> but doesn't
> update any internal state.
>
>
> Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm
> ======================================================================
> ========
> --- Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm (original)
> +++ Jifty-DBI/trunk/lib/Jifty/DBI/Collection.pm Wed Jun 13 23:35:28
> 2007
> @@ -662,13 +662,37 @@
>
> $self->_do_search() if $self->{'must_redo_search'};
>
> + my $item = $self->peek;
> +
> if ( $self->{'itemscount'} < $self->_record_count )
> - { #return the next item
> - my $item = ( $self->{'items'}[ $self->{'itemscount'} ] );
> + {
> $self->{'itemscount'}++;
> - return ($item);
> } else { #we've gone through the whole list. reset the count.
> $self->goto_first_item();
> + }
> +
> + return ($item);
> +}
> +
> +=head2 peek
> +
> +Exactly the same as next, only it doesn't move the iterator.
> +
> +=cut
> +
> +sub peek {
> + my $self = shift;
> + my @row;
> +
> + return (undef) unless ( $self->_is_limited );
> +
> + $self->_do_search() if $self->{'must_redo_search'};
> +
> + if ( $self->{'itemscount'} < $self->_record_count )
> + { #return the next item
> + my $item = ( $self->{'items'}[ $self->{'itemscount'} ] );
> + return ($item);
> + } else { #no more items!
> return (undef);
> }
> }
>
> Modified: Jifty-DBI/trunk/t/01searches.t
> ======================================================================
> ========
> --- Jifty-DBI/trunk/t/01searches.t (original)
> +++ Jifty-DBI/trunk/t/01searches.t Wed Jun 13 23:35:28 2007
> @@ -8,7 +8,7 @@
> BEGIN { require "t/utils.pl" }
> our (@available_drivers);
>
> -use constant TESTS_PER_DRIVER => 78;
> +use constant TESTS_PER_DRIVER => 82;
>
> my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
> plan tests => $total;
> @@ -45,6 +45,7 @@
> is( $users_obj->last, undef, 'last returns undef on not
> limited obj' );
> is( $users_obj->is_last, undef, 'is_last returns undef on
> not limited obj after last' );
> $users_obj->goto_first_item;
> + is( $users_obj->peek, undef, 'peek returns undef on not
> limited obj' );
> is( $users_obj->next, undef, 'next returns undef on not
> limited obj' );
> is( $users_obj->is_last, undef, 'is_last returns undef on
> not limited obj after next' );
> # XXX TODO FIXME: may be this methods should be implemented
> @@ -60,6 +61,7 @@
> isa_ok( $users_obj->first, 'Jifty::DBI::Record', 'first
> returns record object' );
> isa_ok( $users_obj->last, 'Jifty::DBI::Record', 'last
> returns record object' );
> $users_obj->goto_first_item;
> + isa_ok( $users_obj->peek, 'Jifty::DBI::Record', 'peek
> returns record object' );
> isa_ok( $users_obj->next, 'Jifty::DBI::Record', 'next
> returns record object' );
> $items_ref = $users_obj->items_array_ref;
> isa_ok( $items_ref, 'ARRAY', 'items_array_ref always
> returns array reference' );
> @@ -91,9 +93,12 @@
> is( $last_rec, $first_rec, 'last returns same object as
> first' );
> is( $users_obj->is_last, 1, 'is_last always returns 1
> after last call');
> $users_obj->goto_first_item;
> + my $peek_rec = $users_obj->peek;
> my $next_rec = $users_obj->next;
> + is( $next_rec, $peek_rec, 'peek returns same object as
> next' );
> is( $next_rec, $first_rec, 'next returns same object as
> first' );
> is( $users_obj->is_last, 1, 'is_last returns 1 after fetch
> first record with next method');
> + is( $users_obj->peek, undef, 'only one record in the
> collection' );
> is( $users_obj->next, undef, 'only one record in the
> collection' );
> TODO: {
> local $TODO = 'require discussion';
> _______________________________________________
> Jifty-commit mailing list
> Jifty-commit at lists.jifty.org
> http://lists.jifty.org/cgi-bin/mailman/listinfo/jifty-commit
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 186 bytes
Desc: This is a digitally signed message part
Url : http://lists.bestpractical.com/pipermail/jifty-devel/attachments/20070613/07059442/PGP.pgp
More information about the jifty-devel
mailing list