Permalink
Browse files

Override connection parameters from URL with hash options.

  • Loading branch information...
1 parent 312af27 commit 9b906be318740016115b2c68d3799c3762499c9a Michel Martens & Damian Janowski committed Sep 3, 2010
Showing with 15 additions and 6 deletions.
  1. +4 −4 lib/redis.rb
  2. +11 −2 test/redis_test.rb
View
@@ -22,10 +22,10 @@ def self.connect(options = {})
url = URI(options.delete(:url) || ENV["REDIS_URL"] || "redis://127.0.0.1:6379/0")
- options[:host] = url.host
- options[:port] = url.port
- options[:password] = url.password
- options[:db] = url.path[1..-1].to_i
+ options[:host] ||= url.host
+ options[:port] ||= url.port
+ options[:password] ||= url.password
+ options[:db] ||= url.path[1..-1].to_i
new(options)
end
View
@@ -24,7 +24,7 @@ class RedisTest < Test::Unit::TestCase
assert_nil redis.client.password
end
- test "takes a url" do
+ test "takes a URL" do
redis = Redis.connect :url => "redis://:secr3t@foo.com:999/2"
assert_equal "foo.com", redis.client.host
@@ -33,7 +33,16 @@ class RedisTest < Test::Unit::TestCase
assert_equal "secr3t", redis.client.password
end
- test "doesn not modify the passed options" do
+ test "overrides the URL if another connection option is passed" do
+ redis = Redis.connect :url => "redis://:secr3t@foo.com:999/2", :port => 1000
+
+ assert_equal "foo.com", redis.client.host
+ assert_equal 1000, redis.client.port
+ assert_equal 2, redis.client.db
+ assert_equal "secr3t", redis.client.password
+ end
+
+ test "does not modify the passed options" do
options = { :url => "redis://:secr3t@foo.com:999/2" }
redis = Redis.connect(options)

0 comments on commit 9b906be

Please sign in to comment.