Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Removes `:with_scores` and `:with_ranks` as valid options

* Removes `:with_scores` and `:with_ranks` as valid options.
* `rake test_rubies` will only test under 1.8.7 and 1.9.3 now.
* Update README for :sort_by option.
* Update CHANGELOG.
  • Loading branch information...
commit 835e377a8f68f72692db74d4100887186a86f9e0 1 parent 9eaadac
@czarneckid czarneckid authored
View
1  .rvmrc
@@ -1,3 +1,2 @@
rvm --create 1.8.7@leaderboard_gem
-rvm --create 1.9.2@leaderboard_gem
rvm --create 1.9.3@leaderboard_gem
View
7 CHANGELOG.markdown
@@ -1,5 +1,12 @@
# CHANGELOG
+## leaderboard 3.0.1 (2012-11-08)
+
+* Removes `:use_zero_index_for_rank_option` as valid option for requesting data from the leaderboard. [Original proposal](https://github.com/agoragames/leaderboard/pull/27)
+* Adds `:sort_by` as valid option for requesting data from the leaderboard. [Original proposal](https://github.com/agoragames/leaderboard/pull/30)
+* Optional member data is stored in a single hash. Many benefits to this approach. [Original proposal](https://github.com/agoragames/leaderboard/pull/26)
+* Removes `:with_scores` and `:with_ranks` as valid options for requesting data from the leaderboard.
+
## leaderboard 2.5.0 (2012-10-12)
* Added `members_from_rank_range` and `members_from_rank_range_in` methods to be able to retrieve members from a leaderboard in a given rank range.
View
31 README.markdown
@@ -168,21 +168,8 @@ Get page 1 in the leaderboard:
```
You can pass various options to the calls `leaders`, `around_me` and `ranked_in_list`.
-Valid options are `:with_scores`, `:with_rank`, `:with_member_data`
-and `:page_size`. Below is an example of retrieving the first page in the leaderboard
-without ranks:
-
-```ruby
- highscore_lb.leaders(1, :with_rank => false)
- => [{:member=>"member_10", :score=>9.0}, {:member=>"member_9", :score=>7.0}, {:member=>"member_8", :score=>5.0}, {:member=>"member_7", :score=>3.0}, {:member=>"member_6", :score=>1.0}, {:member=>"member_5", :score=>0.0}, {:member=>"member_4", :score=>0.0}, {:member=>"member_3", :score=>0.0}, {:member=>"member_2", :score=>0.0}, {:member=>"member_1", :score=>0.0}]
-```
-
-Below is an example of retrieving the first page in the leaderboard without scores or ranks:
-
-```ruby
- highscore_lb.leaders(1, :with_scores => false, :with_rank => false)
- => [{:member=>"member_10"}, {:member=>"member_9"}, {:member=>"member_8"}, {:member=>"member_7"}, {:member=>"member_6"}, {:member=>"member_5"}, {:member=>"member_4"}, {:member=>"member_3"}, {:member=>"member_2"}, {:member=>"member_1"}]
-```
+Valid options are `:with_member_data`, `:page_size` and `:sort_option`. Valid values for
+`:sort_option` are `:none` (default), `:score` and `:rank`.
You can also use the `members` and `members_in` methods as aliases for the `leaders` and `leaders_in` methods.
@@ -235,6 +222,20 @@ members = highscore_lb.members_from_rank_range(1, 5)
=> [{:member=>"member_95", :rank=>1, :score=>95.0}, {:member=>"member_94", :rank=>2, :score=>94.0}, {:member=>"member_93", :rank=>3, :score=>93.0}, {:member=>"member_92", :rank=>4, :score=>92.0}, {:member=>"member_91", :rank=>5, :score=>91.0}]
```
+The option `:sort_option` is useful for retrieving an arbitrary list of
+members from a given leaderboard where you would like the data sorted
+when returned. The follow examples demonstrate its use:
+
+```ruby
+friends = highscore_lb.ranked_in_list(['member_6', 'member_1', 'member_10'], :sort_by => :rank)
+ => [{:member=>"member_10", :rank=>47, :score=>10.0}, {:member=>"member_6", :rank=>51, :score=>6.0}, {:member=>"member_1", :rank=>56, :score=>1.0}]
+```
+
+```ruby
+friends = highscore_lb.ranked_in_list(['member_6', 'member_1', 'member_10'], :sort_by => :score)
+ => [{:member=>"member_1", :rank=>56, :score=>1.0}, {:member=>"member_6", :rank=>51, :score=>6.0}, {:member=>"member_10", :rank=>47, :score=>10.0}]
+```
+
### Ranking multiple members in a leaderboard at once
Insert multiple data items for members and their associated scores:
View
4 Rakefile
@@ -10,7 +10,7 @@ end
task :default => :spec
-desc "Run the specs against Ruby 1.8.7, 1.9.2 and 1.9.3"
+desc "Run the specs against Ruby 1.8.7 and 1.9.3"
task :test_rubies do
- system "rvm 1.8.7@leaderboard_gem,1.9.2@leaderboard_gem,1.9.3@leaderboard_gem do rake spec"
+ system "rvm 1.8.7@leaderboard_gem,1.9.3@leaderboard_gem do rake spec"
end
View
24 lib/leaderboard.rb
@@ -27,14 +27,10 @@ class Leaderboard
}
# 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.
# +:with_member_data+ false: Return member data along with the member names.
# +:page_size+ nil: The default page size will be used.
# +:sort_by+ :none: The default sort for a call to `ranked_in_list`.
DEFAULT_LEADERBOARD_REQUEST_OPTIONS = {
- :with_scores => true,
- :with_rank => true,
:with_member_data => false,
:page_size => nil,
:sort_by => :none
@@ -782,29 +778,19 @@ def ranked_in_list_in(leaderboard_name, members, options = {})
responses = @redis_connection.multi do |transaction|
members.each do |member|
if @reverse
- transaction.zrank(leaderboard_name, member) if leaderboard_options[:with_rank]
+ transaction.zrank(leaderboard_name, member)
else
- transaction.zrevrank(leaderboard_name, member) if leaderboard_options[:with_rank]
+ transaction.zrevrank(leaderboard_name, member)
end
- transaction.zscore(leaderboard_name, member) if leaderboard_options[:with_scores]
+ transaction.zscore(leaderboard_name, member)
end
end
members.each_with_index do |member, index|
data = {}
data[:member] = member
- if leaderboard_options[:with_scores]
- if leaderboard_options[:with_rank]
- data[:rank] = responses[index * 2] + 1 rescue nil
- data[:score] = responses[index * 2 + 1].to_f
- else
- data[:score] = responses[index].to_f
- end
- else
- if leaderboard_options[:with_rank]
- data[:rank] = responses[index] + 1 rescue nil
- end
- end
+ data[:rank] = responses[index * 2] + 1 rescue nil
+ data[:score] = responses[index * 2 + 1].to_f
if leaderboard_options[:with_member_data]
data[:member_data] = member_data_for_in(leaderboard_name, member)
View
69 spec/leaderboard_spec.rb
@@ -161,15 +161,15 @@
it 'should allow you to retrieve members in a given score range' do
rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
- members = @leaderboard.members_from_score_range(10, 15, {:with_scores => false, :with_rank => false})
+ members = @leaderboard.members_from_score_range(10, 15)
- member_15 = {:member => 'member_15'}
+ member_15 = {:member => 'member_15', :score => 15.0, :rank => 11}
members[0].should eql(member_15)
- member_10 = {:member => 'member_10'}
+ member_10 = {:member => 'member_10', :score => 10.0, :rank => 16}
members[5].should eql(member_10)
- members = @leaderboard.members_from_score_range(10, 15, {:with_scores => true, :with_rank => true, :with_member_data => true})
+ members = @leaderboard.members_from_score_range(10, 15, {:with_member_data => true})
member_15 = {:member => 'member_15', :rank => 11, :score => 15.0, :member_data => {:member_name => 'Leaderboard member 15'}.to_s}
members[0].should == member_15
@@ -178,29 +178,16 @@
members[5].should == member_10
end
- it 'should allow you to retrieve leaders without scores and ranks' do
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
-
- @leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE)
- leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
-
- member_25 = {:member => 'member_25'}
- leaders[0].should eql(member_25)
-
- member_1 = {:member => 'member_1'}
- leaders[24].should eql(member_1)
- end
-
it 'should allow you to retrieve leaders with extra data' do
rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
@leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE)
- leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false, :with_member_data => true})
+ leaders = @leaderboard.leaders(1, {:with_member_data => true})
- member_25 = {:member => 'member_25', :member_data => { :member_name => "Leaderboard member 25" }.to_s }
+ member_25 = {:member => 'member_25', :score => 25.0, :rank => 1, :member_data => { :member_name => "Leaderboard member 25" }.to_s }
leaders[0].should == member_25
- member_1 = {:member => 'member_1', :member_data => { :member_name => "Leaderboard member 1" }.to_s }
+ member_1 = {:member => 'member_1', :score => 1.0, :rank => 25, :member_data => { :member_name => "Leaderboard member 1" }.to_s }
leaders[24].should == member_1
end
@@ -232,33 +219,7 @@
leaders = @leaderboard.leaders(1, :page_size => 1)
leaders.size.should be(1)
-
- leaders = @leaderboard.leaders(1, :with_rank => false)
- leaders.size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
- member_26 = {:member => 'member_26', :score => 26.0}
- member_25 = {:member => 'member_25', :score => 25.0}
- member_24 = {:member => 'member_24', :score => 24.0}
- leaders[0].should eql(member_26)
- leaders[1].should eql(member_25)
- leaders[2].should eql(member_24)
-
- leaders = @leaderboard.leaders(1, :with_scores => false)
- leaders.size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
- member_26 = {:member => 'member_26', :rank => 1}
- member_25 = {:member => 'member_25', :rank => 2}
- leaders[0].should eql(member_26)
- leaders[1].should eql(member_25)
-
- leaders = @leaderboard.leaders(1, :with_scores => false, :with_rank => false)
- leaders.size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
- member_26 = {:member => 'member_26'}
- member_25 = {:member => 'member_25'}
- leaders[0].should eql(member_26)
- leaders[1].should eql(member_25)
-
- leaders = @leaderboard.leaders(1, :with_rank => false, :page_size => 1)
- leaders.size.should be(1)
- member_26 = {:member => 'member_26', :score => 26.0}
+ member_26 = {:member => 'member_26', :score => 26.0, :rank => 1}
leaders[0].should eql(member_26)
end
@@ -313,7 +274,7 @@
@leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE)
members = ['member_1', 'member_5', 'member_10']
- ranked_members = @leaderboard.ranked_in_list(members, {:with_scores => false, :with_rank => true})
+ ranked_members = @leaderboard.ranked_in_list(members, {:with_rank => true})
ranked_members.size.should be(3)
ranked_members[0][:rank].should be(25)
ranked_members[1][:rank].should be(21)
@@ -449,19 +410,15 @@
foobar.disconnect
end
- it 'should respect the with_scores option in the massage_leader_data method' do
+ it 'should respect options in the massage_leader_data method' do
rank_members_in_leaderboard(25)
@leaderboard.total_members.should be(25)
- leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
+ leaders = @leaderboard.leaders(1)
leaders[0][:member].should_not be_nil
- leaders[0][:score].should be_nil
- leaders[0][:rank].should be_nil
-
- @leaderboard.page_size = 25
- leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
- leaders.size.should be(25)
+ leaders[0][:score].should_not be_nil
+ leaders[0][:rank].should_not be_nil
@leaderboard.page_size = Leaderboard::DEFAULT_PAGE_SIZE
leaders = @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
View
63 spec/reverse_leaderboard_spec.rb
@@ -51,13 +51,13 @@
members = @leaderboard.members_from_score_range(10, 15, {:with_scores => false, :with_rank => false})
- member_10 = {:member => 'member_10'}
+ member_10 = {:member => 'member_10', :score => 10.0, :rank => 10}
members[0].should eql(member_10)
- member_15 = {:member => 'member_15'}
+ member_15 = {:member => 'member_15', :score => 15.0, :rank => 15}
members[5].should eql(member_15)
- members = @leaderboard.members_from_score_range(10, 15, {:with_scores => true, :with_rank => true, :with_member_data => true})
+ members = @leaderboard.members_from_score_range(10, 15, {:with_member_data => true})
member_10 = {:member => 'member_10', :rank => 10, :score => 10.0, :member_data => {:member_name => 'Leaderboard member 10'}.to_s}
members[0].should == member_10
@@ -66,52 +66,20 @@
members[5].should == member_15
end
- it 'should allow you to retrieve leaders without scores and ranks' do
- rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE)
-
- @leaderboard.total_members.should be(Leaderboard::DEFAULT_PAGE_SIZE)
- leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
-
- member_1 = {:member => 'member_1'}
- leaders[0].should eql(member_1)
-
- member_25 = {:member => 'member_25'}
- leaders[24].should eql(member_25)
- end
-
it 'should allow you to call leaders with various options that respect the defaults for the options not passed in' do
rank_members_in_leaderboard(Leaderboard::DEFAULT_PAGE_SIZE + 1)
leaders = @leaderboard.leaders(1, :page_size => 1)
leaders.size.should be(1)
- leaders = @leaderboard.leaders(1, :with_rank => false)
+ leaders = @leaderboard.leaders(1)
leaders.size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
- member_1 = {:member => 'member_1', :score => 1.0}
- member_2 = {:member => 'member_2', :score => 2.0}
- member_3 = {:member => 'member_3', :score => 3.0}
+ member_1 = {:member => 'member_1', :score => 1.0, :rank => 1}
+ member_2 = {:member => 'member_2', :score => 2.0, :rank => 2}
+ member_3 = {:member => 'member_3', :score => 3.0, :rank => 3}
leaders[0].should eql(member_1)
leaders[1].should eql(member_2)
leaders[2].should eql(member_3)
-
- leaders = @leaderboard.leaders(1, :with_scores => false)
- leaders.size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
- member_1 = {:member => 'member_1', :rank => 1}
- member_2 = {:member => 'member_2', :rank => 2}
- leaders[0].should eql(member_1)
- leaders[1].should eql(member_2)
-
- leaders = @leaderboard.leaders(1, :with_scores => false, :with_rank => false)
- leaders.size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
- member_1 = {:member => 'member_1'}
- member_2 = {:member => 'member_2'}
- leaders[0].should eql(member_1)
- leaders[1].should eql(member_2)
-
- leaders = @leaderboard.leaders(1, :with_rank => false, :page_size => 1)
- leaders.size.should be(1)
- member_1 = {:member => 'member_1', :score => 1.0}
- leaders[0].should eql(member_1)
end
it 'should return a single member when calling member_at' do
@@ -258,29 +226,22 @@
foobar.disconnect
end
- it 'should respect the with_scores option in the massage_leader_data method' do
+ it 'should respect options in the massage_leader_data method' do
rank_members_in_leaderboard(25)
@leaderboard.total_members.should be(25)
- leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
+ leaders = @leaderboard.leaders(1)
leaders[0][:member].should_not be_nil
- leaders[0][:score].should be_nil
- leaders[0][:rank].should be_nil
-
- @leaderboard.page_size = 25
- leaders = @leaderboard.leaders(1, {:with_scores => false, :with_rank => false})
- leaders.size.should be(25)
+ leaders[0][:score].should_not be_nil
+ leaders[0][:rank].should_not be_nil
@leaderboard.page_size = Leaderboard::DEFAULT_PAGE_SIZE
leaders = @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
+ leaders.size.should be(25)
leaders[0][:member].should_not be_nil
leaders[0][:score].should_not be_nil
leaders[0][:rank].should_not be_nil
-
- @leaderboard.page_size = 25
- leaders = @leaderboard.leaders(1, Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS)
- leaders.size.should be(25)
end
it 'should return the correct number of members when calling around_me with a page_size options' do
Please sign in to comment.
Something went wrong with that request. Please try again.