Permalink
Browse files

Version 3 proposal: Add a `:sort_by` option

As indicated in #29, the currently undocumented behavior of a
direct call to `ranked_in_list` is to do no sorting on the results.
The call was originally intended to be used to retrieve a list
of friends from a leaderboard. However, it would be useful in
certain situations to have the results sorted. This adds a
`:sort_by` option to that would be appropriate for the
`ranked_in_list` call to sort by either score (`:score`) or
rank (`:rank`) when returning results.
  • Loading branch information...
1 parent e4b5922 commit a8bd12f2d5d18d001590cfd4f805c46f87f9cfed @czarneckid czarneckid committed Oct 21, 2012
Showing with 82 additions and 1 deletion.
  1. +10 −1 lib/leaderboard.rb
  2. +36 −0 spec/leaderboard_spec.rb
  3. +36 −0 spec/reverse_leaderboard_spec.rb
View
@@ -32,12 +32,14 @@ class Leaderboard
# +:with_member_data+ false: Return member data 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.
+ # +: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,
:use_zero_index_for_rank => false,
- :page_size => nil
+ :page_size => nil,
+ :sort_by => :none
}
# Name of the leaderboard.
@@ -835,6 +837,13 @@ def ranked_in_list_in(leaderboard_name, members, options = {})
ranks_for_members << data
end
+ case leaderboard_options[:sort_by]
+ when :rank
+ ranks_for_members = ranks_for_members.sort_by { |member| member[:rank] }
+ when :score
+ ranks_for_members = ranks_for_members.sort_by { |member| member[:score] }
+ end
+
ranks_for_members
end
View
@@ -638,4 +638,40 @@
members[0][:score].to_i.should be(25)
members[24][:member].should eql('member_1')
end
+
+ it 'should sort by rank if the :sort_by option is set to :rank' do
+ rank_members_in_leaderboard(25)
+
+ members = ['member_5', 'member_1', 'member_10']
+ ranked_members = @leaderboard.ranked_in_list(members, :sort_by => :rank)
+
+ ranked_members.size.should be(3)
+
+ ranked_members[0][:rank].should be(16)
+ ranked_members[0][:score].should eql(10.0)
+
+ ranked_members[1][:rank].should be(21)
+ ranked_members[1][:score].should eql(5.0)
+
+ ranked_members[2][:rank].should be(25)
+ ranked_members[2][:score].should eql(1.0)
+ end
+
+ it 'should sort by score if the :sort_by option is set to :score' do
+ rank_members_in_leaderboard(25)
+
+ members = ['member_5', 'member_1', 'member_10']
+ ranked_members = @leaderboard.ranked_in_list(members, :sort_by => :score)
+
+ ranked_members.size.should be(3)
+
+ ranked_members[0][:rank].should be(25)
+ ranked_members[0][:score].should eql(1.0)
+
+ ranked_members[1][:rank].should be(21)
+ ranked_members[1][:score].should eql(5.0)
+
+ ranked_members[2][:rank].should be(16)
+ ranked_members[2][:score].should eql(10.0)
+ end
end
@@ -354,4 +354,40 @@
members[0][:score].to_i.should be(1)
members[24][:member].should eql('member_25')
end
+
+ it 'should sort by rank if the :sort_by option is set to :rank' do
+ rank_members_in_leaderboard(25)
+
+ members = ['member_5', 'member_1', 'member_10']
+ ranked_members = @leaderboard.ranked_in_list(members, :sort_by => :rank)
+
+ ranked_members.size.should be(3)
+
+ ranked_members[0][:rank].should be(1)
+ ranked_members[0][:score].should eql(1.0)
+
+ ranked_members[1][:rank].should be(5)
+ ranked_members[1][:score].should eql(5.0)
+
+ ranked_members[2][:rank].should be(10)
+ ranked_members[2][:score].should eql(10.0)
+ end
+
+ it 'should sort by score if the :sort_by option is set to :score' do
+ rank_members_in_leaderboard(25)
+
+ members = ['member_5', 'member_1', 'member_10']
+ ranked_members = @leaderboard.ranked_in_list(members, :sort_by => :score)
+
+ ranked_members.size.should be(3)
+
+ ranked_members[0][:rank].should be(1)
+ ranked_members[0][:score].should eql(1.0)
+
+ ranked_members[1][:rank].should be(5)
+ ranked_members[1][:score].should eql(5.0)
+
+ ranked_members[2][:rank].should be(10)
+ ranked_members[2][:score].should eql(10.0)
+ end
end

0 comments on commit a8bd12f

Please sign in to comment.