Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Resolve conflicts from merge of version-3-proposal-remove-zero-index-…

…for-rank
  • Loading branch information...
commit e5551f2533212812d364d220955aa1119a1d87ab 2 parents 7553f18 + b05e1ad
@czarneckid czarneckid authored
View
8 README.markdown
@@ -166,10 +166,10 @@ Get page 1 in the leaderboard:
highscore_lb.leaders(1)
=> [{:member=>"member_10", :rank=>1, :score=>10.0}, {:member=>"member_9", :rank=>2, :score=>9.0}, {:member=>"member_8", :rank=>3, :score=>8.0}, {:member=>"member_7", :rank=>4, :score=>7.0}, {:member=>"member_6", :rank=>5, :score=>6.0}, {:member=>"member_5", :rank=>6, :score=>5.0}, {:member=>"member_4", :rank=>7, :score=>4.0}, {:member=>"member_3", :rank=>8, :score=>3.0}, {:member=>"member_2", :rank=>9, :score=>2.0}, {:member=>"member_1", :rank=>10, :score=>1.0}]
```
-
-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`, `:use_zero_index_for_rank`
-and `:page_size`. Below is an example of retrieving the first page 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
View
55 lib/leaderboard.rb
@@ -30,13 +30,11 @@ class 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.
- # +: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,
:with_member_data => false,
- :use_zero_index_for_rank => false,
:page_size => nil
}
@@ -300,33 +298,23 @@ def change_score_for_member_in(leaderboard_name, member, delta)
# Retrieve the rank for a member in the leaderboard.
#
# @param member [String] Member name.
- # @param use_zero_index_for_rank [boolean, false] If the returned rank should be 0-indexed.
- #
+ #
# @return the rank for a member in the leaderboard.
- def rank_for(member, use_zero_index_for_rank = false)
- rank_for_in(@leaderboard_name, member, use_zero_index_for_rank)
+ def rank_for(member)
+ rank_for_in(@leaderboard_name, member)
end
# Retrieve the rank for a member in the named leaderboard.
#
# @param leaderboard_name [String] Name of the leaderboard.
# @param member [String] Member name.
- # @param use_zero_index_for_rank [boolean, false] If the returned rank should be 0-indexed.
- #
+ #
# @return the rank for a member in the leaderboard.
- def rank_for_in(leaderboard_name, member, use_zero_index_for_rank = false)
+ def rank_for_in(leaderboard_name, member)
if @reverse
- if use_zero_index_for_rank
- return @redis_connection.zrank(leaderboard_name, member)
- else
- return @redis_connection.zrank(leaderboard_name, member) + 1 rescue nil
- end
+ return @redis_connection.zrank(leaderboard_name, member) + 1 rescue nil
else
- if use_zero_index_for_rank
- return @redis_connection.zrevrank(leaderboard_name, member)
- else
- return @redis_connection.zrevrank(leaderboard_name, member) + 1 rescue nil
- end
+ return @redis_connection.zrevrank(leaderboard_name, member) + 1 rescue nil
end
end
@@ -371,21 +359,19 @@ def check_member_in?(leaderboard_name, member)
# Retrieve the score and rank for a member in the leaderboard.
#
# @param member [String] Member name.
- # @param use_zero_index_for_rank [boolean, false] If the returned rank should be 0-indexed.
#
# @return the score and rank for a member in the leaderboard as a Hash.
- def score_and_rank_for(member, use_zero_index_for_rank = false)
- score_and_rank_for_in(@leaderboard_name, member, use_zero_index_for_rank)
+ def score_and_rank_for(member)
+ score_and_rank_for_in(@leaderboard_name, member)
end
# Retrieve the score and rank for a member in the named leaderboard.
#
# @param leaderboard_name [String]Name of the leaderboard.
# @param member [String] Member name.
- # @param use_zero_index_for_rank [boolean, false] If the returned rank should be 0-indexed.
#
# @return the score and rank for a member in the named leaderboard as a Hash.
- def score_and_rank_for_in(leaderboard_name, member, use_zero_index_for_rank = false)
+ def score_and_rank_for_in(leaderboard_name, member)
responses = @redis_connection.multi do |transaction|
transaction.zscore(leaderboard_name, member)
if @reverse
@@ -396,11 +382,9 @@ def score_and_rank_for_in(leaderboard_name, member, use_zero_index_for_rank = fa
end
responses[0] = responses[0].to_f
- if !use_zero_index_for_rank
- responses[1] = responses[1] + 1 rescue nil
- end
-
- {:member => member, :score => responses[0], :rank => responses[1]}
+ responses[1] = responses[1] + 1 rescue nil
+
+ {:member => member, :score => responses[0], :rank => responses[1]}
end
# Remove members from the leaderboard in a given score range.
@@ -809,23 +793,14 @@ def ranked_in_list_in(leaderboard_name, members, options = {})
data[:member] = member
if leaderboard_options[:with_scores]
if leaderboard_options[:with_rank]
- if leaderboard_options[:use_zero_index_for_rank]
- data[:rank] = responses[index * 2]
- else
- data[:rank] = responses[index * 2] + 1 rescue nil
- end
-
+ 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]
- if leaderboard_options[:use_zero_index_for_rank]
- data[:rank] = responses[index]
- else
- data[:rank] = responses[index] + 1 rescue nil
- end
+ data[:rank] = responses[index] + 1 rescue nil
end
end
View
5 spec/leaderboard_spec.rb
@@ -69,7 +69,6 @@
rank_members_in_leaderboard(5)
@leaderboard.rank_for('member_4').should be(2)
- @leaderboard.rank_for('member_4', true).should be(1)
end
it 'should return the correct score when calling score_for' do
@@ -271,7 +270,6 @@
@leaderboard.member_at(50)[:rank].should eql(50)
@leaderboard.member_at(51).should be_nil
@leaderboard.member_at(1, :with_member_data => true)[:member_data].should == {:member_name => 'Leaderboard member 50'}.to_s
- @leaderboard.member_at(1, :use_zero_index_for_rank => true)[:rank].should == 0
end
it 'should return the correct information when calling around_me' do
@@ -315,8 +313,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, :use_zero_index_for_rank => false})
-
+ ranked_members = @leaderboard.ranked_in_list(members, {:with_scores => false, :with_rank => true})
ranked_members.size.should be(3)
ranked_members[0][:rank].should be(25)
ranked_members[1][:rank].should be(21)
View
5 spec/reverse_leaderboard_spec.rb
@@ -16,7 +16,6 @@
rank_members_in_leaderboard(5)
@leaderboard.rank_for('member_4').should be(4)
- @leaderboard.rank_for('member_4', true).should be(3)
end
it 'should return the correct list when calling leaders' do
@@ -123,7 +122,6 @@
@leaderboard.member_at(50)[:rank].should eql(50)
@leaderboard.member_at(51).should be_nil
@leaderboard.member_at(1, :with_member_data => true)[:member_data].should == {:member_name => 'Leaderboard member 1'}.to_s
- @leaderboard.member_at(1, :use_zero_index_for_rank => true)[:rank].should == 0
end
it 'should return the correct information when calling around_me' do
@@ -167,8 +165,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, :use_zero_index_for_rank => false})
-
+ ranked_members = @leaderboard.ranked_in_list(members, {:with_scores => false, :with_rank => true})
ranked_members.size.should be(3)
ranked_members[0][:rank].should be(1)
Please sign in to comment.
Something went wrong with that request. Please try again.