Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added total_scores method #63

Merged
merged 1 commit into from
Feb 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ Competition ranking: The `CompetitionRankingLeaderboard` subclass of `Leaderboar
total_members: Total # of members in the leaderboard
total_pages: Total # of pages in the leaderboard given the leaderboard's page_size
total_members_in_score_range(min_score, max_score): Count the number of members within a score range in the leaderboard
total_scores: Sum of scores for all members in leaderboard
change_score_for(member, delta): Change the score for a member by some amount delta (delta could be positive or negative)
rank_for(member): Retrieve the rank for a given member in the leaderboard
score_for(member): Retrieve the score for a given member in the leaderboard
Expand Down
7 changes: 7 additions & 0 deletions lib/leaderboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,13 @@ def total_members_in_score_range_in(leaderboard_name, min_score, max_score)
@redis_connection.zcount(leaderboard_name, min_score, max_score)
end

# Sum of scores for all members in leaderboard
#
# @return Sum of scores for all members in leaderboard
def total_scores
all_leaders.map{|hash| hash[:score] }.inject(0, :+)
end

# Change the score for a member in the leaderboard by a score delta which can be positive or negative.
#
# @param member [String] Member name.
Expand Down
6 changes: 6 additions & 0 deletions spec/leaderboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -855,4 +855,10 @@
leaders = @leaderboard.ranked_in_list(['member_200'], :include_missing => false, :with_member_data => true)
expect(leaders.size).to be(0)
end

it 'should return the sum of all scores in the leaderboard' do
rank_members_in_leaderboard(25)

expect(@leaderboard.total_scores()).to eq(325)
end
end