Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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...
commit 0fd079f07e73346a9b752ca148fbf16594a80bfe 1 parent 7110511
@czarneckid czarneckid authored
View
11 lib/leaderboard.rb
@@ -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.
@@ -789,6 +791,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
36 spec/leaderboard_spec.rb
@@ -618,4 +618,40 @@
leaders[-1][:member].should eql('member_25')
leaders[-1][:score].to_i.should be(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(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
View
36 spec/reverse_leaderboard_spec.rb
@@ -334,4 +334,40 @@
@leaderboard.total_members.should be(2)
@leaderboard.leaders(1).first[: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(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
Please sign in to comment.
Something went wrong with that request. Please try again.