Skip to content
This repository
Browse code

Merge pull request #8 from agoragames/reverse_option

Reverse option
  • Loading branch information...
commit 187771b0fbad87f60a76d670b906b0a625acb069 2 parents e5fcd73 + 0bb74a1
David Czarnecki authored April 26, 2012
4  CHANGELOG.markdown
Source Rendered
... ...
@@ -1,5 +1,9 @@
1 1
 # CHANGELOG
2 2
 
  3
+## leaderboard 2.0.6 (2012-04-26)
  4
+
  5
+* 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.
  6
+
3 7
 ## leaderboard 2.0.5 (2012-03-14)
4 8
 
5 9
 * 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.
15  README.markdown
Source Rendered
@@ -48,9 +48,12 @@ DEFAULT_OPTIONS = {
48 48
 }
49 49
 ```
50 50
 
51  
-You would use the option, `:reverse => true`, if you wanted a leaderboard sorted from lowest to highest score.
  51
+The `DEFAULT_PAGE_SIZE` is 25. 
52 52
 
53  
-You can pass in an existing connection to Redis using :redis_connection in the Redis options hash:
  53
+You would use the option, `:reverse => true`, if you wanted a leaderboard sorted from lowest-to-highest score. You 
  54
+may also set the `reverse` option on a leaderboard after you have created a new instance of a leaderboard.
  55
+
  56
+You can pass in an existing connection to Redis using `:redis_connection` in the Redis options hash:
54 57
 
55 58
 ```ruby
56 59
   redis = Redis.new
@@ -70,7 +73,7 @@ You can set the page size to something other than the default page size (25):
70 73
    => #<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)>> 
71 74
 ```
72 75
   
73  
-Add members to your leaderboard using rank_member:
  76
+Add members to your leaderboard using `rank_member`:
74 77
 
75 78
 ```ruby
76 79
   1.upto(10) do |index|
@@ -79,7 +82,7 @@ Add members to your leaderboard using rank_member:
79 82
    => 1 
80 83
 ```
81 84
 
82  
-You can call rank_member with the same member and the leaderboard will be updated automatically.
  85
+You can call `rank_member` with the same member and the leaderboard will be updated automatically.
83 86
 
84 87
 Get some information about your leaderboard:
85 88
 
@@ -134,14 +137,14 @@ Add more members to your leaderboard:
134 137
    => 3 
135 138
 ```
136 139
   
137  
-Get an "Around Me" leaderboard for a member:
  140
+Get an "Around Me" leaderboard page for a given member, which pulls members above and below the given member:
138 141
 
139 142
 ```ruby
140 143
   highscore_lb.around_me('member_53')
141 144
    => [{: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}] 
142 145
 ```
143 146
 	
144  
-Get rank and score for an arbitrary list of members (e.g. friends):
  147
+Get rank and score for an arbitrary list of members (e.g. friends) from the leaderboard:
145 148
 
