Skip to content
This repository has been archived by the owner on Apr 26, 2019. It is now read-only.

Commit

Permalink
Handle redis-rb 3.0 API for zrevrange with scores.
Browse files Browse the repository at this point in the history
  • Loading branch information
oggy committed Jul 19, 2012
1 parent 38b3bbe commit 855b0d0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
4 changes: 4 additions & 0 deletions lib/redness/red.rb
Expand Up @@ -7,6 +7,10 @@ def self.redis
$redis
end

def self.client_version
@version ||= Redis::VERSION.scan(/\d+/).map { |s| s.to_i }
end

def self.delete(key)
redis.del(key)
end
Expand Down
10 changes: 8 additions & 2 deletions lib/redness/red_set.rb
Expand Up @@ -45,8 +45,14 @@ def self.get(key, options = {})
results = redis.zrevrange(key, lower_bound, upper_bound, :with_scores => with_scores)
if with_scores
[].tap do |memo|
results.each_slice(2) do |slice|
memo << [slice[0].to_i, scoring.call(slice[1].to_i)]
if Red.client_version.first < 3
results.each_slice(2) do |slice|
memo << [slice[0].to_i, scoring.call(slice[1].to_i)]
end
else
results.each do |member, score|
memo << [member.to_i, scoring.call(score)]
end
end
end
else
Expand Down
30 changes: 22 additions & 8 deletions spec/redness/red_set_spec.rb
Expand Up @@ -98,14 +98,28 @@
end

describe ".get" do
describe "pagination" do
it "should return results bounded by provided options" do
RedSet.add("somekey", 1)
RedSet.add("somekey", 2)

RedSet.get("somekey", :lower => 0, :upper => 0).should == [2]
RedSet.get("somekey", :lower => 1, :upper => 1).should == [1]
end
it "should return the elements in reverse-inserted order" do
RedSet.add('somekey', 1)
RedSet.add('somekey', 2)
RedSet.add('somekey', 3)

RedSet.get('somekey').should == [3, 2, 1]
end

it "should pair the elements with their scores if :with_scores is true" do
RedSet.add('somekey', 1)
RedSet.add('somekey', 2)
RedSet.add('somekey', 3)

RedSet.get('somekey', :with_scores => true).should == [[3, 2], [2, 1], [1, 0]]
end

it "should return results bounded by provided pagination options" do
RedSet.add("somekey", 1)
RedSet.add("somekey", 2)

RedSet.get("somekey", :lower => 0, :upper => 0).should == [2]
RedSet.get("somekey", :lower => 1, :upper => 1).should == [1]
end
end

Expand Down

0 comments on commit 855b0d0

Please sign in to comment.