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

Add global_member_data option that allows multiple leaderboards to share the same set of member_data #51

Merged
merged 1 commit into from
Jan 23, 2015
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
3 changes: 2 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ DEFAULT_OPTIONS = {
:rank_key => :rank,
:score_key => :score,
:member_data_key => :member_data,
:member_data_namespace => 'member_data'
:member_data_namespace => 'member_data',
:global_member_data => false
}
```

Expand Down
8 changes: 5 additions & 3 deletions lib/leaderboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class Leaderboard
:rank_key => :rank,
:score_key => :score,
:member_data_key => :member_data,
:member_data_namespace => 'member_data'
:member_data_namespace => 'member_data',
:global_member_data => false
}

# Default Redis host: localhost
Expand Down Expand Up @@ -80,6 +81,7 @@ def initialize(leaderboard_name, options = DEFAULT_OPTIONS, redis_options = DEFA
@score_key = leaderboard_options[:score_key]
@member_data_key = leaderboard_options[:member_data_key]
@member_data_namespace = leaderboard_options[:member_data_namespace]
@global_member_data = leaderboard_options[:global_member_data]

@redis_connection = redis_options[:redis_connection]
unless @redis_connection.nil?
Expand Down Expand Up @@ -974,7 +976,7 @@ def intersect_leaderboards(destination, keys, options = {:aggregate => :sum})
#
# @return a key in the form of +leaderboard_name:member_data+
def member_data_key(leaderboard_name)
"#{leaderboard_name}:#{@member_data_namespace}"
@global_member_data == false ? "#{leaderboard_name}:#{@member_data_namespace}" : @member_data_namespace
end

# Validate and return the page size. Returns the +DEFAULT_PAGE_SIZE+ if the page size is less than 1.
Expand All @@ -989,4 +991,4 @@ def validate_page_size(page_size)

page_size
end
end
end
10 changes: 9 additions & 1 deletion spec/leaderboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -767,4 +767,12 @@
expect(@redis_connection.exists("name:member_data")).to be_falsey
expect(@redis_connection.exists("name:md")).to be_truthy
end
end

it 'should allow you to have a global member data namespace' do
@leaderboard = Leaderboard.new('name', {:global_member_data => true}, {:host => "127.0.0.1", :db => 15})
rank_members_in_leaderboard

expect(@redis_connection.exists("member_data")).to be_truthy
expect(@redis_connection.exists("name:member_data")).to be_falsey
end
end