Skip to content

Commit

Permalink
Pass in an existing Redis connection to use that. Fixes #1 - #1
Browse files Browse the repository at this point in the history
  • Loading branch information
David Czarnecki committed Aug 4, 2011
1 parent 3e5890c commit e4122ab
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/leaderboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ def initialize(leaderboard_name, host = DEFAULT_REDIS_HOST, port = DEFAULT_REDIS

@page_size = page_size

@redis_connection = redis_options[:redis_connection]
unless @redis_connection.nil?
redis_options.delete(:redis_connection)
end

redis_options = redis_options.dup
redis_options[:host] ||= @host
redis_options[:port] ||= @port
redis_options[:port] ||= @port

@redis_options = redis_options

@redis_connection = Redis.new(@redis_options)
@redis_connection = Redis.new(@redis_options) if @redis_connection.nil?
end

def page_size=(page_size)
Expand Down
7 changes: 7 additions & 0 deletions test/test_leaderboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ def test_delete_leaderboard
assert_equal false, @redis_connection.exists('name')
end

def test_can_pass_existing_redis_connection_to_initializer
@leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_REDIS_HOST, Leaderboard::DEFAULT_REDIS_PORT, Leaderboard::DEFAULT_PAGE_SIZE, {:redis_connection => @redis_connection})
rank_members_to_leaderboard

assert_equal 1, @redis_connection.info["connected_clients"].to_i
end

def test_rank_member_and_total_members
@leaderboard.rank_member('member', 1)

Expand Down

0 comments on commit e4122ab

Please sign in to comment.