Skip to content

Commit

Permalink
fix peek for list or scalar context
Browse files Browse the repository at this point in the history
  • Loading branch information
xdg committed Apr 16, 2013
1 parent b6759af commit bf832a2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 6 additions & 3 deletions lib/MongoDBx/Queue.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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<search>. 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
Expand Down
18 changes: 12 additions & 6 deletions t/search.t
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit bf832a2

Please sign in to comment.