Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Version 3 proposal: Remove use_zero_index_for_rank_option #27

Closed
wants to merge 3 commits into from

2 participants

@czarneckid
Owner

Is this ever really used in practice? My guess is no. Although it
could be left in as it's not necessarily a lot of code, but it
does remove some conditional paths.

czarneckid added some commits
@czarneckid czarneckid Version 3 proposal: Remove use_zero_index_for_rank_option
Is this ever really used in practice? My guess is no. Although it
could be left in as it's not necessarily a lot of code, but it
does remove some conditional paths.
3a85706
@czarneckid czarneckid Version 3 proposal: Remove use_zero_index_for_rank_option
Is this ever really used in practice? My guess is no. Although it
could be left in as it's not necessarily a lot of code, but it
does remove some conditional paths.
e621eee
@czarneckid czarneckid Rebase from master b05e1ad
@hypomodern
Owner

:+1:

@czarneckid
Owner

This code has been integrated into the version 3 branch.

@czarneckid czarneckid closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 16, 2012
  1. @czarneckid

    Version 3 proposal: Remove use_zero_index_for_rank_option

    czarneckid authored
    Is this ever really used in practice? My guess is no. Although it
    could be left in as it's not necessarily a lot of code, but it
    does remove some conditional paths.
  2. @czarneckid

    Version 3 proposal: Remove use_zero_index_for_rank_option

    czarneckid authored
    Is this ever really used in practice? My guess is no. Although it
    could be left in as it's not necessarily a lot of code, but it
    does remove some conditional paths.
  3. @czarneckid

    Rebase from master

    czarneckid authored
This page is out of date. Refresh to see the latest.
View
8 README.markdown
@@ -162,10 +162,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
}
@@ -299,33 +297,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
@@ -370,21 +358,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
@@ -395,11 +381,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.
@@ -762,23 +746,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
7 spec/leaderboard_spec.rb
@@ -67,7 +67,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
@@ -268,8 +267,7 @@
@leaderboard.member_at(26)[:rank].should eql(26)
@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 eql({'member_name' => 'Leaderboard member 50'})
- @leaderboard.member_at(1, :use_zero_index_for_rank => true)[:rank].should eql(0)
+ @leaderboard.member_at(1, :with_member_data => true)[:member_data].should == {'member_name' => 'Leaderboard member 50'}
end
it 'should return the correct information when calling around_me' do
@@ -313,8 +311,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
7 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
@@ -122,8 +121,7 @@
@leaderboard.member_at(26)[:rank].should eql(26)
@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 eql({'member_name' => 'Leaderboard member 1'})
- @leaderboard.member_at(1, :use_zero_index_for_rank => true)[:rank].should eql(0)
+ @leaderboard.member_at(1, :with_member_data => true)[:member_data].should == {'member_name' => 'Leaderboard member 1'}
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)
Something went wrong with that request. Please try again.