lib/leaderboard.rb in leaderboard-3.0.1 vs lib/leaderboard.rb in leaderboard-3.0.2
- old
+ new
@@ -107,46 +107,46 @@
# Rank a member in the leaderboard.
#
# @param member [String] Member name.
# @param score [float] Member score.
- # @param member_data [Hash] Optional member data.
+ # @param member_data [String] Optional member data.
def rank_member(member, score, member_data = nil)
rank_member_in(@leaderboard_name, member, score, member_data)
end
# Rank a member in the named leaderboard.
#
# @param leaderboard_name [String] Name of the leaderboard.
# @param member [String] Member name.
# @param score [float] Member score.
- # @param member_data [Hash] Optional member data.
+ # @param member_data [String] Optional member data.
def rank_member_in(leaderboard_name, member, score, member_data = nil)
@redis_connection.multi do |transaction|
transaction.zadd(leaderboard_name, score, member)
transaction.hset(member_data_key(leaderboard_name), member, member_data) if member_data
end
end
- # Rank a member in the leaderboard based on execution of the +rank_conditional+.
+ # Rank a member in the leaderboard based on execution of the +rank_conditional+.
#
# The +rank_conditional+ is passed the following parameters:
# member: Member name.
# current_score: Current score for the member in the leaderboard.
# score: Member score.
# member_data: Optional member data.
# leaderboard_options: Leaderboard options, e.g. :reverse => Value of reverse option
#
# @param rank_conditional [lambda] Lambda which must return +true+ or +false+ that controls whether or not the member is ranked in the leaderboard.
# @param member [String] Member name.
- # @param score [String] Member score.
- # @param member_data [Hash] Optional member_data.
+ # @param score [float] Member score.
+ # @param member_data [String] Optional member_data.
def rank_member_if(rank_conditional, member, score, member_data = nil)
rank_member_if_in(@leaderboard_name, rank_conditional, member, score, member_data)
end
- # Rank a member in the named leaderboard based on execution of the +rank_conditional+.
+ # Rank a member in the named leaderboard based on execution of the +rank_conditional+.
#
# The +rank_conditional+ is passed the following parameters:
# member: Member name.
# current_score: Current score for the member in the leaderboard.
# score: Member score.
@@ -154,14 +154,14 @@
# leaderboard_options: Leaderboard options, e.g. :reverse => Value of reverse option
#
# @param leaderboard_name [String] Name of the leaderboard.
# @param rank_conditional [lambda] Lambda which must return +true+ or +false+ that controls whether or not the member is ranked in the leaderboard.
# @param member [String] Member name.
- # @param score [String] Member score.
- # @param member_data [Hash] Optional member_data.
+ # @param score [float] Member score.
+ # @param member_data [String] Optional member_data.
def rank_member_if_in(leaderboard_name, rank_conditional, member, score, member_data = nil)
- current_score = @redis_connection.zscore(leaderboard_name, member)
+ current_score = @redis_connection.zscore(leaderboard_name, member)
current_score = current_score.to_f if current_score
if rank_conditional.call(member, current_score, score, member_data, {:reverse => @reverse})
rank_member_in(leaderboard_name, member, score, member_data)
end
@@ -169,38 +169,38 @@
# Retrieve the optional member data for a given member in the leaderboard.
#
# @param member [String] Member name.
#
- # @return Hash of optional member data.
+ # @return String of optional member data.
def member_data_for(member)
member_data_for_in(@leaderboard_name, member)
end
# Retrieve the optional member data for a given member in the named leaderboard.
#
# @param leaderboard_name [String] Name of the leaderboard.
# @param member [String] Member name.
#
- # @return Hash of optional member data.
+ # @return String of optional member data.
def member_data_for_in(leaderboard_name, member)
@redis_connection.hget(member_data_key(leaderboard_name), member)
end
# Update the optional member data for a given member in the leaderboard.
#
# @param member [String] Member name.
- # @param member_data [Hash] Optional member data.
+ # @param member_data [String] Optional member data.
def update_member_data(member, member_data)
update_member_data_in(@leaderboard_name, member, member_data)
end
# Update the optional member data for a given member in the named leaderboard.
#
# @param leaderboard_name [String] Name of the leaderboard.
# @param member [String] Member name.
- # @param member_data [Hash] Optional member data.
+ # @param member_data [String] Optional member data.
def update_member_data_in(leaderboard_name, member, member_data)
@redis_connection.hset(member_data_key(leaderboard_name), member, member_data)
end
# Remove the optional member data for a given member in the leaderboard.
@@ -334,21 +334,21 @@
end
# Retrieve the rank for a member in the leaderboard.
#
# @param member [String] Member name.
- #
+ #
# @return the rank for a member in the leaderboard.
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.
- #
+ #
# @return the rank for a member in the leaderboard.
def rank_for_in(leaderboard_name, member)
if @reverse
return @redis_connection.zrank(leaderboard_name, member) + 1 rescue nil
else
@@ -370,11 +370,11 @@
# @param leaderboard_name Name of the leaderboard.
# @param member [String] Member name.
#
# @return the score for a member in the leaderboard or +nil+ if the member is not in the leaderboard.
def score_for_in(leaderboard_name, member)
- score = @redis_connection.zscore(leaderboard_name, member)
+ score = @redis_connection.zscore(leaderboard_name, member)
score.to_f if score
end
# Check to see if a member exists in the leaderboard.
#
@@ -420,12 +420,12 @@
end
end
responses[0] = responses[0].to_f if responses[0]
responses[1] = responses[1] + 1 rescue nil
-
- {:member => member, :score => responses[0], :rank => responses[1]}
+
+ {:member => member, :score => responses[0], :rank => responses[1]}
end
# Remove members from the leaderboard in a given score range.
#
# @param min_score [float] Minimum score.
@@ -519,11 +519,14 @@
# expiration out to the keys for the member data.
#
# @param leaderboard_name [String] Name of the leaderboard.
# @param seconds [int] Number of seconds after which the leaderboard will be expired.
def expire_leaderboard_for(leaderboard_name, seconds)
- @redis_connection.expire(leaderboard_name, seconds)
+ @redis_connection.multi do |transaction|
+ transaction.expire(leaderboard_name, seconds)
+ transaction.expire(member_data_key(leaderboard_name), seconds)
+ end
end
# Expire the current leaderboard at a specific UNIX timestamp. Do not use this with
# leaderboards that utilize member data as there is no facility to cascade the
# expiration out to the keys for the member data.
@@ -538,11 +541,14 @@
# expiration out to the keys for the member data.
#
# @param leaderboard_name [String] Name of the leaderboard.
# @param timestamp [int] UNIX timestamp at which the leaderboard will be expired.
def expire_leaderboard_at_for(leaderboard_name, timestamp)
- @redis_connection.expireat(leaderboard_name, timestamp)
+ @redis_connection.multi do |transaction|
+ transaction.expireat(leaderboard_name, timestamp)
+ transaction.expireat(member_data_key(leaderboard_name), timestamp)
+ end
end
# Retrieve a page of leaders from the leaderboard.
#
# @param current_page [int] Page to retrieve from the leaderboard.
@@ -871,13 +877,13 @@
private
# Key for retrieving optional member data.
#
# @param leaderboard_name [String] Name of the leaderboard.
- #
+ #
# @return a key in the form of +leaderboard_name:member_data+
def member_data_key(leaderboard_name)
- "#{leaderboard_name}:member_data"
+ "#{leaderboard_name}:member_data"
end
# Validate and return the page size. Returns the +DEFAULT_PAGE_SIZE+ if the page size is less than 1.
#
# @param page_size [int] Page size.
\ No newline at end of file