lib/leaderboard.rb in leaderboard-3.9.0 vs lib/leaderboard.rb in leaderboard-3.10.0
- old
+ new
@@ -39,11 +39,12 @@
# +:sort_by+ :none: The default sort for a call to `ranked_in_list`.
DEFAULT_LEADERBOARD_REQUEST_OPTIONS = {
:with_member_data => false,
:page_size => nil,
:members_only => false,
- :sort_by => :none
+ :sort_by => :none,
+ :include_missing => true
}
# Name of the leaderboard.
attr_reader :leaderboard_name
@@ -350,21 +351,26 @@
# Change the score for a member in the leaderboard by a score delta which can be positive or negative.
#
# @param member [String] Member name.
# @param delta [float] Score change.
- def change_score_for(member, delta)
- change_score_for_member_in(@leaderboard_name, member, delta)
+ # @param member_data [String] Optional member data.
+ def change_score_for(member, delta, member_data = nil)
+ change_score_for_member_in(@leaderboard_name, member, delta, member_data)
end
# Change the score for a member in the named leaderboard by a delta which can be positive or negative.
#
# @param leaderboard_name [String] Name of the leaderboard.
# @param member [String] Member name.
# @param delta [float] Score change.
- def change_score_for_member_in(leaderboard_name, member, delta)
- @redis_connection.zincrby(leaderboard_name, delta, member)
+ # @param member_data [String] Optional member data.
+ def change_score_for_member_in(leaderboard_name, member, delta, member_data)
+ @redis_connection.multi do |transaction|
+ transaction.zincrby(leaderboard_name, delta, member)
+ transaction.hset(member_data_key(leaderboard_name), member, member_data) if member_data
+ end
end
# Retrieve the rank for a member in the leaderboard.
#
# @param member [String] Member name.
@@ -950,9 +956,12 @@
members.each_with_index do |member, index|
data = {}
data[@member_key] = member
unless leaderboard_options[:members_only]
data[@rank_key] = responses[index * 2] + 1 rescue nil
+ if data[@rank_key] == nil
+ next unless leaderboard_options[:include_missing]
+ end
data[@score_key] = responses[index * 2 + 1].to_f if responses[index * 2 + 1]
end
if leaderboard_options[:with_member_data]
data[@member_data_key] = member_data_for_in(leaderboard_name, member)