146 149
 ```ruby
147 150
   highscore_lb.ranked_in_list(['member_1', 'member_62', 'member_67'])
20  lib/leaderboard.rb
... ...
@@ -1,23 +1,36 @@
1 1
 require 'redis'
2 2
 require 'leaderboard/version'
3 3
 
4  
-class Leaderboard
  4
+class Leaderboard 
  5
+  # Default page size: 25
5 6
   DEFAULT_PAGE_SIZE = 25
6 7
   
  8
+  # Default options when creating a leaderboard. Page size is 25 and reverse
  9
+  # is set to false, meaning various methods will return results in
  10
+  # highest-to-lowest order.
7 11
   DEFAULT_OPTIONS = {
8 12
     :page_size => DEFAULT_PAGE_SIZE,
9 13
     :reverse => false
10 14
   }
11 15
 
  16
+  # Default Redis host: localhost
12 17
   DEFAULT_REDIS_HOST = 'localhost'
13 18
   
  19
+  # Default Redis post: 6379
14 20
   DEFAULT_REDIS_PORT = 6379  
15 21
   
  22
+  # Default Redis options when creating a connection to Redis. The
  23
+  # +DEFAULT_REDIS_HOST+ and +DEFAULT_REDIS_PORT+ will be passed.
16 24
   DEFAULT_REDIS_OPTIONS = {
17 25
     :host => DEFAULT_REDIS_HOST,
18 26
     :port => DEFAULT_REDIS_PORT
19 27
   }
20 28
   
  29
+  # Default options when requesting data from a leaderboard.
  30
+  # +:with_scores+ true: Return scores along with the member names.
  31
+  # +:with_rank+ true: Return ranks along with the member names.
  32
+  # +:use_zero_index_for_rank+ false: If you want to 0-index ranks.
  33
+  # +:page_size+ nil: The default page size will be used.
21 34
   DEFAULT_LEADERBOARD_REQUEST_OPTIONS = {
22 35
     :with_scores => true, 
23 36
     :with_rank => true, 
@@ -30,6 +43,11 @@ class Leaderboard
30 43
 
31 44
   # Page size to be used when paging through the leaderboard. 
32 45
   attr_reader :page_size
  46
+
  47
+  # Determines whether or not various leaderboard methods return their
  48
+  # data in highest-to-lowest (+:reverse+ false) or 
  49
+  # lowest-to-highest (+:reverse+ true)
  50
+  attr_accessor :reverse
33 51
   
34 52
   # Create a new instance of a leaderboard.
35 53
   # 
3  lib/leaderboard/version.rb
... ...
@@ -1,3 +1,4 @@
1 1
 class Leaderboard
2  
-  VERSION = '2.0.5'.freeze
  2
+  # Leaderboard version
  3
+  VERSION = '2.0.6'.freeze
3 4
 end
38  spec/leaderboard_spec.rb
@@ -2,8 +2,8 @@
2 2
 
3 3
 describe 'Leaderboard' do
4 4
   before(:each) do
5  
-    @redis_connection = Redis.new(:host => "127.0.0.1")
6  
-    @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1")
  5
+    @redis_connection = Redis.new(:host => "127.0.0.1", :db => 15)
  6
+    @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
7 7
   end
8 8
 
9 9
   after(:each) do
@@ -300,8 +300,8 @@
300 300
   end
301 301
 
302 302
   it 'should allow you to merge leaderboards' do
303  
-    foo = Leaderboard.new('foo')    
304  
-    bar = Leaderboard.new('bar')
  303
+    foo = Leaderboard.new('foo', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
  304
+    bar = Leaderboard.new('bar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
305 305
     
306 306
     foo.rank_member('foo_1', 1)
307 307
     foo.rank_member('foo_2', 2)
@@ -312,7 +312,7 @@
312 312
     foobar_keys = foo.merge_leaderboards('foobar', ['bar'])
313 313
     foobar_keys.should be(5)
314 314
     
315  
-    foobar = Leaderboard.new('foobar')  
  315
+    foobar = Leaderboard.new('foobar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
316 316
     foobar.total_members.should be(5)
317 317
     
318 318
     first_leader_in_foobar = foobar.leaders(1).first
@@ -326,8 +326,8 @@
326 326
   end
327 327
 
328 328
   it 'should allow you to intersect leaderboards' do
329  
-    foo = Leaderboard.new('foo')
330  
-    bar = Leaderboard.new('bar')
  329
+    foo = Leaderboard.new('foo', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
  330
+    bar = Leaderboard.new('bar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
331 331
     
332 332
     foo.rank_member('foo_1', 1)
333 333
     foo.rank_member('foo_2', 2)
@@ -339,7 +339,7 @@
339 339
     foobar_keys = foo.intersect_leaderboards('foobar', ['bar'], {:aggregate => :max})    
340 340
     foobar_keys.should be(2)
341 341
     
342  
-    foobar = Leaderboard.new('foobar')
  342
+    foobar = Leaderboard.new('foobar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
343 343
     foobar.total_members.should be(2)
344 344
     
345 345
     first_leader_in_foobar = foobar.leaders(1).first
@@ -478,4 +478,26 @@
478 478
     @leaderboard.total_members.should be(2)
479 479
     @leaderboard.leaders(1).first[:member].should == 'member_10'
480 480
   end
  481
+
  482
+  it 'should allow you to set reverse after creating a leaderboard to see results in highest-to-lowest or lowest-to-highest order' do
  483
+    rank_members_in_leaderboard(25)
  484
+
  485
+    leaders = @leaderboard.leaders(1)
  486
+        
  487
+    leaders.size.should be(25)
  488
+    leaders[0][:member].should == 'member_25'
  489
+    leaders[-2][:member].should == 'member_2'
  490
+    leaders[-1][:member].should == 'member_1'
  491
+    leaders[-1][:score].to_i.should be(1)
  492
+
  493
+    @leaderboard.reverse = true
  494
+
  495
+    leaders = @leaderboard.leaders(1)
  496
+        
  497
+    leaders.size.should be(25)
  498
+    leaders[0][:member].should == 'member_1'
  499
+    leaders[-2][:member].should == 'member_24'
  500
+    leaders[-1][:member].should == 'member_25'
  501
+    leaders[-1][:score].to_i.should be(25)
  502
+  end
481 503
 end
16  spec/reverse_leaderboard_spec.rb
@@ -2,8 +2,8 @@
2 2
 
3 3
 describe 'Leaderboard (reverse option)' do
4 4
   before(:each) do
5  
-    @redis_connection = Redis.new(:host => "127.0.0.1")
6  
-    @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:reverse => true}), :host => "127.0.0.1")
  5
+    @redis_connection = Redis.new(:host => "127.0.0.1", :db => 15)
  6
+    @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS.merge({:reverse => true}), :host => "127.0.0.1", :db => 15)
7 7
   end
8 8
   
9 9
   after(:each) do
@@ -164,8 +164,8 @@
164 164
   end
165 165
 
166 166
   it 'should allow you to merge leaderboards' do
167  
-    foo = Leaderboard.new('foo')    
168  
-    bar = Leaderboard.new('bar')
  167
+    foo = Leaderboard.new('foo', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)    
  168
+    bar = Leaderboard.new('bar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
169 169
     
170 170
     foo.rank_member('foo_1', 1)
171 171
     foo.rank_member('foo_2', 2)
@@ -176,7 +176,7 @@
176 176
     foobar_keys = foo.merge_leaderboards('foobar', ['bar'])
177 177
     foobar_keys.should be(5)
178 178
     
179  
-    foobar = Leaderboard.new('foobar')  
  179
+    foobar = Leaderboard.new('foobar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)  
180 180
     foobar.total_members.should be(5)
181 181
     
182 182
     first_leader_in_foobar = foobar.leaders(1).first
@@ -190,8 +190,8 @@
190 190
   end
191 191
 
192 192
   it 'should allow you to intersect leaderboards' do
193  
-    foo = Leaderboard.new('foo')
194  
-    bar = Leaderboard.new('bar')
  193
+    foo = Leaderboard.new('foo', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
  194
+    bar = Leaderboard.new('bar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
195 195
     
196 196
     foo.rank_member('foo_1', 1)
197 197
     foo.rank_member('foo_2', 2)
@@ -203,7 +203,7 @@
203 203
     foobar_keys = foo.intersect_leaderboards('foobar', ['bar'], {:aggregate => :max})    
204 204
     foobar_keys.should be(2)
205 205
     
206  
-    foobar = Leaderboard.new('foobar')
  206
+    foobar = Leaderboard.new('foobar', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1", :db => 15)
207 207
     foobar.total_members.should be(2)
208 208
     
209 209
     first_leader_in_foobar = foobar.leaders(1).first
3  spec/spec_helper.rb
@@ -4,6 +4,9 @@
4 4
 Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
5 5
 
6 6
 RSpec.configure do |config|
  7
+  # Helper method to rank members in a leaderboard
  8
+  # 
  9
+  # @param members_to_add [int] Number of members to add to the leaderboard.
7 10
   def rank_members_in_leaderboard(members_to_add = 5)
8 11
     1.upto(members_to_add) do |index|
9 12
       @leaderboard.rank_member("member_#{index}", index)
2  spec/version_spec.rb
@@ -2,6 +2,6 @@
2 2
 
3 3
 describe 'Leaderboard::VERSION' do
4 4
   it 'should be the correct version' do
5  
-    Leaderboard::VERSION.should == '2.0.5'
  5
+    Leaderboard::VERSION.should == '2.0.6'
6 6
   end
7 7
 end

0 notes on commit 187771b

Please sign in to comment.
Something went wrong with that request. Please try again.