diff --git a/lib/leaderboard.js b/lib/leaderboard.js index d98b7e3..c14d1eb 100644 --- a/lib/leaderboard.js +++ b/lib/leaderboard.js @@ -82,6 +82,7 @@ this.scoreKeyOption = options['scoreKey'] || 'score'; this.memberDataKeyOption = options['memberDataKey'] || 'member_data'; this.memberDataNamespace = options['memberDataNamespace'] || 'member_data'; + this.useGlobalMemberData = options['useGlobalMemberData'] ? true : false; this.redisConnection = redisOptions['redis_connection']; if (this.redisConnection != null) { delete redisOptions['redis_connection']; @@ -1538,7 +1539,11 @@ */ Leaderboard.prototype.memberDataKey = function(leaderboardName) { - return "" + leaderboardName + ":" + this.memberDataNamespace; + if (this.useGlobalMemberData) { + return "" + this.memberDataNamespace; + } else { + return "" + leaderboardName + ":" + this.memberDataNamespace; + } }; return Leaderboard; diff --git a/spec/leaderboard_spec.coffee b/spec/leaderboard_spec.coffee index 5089392..72eaad5 100644 --- a/spec/leaderboard_spec.coffee +++ b/spec/leaderboard_spec.coffee @@ -64,6 +64,23 @@ describe 'Leaderboard', -> reply.should.equal(1) done()) + it 'should allow you to change the memberDataNamespace to global namespace', (done) -> + updated_options = + 'memberDataNamespace': 'global:namespace' + 'useGlobalMemberData': true + + @leaderboard = new Leaderboard('highscores', updated_options) + for index in [0...Leaderboard.DEFAULT_PAGE_SIZE + 1] + @leaderboard.rankMember("member_#{index}", index, "member_data_#{index}", (reply) -> ) + + @leaderboard.redisConnection.exists('highscores:member_data', (err, reply) -> + reply.should.equal(0)) + @leaderboard.redisConnection.exists('highscores:global:namespace', (err, reply) -> + reply.should.equal(0)) + @leaderboard.redisConnection.exists('global:namespace', (err, reply) -> + reply.should.equal(1) + done()) + it 'should allow you to disconnect the Redis connection', (done) -> @leaderboard.disconnect() done() diff --git a/src/leaderboard.coffee b/src/leaderboard.coffee index 508b5b4..f739ec1 100644 --- a/src/leaderboard.coffee +++ b/src/leaderboard.coffee @@ -55,6 +55,7 @@ class Leaderboard @scoreKeyOption = options['scoreKey'] || 'score' @memberDataKeyOption = options['memberDataKey'] || 'member_data' @memberDataNamespace = options['memberDataNamespace'] || 'member_data' + @useGlobalMemberData = if (options['useGlobalMemberData']) then true else false @redisConnection = redisOptions['redis_connection'] @@ -1076,6 +1077,6 @@ class Leaderboard # @return a key in the form of +leaderboardName:member_data+ ### memberDataKey: (leaderboardName) -> - "#{leaderboardName}:#{@memberDataNamespace}" + return if @useGlobalMemberData then "#{@memberDataNamespace}" else "#{leaderboardName}:#{@memberDataNamespace}" module.exports = Leaderboard