Permalink
Browse files

Merge pull request #8 from agoragames/reverse_option

Reverse option
  • Loading branch information...
David Czarnecki
David Czarnecki committed Apr 27, 2012
2 parents e5fcd73 + 0bb74a1 commit 187771b0fbad87f60a76d670b906b0a625acb069
View
@@ -1,5 +1,9 @@
# CHANGELOG
+## leaderboard 2.0.6 (2012-04-26)
+
+* Added accessor for +reverse+ option so that you can set reverse after creating a leaderboard to see results in either highest-to-lowest or lowest-to-highest order.
+
## leaderboard 2.0.5 (2012-03-14)
* Added `rank_members(members_and_scores)` and `rank_members_in(leaderboard_name, members_and_scores)` allowing you to pass in some variable number of `member_name, score` and so on or an actual array of those data items. Use this method to do bulk insert of data, but be mindful of the amount of data you are inserting since a single transaction can get quite large.
View
@@ -48,9 +48,12 @@ DEFAULT_OPTIONS = {
}
```
-You would use the option, `:reverse => true`, if you wanted a leaderboard sorted from lowest to highest score.
+The `DEFAULT_PAGE_SIZE` is 25.
-You can pass in an existing connection to Redis using :redis_connection in the Redis options hash:
+You would use the option, `:reverse => true`, if you wanted a leaderboard sorted from lowest-to-highest score. You
+may also set the `reverse` option on a leaderboard after you have created a new instance of a leaderboard.
+
+You can pass in an existing connection to Redis using `:redis_connection` in the Redis options hash:
```ruby
redis = Redis.new
@@ -70,7 +73,7 @@ You can set the page size to something other than the default page size (25):
=> #<Leaderboard:0x000001028791e8 @leaderboard_name="highscores", @page_size=5, @redis_connection=#<Redis client v2.2.2 connected to redis://127.0.0.1:6379/0 (Redis v2.2.5)>>
```
-Add members to your leaderboard using rank_member:
+Add members to your leaderboard using `rank_member`:
```ruby
1.upto(10) do |index|
@@ -79,7 +82,7 @@ Add members to your leaderboard using rank_member:
=> 1
```
-You can call rank_member with the same member and the leaderboard will be updated automatically.
+You can call `rank_member` with the same member and the leaderboard will be updated automatically.
Get some information about your leaderboard:
@@ -134,14 +137,14 @@ Add more members to your leaderboard:
=> 3
```
-Get an "Around Me" leaderboard for a member:
+Get an "Around Me" leaderboard page for a given member, which pulls members above and below the given member:
```ruby
highscore_lb.around_me('member_53')
=> [{:member=>"member_65", :rank=>31, :score=>65.0}, {:member=>"member_64", :rank=>32, :score=>64.0}, {:member=>"member_63", :rank=>33, :score=>63.0}, {:member=>"member_62", :rank=>34, :score=>62.0}, {:member=>"member_61", :rank=>35, :score=>61.0}, {:member=>"member_60", :rank=>36, :score=>60.0}, {:member=>"member_59", :rank=>37, :score=>59.0}, {:member=>"member_58", :rank=>38, :score=>58.0}, {:member=>"member_57", :rank=>39, :score=>57.0}, {:member=>"member_56", :rank=>40, :score=>56.0}, {:member=>"member_55", :rank=>41, :score=>55.0}, {:member=>"member_54", :rank=>42, :score=>54.0}, {:member=>"member_53", :rank=>43, :score=>53.0}, {:member=>"member_52", :rank=>44, :score=>52.0}, {:member=>"member_51", :rank=>45, :score=>51.0}, {:member=>"member_50", :rank=>46, :score=>50.0}, {:member=>"member_10", :rank=>47, :score=>10.0}, {:member=>"member_9", :rank=>48, :score=>9.0}, {:member=>"member_8", :rank=>49, :score=>8.0}, {:member=>"member_7", :rank=>50, :score=>7.0}, {:member=>"member_6", :rank=>51, :score=>6.0}, {:member=>"member_5", :rank=>52, :score=>5.0}, {:member=>"member_4", :rank=>53, :score=>4.0}, {:member=>"member_3", :rank=>54, :score=>3.0}, {:member=>"member_2", :rank=>55, :score=>2.0}]
```
-Get rank and score for an arbitrary list of members (e.g. friends):
+Get rank and score for an arbitrary list of members (e.g. friends) from the leaderboard:
```ruby
highscore_lb.ranked_in_list(['member_1', 'member_62', 'member_67'])
View
@@ -1,23 +1,36 @@
require 'redis'
require 'leaderboard/version'
-class Leaderboard
+class Leaderboard
+ # Default page size: 25
DEFAULT_PAGE_SIZE = 25
+ # Default options when creating a leaderboard. Page size is 25 and reverse
+ # is set to false, meaning various methods will return results in
+ # highest-to-lowest order.
DEFAULT_OPTIONS = {
:page_size => DEFAULT_PAGE_SIZE,
:reverse => false
}
+ # Default Redis host: localhost
DEFAULT_REDIS_HOST = 'localhost'
+ # Default Redis post: 6379
DEFAULT_REDIS_PORT = 6379
+ # Default Redis options when creating a connection to Redis. The
+ # +DEFAULT_REDIS_HOST+ and +DEFAULT_REDIS_PORT+ will be passed.
DEFAULT_REDIS_OPTIONS = {
:host => DEFAULT_REDIS_HOST,
:port => DEFAULT_REDIS_PORT
}
+ # Default options when requesting data from a leaderboard.
+ # +:with_scores+ true: Return scores along with the member names.
+ # +:with_rank+ true: Return ranks along with the member names.
+ # +:use_zero_index_for_rank+ false: If you want to 0-index ranks.
+ # +:page_size+ nil: The default page size will be used.
DEFAULT_LEADERBOARD_REQUEST_OPTIONS = {
:with_scores => true,
:with_rank => true,
@@ -30,6 +43,11 @@ class Leaderboard
# Page size to be used when paging through the leaderboard.
attr_reader :page_size
+
+ # Determines whether or not various leaderboard methods return their
+ # data in highest-to-lowest (+:reverse+ false) or
+ # lowest-to-highest (+:reverse+ true)
+ attr_accessor :reverse
# Create a new instance of a leaderboard.
#
@@ -1,3 +1,4 @@
class Leaderboard
- VERSION = '2.0.5'.freeze
+ # Leaderboard version
+ VERSION = '2.0.6'.freeze
end
View
@@ -2,8 +2,8 @@
describe 'Leaderboard' do
before(:each) do
- @redis_connection = Redis.new(:host => "127.0.0.1")
- @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1")
+ @redis_connection = Redis.new(:host => "127.0.0.1", :db => 15)
+ @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
end
after(:each) do
@@ -300,8 +300,8 @@
end
it 'should allow you to merge leaderboards' do
- foo = Leaderboard.new('foo')
- bar = Leaderboard.new('bar')
+ foo = Leaderboard.new('foo', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
+ bar = Leaderboard.new('bar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
foo.rank_member('foo_1', 1)
foo.rank_member('foo_2', 2)
@@ -312,7 +312,7 @@
foobar_keys = foo.merge_leaderboards('foobar', ['bar'])
foobar_keys.should be(5)
- foobar = Leaderboard.new('foobar')
+ foobar = Leaderboard.new('foobar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
foobar.total_members.should be(5)
first_leader_in_foobar = foobar.leaders(1).first
@@ -326,8 +326,8 @@
end
it 'should allow you to intersect leaderboards' do
- foo = Leaderboard.new('foo')
- bar = Leaderboard.new('bar')
+ foo = Leaderboard.new('foo', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
+ bar = Leaderboard.new('bar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
foo.rank_member('foo_1', 1)
foo.rank_member('foo_2', 2)
@@ -339,7 +339,7 @@
foobar_keys = foo.intersect_leaderboards('foobar', ['bar'], {:aggregate => :max})
foobar_keys.should be(2)
- foobar = Leaderboard.new('foobar')
+ foobar = Leaderboard.new('foobar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
foobar.total_members.should be(2)
first_leader_in_foobar = foobar.leaders(1).first
@@ -478,4 +478,26 @@
@leaderboard.total_members.should be(2)
@leaderboard.leaders(1).first[:member].should == 'member_10'
end
+
+ it 'should allow you to set reverse after creating a leaderboard to see results in highest-to-lowest or lowest-to-highest order' do
+ rank_members_in_leaderboard(25)
+
+ leaders = @leaderboard.leaders(1)
+
+ leaders.size.should be(25)
+ leaders[0][:member].should == 'member_25'
+ leaders[-2][:member].should == 'member_2'
+ leaders[-1][:member].should == 'member_1'
+ leaders[-1][:score].to_i.should be(1)
+
+ @leaderboard.reverse = true
+
+ leaders = @leaderboard.leaders(1)
+
+ leaders.size.should be(25)
+ leaders[0][:member].should == 'member_1'
+ leaders[-2][:member].should == 'member_24'
+ leaders[-1][:member].should == 'member_25'
+ leaders[-1][:score].to_i.should be(25)
+ end
end
@@ -2,8 +2,8 @@
describe 'Leaderboard (reverse option)' do
before(:each) do
- @redis_connection = Redis.new(:host => "127.0.0.1")
- @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:reverse => true}), :host => "127.0.0.1")
+ @redis_connection = Redis.new(:host => "127.0.0.1", :db => 15)
+ @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:reverse => true}), :host => "127.0.0.1", :db => 15)
end
after(:each) do
@@ -164,8 +164,8 @@
end
it 'should allow you to merge leaderboards' do
- foo = Leaderboard.new('foo')
- bar = Leaderboard.new('bar')
+ foo = Leaderboard.new('foo', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
+ bar = Leaderboard.new('bar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
foo.rank_member('foo_1', 1)
foo.rank_member('foo_2', 2)
@@ -176,7 +176,7 @@
foobar_keys = foo.merge_leaderboards('foobar', ['bar'])
foobar_keys.should be(5)
- foobar = Leaderboard.new('foobar')
+ foobar = Leaderboard.new('foobar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
foobar.total_members.should be(5)
first_leader_in_foobar = foobar.leaders(1).first
@@ -190,8 +190,8 @@
end
it 'should allow you to intersect leaderboards' do
- foo = Leaderboard.new('foo')
- bar = Leaderboard.new('bar')
+ foo = Leaderboard.new('foo', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
+ bar = Leaderboard.new('bar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
foo.rank_member('foo_1', 1)
foo.rank_member('foo_2', 2)
@@ -203,7 +203,7 @@
foobar_keys = foo.intersect_leaderboards('foobar', ['bar'], {:aggregate => :max})
foobar_keys.should be(2)
- foobar = Leaderboard.new('foobar')
+ foobar = Leaderboard.new('foobar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
foobar.total_members.should be(2)
first_leader_in_foobar = foobar.leaders(1).first
View
@@ -4,6 +4,9 @@
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
RSpec.configure do |config|
+ # Helper method to rank members in a leaderboard
+ #
+ # @param members_to_add [int] Number of members to add to the leaderboard.
def rank_members_in_leaderboard(members_to_add = 5)
1.upto(members_to_add) do |index|
@leaderboard.rank_member("member_#{index}", index)
View
@@ -2,6 +2,6 @@
describe 'Leaderboard::VERSION' do
it 'should be the correct version' do
- Leaderboard::VERSION.should == '2.0.5'
+ Leaderboard::VERSION.should == '2.0.6'
end
end

0 comments on commit 187771b

Please sign in to comment.