Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial implementation of storing extra data for leaderboard members. #…

  • Loading branch information...
commit 5832a55fdf955d54921b85eeae6adb5eba0690f5 1 parent 2e4d049
@jgadbois jgadbois authored czarneckid committed
View
28 lib/leaderboard.rb
@@ -34,6 +34,7 @@ class Leaderboard
DEFAULT_LEADERBOARD_REQUEST_OPTIONS = {
:with_scores => true,
:with_rank => true,
+ :with_data => false,
:use_zero_index_for_rank => false,
:page_size => nil
}
@@ -108,8 +109,8 @@ def delete_leaderboard_named(leaderboard_name)
#
# @param member [String] Member name.
# @param score [float] Member score.
- def rank_member(member, score)
- rank_member_in(@leaderboard_name, member, score)
+ def rank_member(member, score, data=nil)
+ rank_member_in(@leaderboard_name, member, score, data)
end
# Rank a member in the named leaderboard.
@@ -117,8 +118,23 @@ def rank_member(member, score)
# @param leaderboard_name [String] Name of the leaderboard.
# @param member [String] Member name.
# @param score [float] Member score.
- def rank_member_in(leaderboard_name, member, score)
+ def rank_member_in(leaderboard_name, member, score, data)
@redis_connection.zadd(leaderboard_name, score, member)
+ add_member_data_in(leaderboard_name, member, data)
+ end
+
+ def add_member_data_in(leaderboard_name, member, data={})
+ data.each do |key,val|
+ @redis_connection.hset(data_key_for_member_in(leaderboard_name, member), key, val)
+ end if data
+ end
+
+ def data_for_member_in(leaderboard_name, member)
+ @redis_connection.hgetall(data_key_for_member_in(leaderboard_name, member))
+ end
+
+ def data_key_for_member_in(leaderboard_name, member)
+ "#{leaderboard_name}:data:#{member}"
end
# Rank an array of members in the leaderboard.
@@ -157,6 +173,7 @@ def remove_member(member)
# @param member [String] Member name.
def remove_member_from(leaderboard_name, member)
@redis_connection.zrem(leaderboard_name, member)
+ @redis_connection.del(data_key_for_member_in(leaderboard_name, member))
end
# Retrieve the total number of members in the leaderboard.
@@ -574,6 +591,11 @@ def ranked_in_list_in(leaderboard_name, members, options = {})
end
end
end
+
+ if leaderboard_options[:with_data]
+ extra_data = data_for_member_in(leaderboard_name, member)
+ data[:data] = extra_data if extra_data && extra_data.size > 0
+ end
ranks_for_members << data
end
View
1  spec/db/redis.pid
@@ -0,0 +1 @@
+82118
View
14 spec/leaderboard_spec.rb
@@ -139,6 +139,20 @@
leaders[24].should == member_1
end
+ it 'should allow you to retrieve leaders with extra data' do
+ Time.stub(:now).and_return(123)
+ 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_data=>true})
+
+ member_25 = {:member => 'member_25', :data=>{ "date"=>"123" }}
+ leaders[0].should == member_25
+
+ member_1 = {:member => 'member_1', :data=>{ "date"=>"123" }}
+ leaders[24].should == member_1
+ 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)
View
2  spec/spec_helper.rb
@@ -9,7 +9,7 @@
# @param members_to_add [int] Number of members to add to the leaderboard.
def rank_members_in_leaderboard(members_to_add = 5)
1.upto(members_to_add) do |index|
- @leaderboard.rank_member("member_#{index}", index)
+ @leaderboard.rank_member("member_#{index}", index, { :date => Time.now.to_i })
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.