From bf832a2e09c4fdec33b44cf448e9d42a8fb298dd Mon Sep 17 00:00:00 2001 From: David Golden Date: Tue, 16 Apr 2013 17:05:31 -0400 Subject: [PATCH] fix peek for list or scalar context --- Changes | 4 ++++ lib/MongoDBx/Queue.pm | 9 ++++++--- t/search.t | 18 ++++++++++++------ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/Changes b/Changes index 64871cb..00c7314 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,9 @@ Revision history for MongoDBx-Queue + [FIXED] + + - Have peek() do the right thing for list or scalar return context + {{$NEXT}} 0.003 2013-03-28 18:34:49 America/New_York diff --git a/lib/MongoDBx/Queue.pm b/lib/MongoDBx/Queue.pm index f05b03c..a524cc3 100644 --- a/lib/MongoDBx/Queue.pm +++ b/lib/MongoDBx/Queue.pm @@ -257,18 +257,21 @@ sub search { =method peek - $queue->peek( $task ); + $task = $queue->peek( $task ); -Retrieves a copy of the task from the queue. This is useful to retrieve all +Retrieves a full copy of the task from the queue. This is useful to retrieve all fields from a partial-field result from C. It is equivalent to: $self->search( { _id => $task->{_id} } ); +Returns undef if the task is not found. + =cut sub peek { my ( $self, $task ) = @_; - return $self->search( { $ID => $task->{$ID} } ); + my @result = $self->search( { $ID => $task->{$ID} } ); + return wantarray ? @result : $result[0]; } =method size diff --git a/t/search.t b/t/search.t index 8fc507b..8019042 100644 --- a/t/search.t +++ b/t/search.t @@ -78,14 +78,20 @@ is( $found[0]{first}, 'John', "got first requested field" ); is( $found[0]{tel}, '555-123-4567', "got next requested field" ); is( $found[0]{last}, 'Smith', "got last requested field" ); -@found = $queue->peek( $found[0] ); -is( scalar @found, 1, "got correct number from peek on task" ); -is( $found[0]{first}, 'John', "got first field" ); -is( $found[0]{tel}, '555-123-4567', "got next field" ); -is( $found[0]{last}, 'Smith', "got last field" ); +my $peek = $queue->peek( $found[0] ); +ok( $peek, "peek found result" ); +is( $peek->{first}, 'John', "got first field" ); +is( $peek->{tel}, '555-123-4567', "got next field" ); +is( $peek->{last}, 'Smith', "got last field" ); + +$peek = $queue->peek( { _id => '123456789' } ); +is( $peek, undef, "peek unknown returns undef in scalar context" ); + +my @empty = $queue->peek( { _id => '123456789' } ); +is( scalar @empty, 0, "peek unknown returns empty list in list context" ); @found = $queue->search( { last => "Doe" }, { limit => 1 } ); -is( scalar @found, 1, "got correct number from search limited to 1 result" ); +is( scalar @found, 1, "got correct number from search limited to 1 result" ); done_testing;