Skip to content

Commit

Permalink
Merge pull request #51 from dcampano/master
Browse files Browse the repository at this point in the history
Add global_member_data option that allows multiple leaderboards to share the same set of member_data
  • Loading branch information
czarneckid committed Jan 23, 2015
2 parents 3b00260 + 388e14c commit 63a0adc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
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

0 comments on commit 63a0adc

Please sign in to comment.