From 3d8e8ab4d81816168d21f73e742c6960378ea81f Mon Sep 17 00:00:00 2001 From: Travis Date: Thu, 19 Jul 2012 11:51:08 -0700 Subject: [PATCH 1/3] Can take string keys in constructor. Nice for loading config files from yaml or json. --- lib/redis/client.rb | 20 ++++++++++++++++++++ test/internals_test.rb | 27 +++++++++++++++++++++++++++ test/url_param_test.rb | 2 +- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/redis/client.rb b/lib/redis/client.rb index f108cbac0..40ce76145 100644 --- a/lib/redis/client.rb +++ b/lib/redis/client.rb @@ -57,6 +57,21 @@ def initialize(options = {}) @command_map = {} end + # convert all keys to symbols, returning new hash + # inspired by activesupport + def symbolize_keys(hash) + keys = hash.keys + ret = {} + keys.each do |key| + v = hash[key] + if v.is_a?(Hash) + v = symbolize_keys(v) + end + ret[(key.to_sym rescue key) || key] = v + end + ret + end + def connect @pid = Process.pid @@ -294,6 +309,7 @@ def ensure_connected end def _parse_options(options) + options = symbolize_keys(options) defaults = DEFAULTS.dup url = options[:url] || ENV["REDIS_URL"] @@ -319,6 +335,10 @@ def _parse_options(options) end options = defaults.merge(options) + options = symbolize_keys(options) + p options + p options[:port] + p options["port"] if options[:path] options[:scheme] = "unix" diff --git a/test/internals_test.rb b/test/internals_test.rb index 64999874f..22e87323d 100644 --- a/test/internals_test.rb +++ b/test/internals_test.rb @@ -67,11 +67,38 @@ def test_redis_current assert_equal 1, Redis.current.client.db end + def test_redis_current_string_opts + # set in test above + assert_equal "127.0.0.1", Redis.current.client.host + assert_equal 6380, Redis.current.client.port + assert_equal 1, Redis.current.client.db + + Redis.current = Redis.new(OPTIONS.merge("port" => 6382, "db" => 2)) + + t = Thread.new do + assert_equal "127.0.0.1", Redis.current.client.host + assert_equal 6382, Redis.current.client.port + assert_equal 2, Redis.current.client.db + end + + t.join + + assert_equal "127.0.0.1", Redis.current.client.host + assert_equal 6382, Redis.current.client.port + assert_equal 2, Redis.current.client.db + end + + def test_default_id_with_host_and_port redis = Redis.new(OPTIONS.merge(:host => "host", :port => "1234", :db => 0)) assert_equal "redis://host:1234/0", redis.client.id end + def test_default_id_with_host_and_port_string_opts + redis = Redis.new(OPTIONS.merge("host" => "host2", "port" => "1235", "db" => 1)) + assert_equal "redis://host2:1235/1", redis.client.id + end + def test_default_id_with_host_and_port_and_explicit_scheme redis = Redis.new(OPTIONS.merge(:host => "host", :port => "1234", :db => 0, :scheme => "foo")) assert_equal "redis://host:1234/0", redis.client.id diff --git a/test/url_param_test.rb b/test/url_param_test.rb index 5784338ab..2c395b933 100644 --- a/test/url_param_test.rb +++ b/test/url_param_test.rb @@ -16,7 +16,7 @@ def test_url_defaults_to_______________ end def test_allows_to_pass_in_a_url - redis = Redis.new :url => "redis://:secr3t@foo.com:999/2" + redis = Redis.new "url" => "redis://:secr3t@foo.com:999/2" assert_equal "foo.com", redis.client.host assert_equal 999, redis.client.port From 0a6bc18f289c96ac2507349d2cef76a1b1c87537 Mon Sep 17 00:00:00 2001 From: Travis Date: Thu, 19 Jul 2012 11:52:38 -0700 Subject: [PATCH 2/3] Removed one call to symbolize_keys, wasn't required. --- lib/redis/client.rb | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/redis/client.rb b/lib/redis/client.rb index 40ce76145..ed960c76b 100644 --- a/lib/redis/client.rb +++ b/lib/redis/client.rb @@ -57,21 +57,6 @@ def initialize(options = {}) @command_map = {} end - # convert all keys to symbols, returning new hash - # inspired by activesupport - def symbolize_keys(hash) - keys = hash.keys - ret = {} - keys.each do |key| - v = hash[key] - if v.is_a?(Hash) - v = symbolize_keys(v) - end - ret[(key.to_sym rescue key) || key] = v - end - ret - end - def connect @pid = Process.pid @@ -308,6 +293,22 @@ def ensure_connected end end + # convert all keys to symbols, returning new hash + # inspired by activesupport + def symbolize_keys(hash) + keys = hash.keys + ret = {} + keys.each do |key| + v = hash[key] + if v.is_a?(Hash) + v = symbolize_keys(v) + end + ret[(key.to_sym rescue key) || key] = v + end + ret + end + + def _parse_options(options) options = symbolize_keys(options) defaults = DEFAULTS.dup @@ -335,7 +336,6 @@ def _parse_options(options) end options = defaults.merge(options) - options = symbolize_keys(options) p options p options[:port] p options["port"] From 6b4a9c7fc500b6b48ae067d4f1ad9e4d6048bc8d Mon Sep 17 00:00:00 2001 From: Travis Date: Thu, 19 Jul 2012 11:54:47 -0700 Subject: [PATCH 3/3] Removed debug output. --- lib/redis/client.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/redis/client.rb b/lib/redis/client.rb index ed960c76b..26597ba45 100644 --- a/lib/redis/client.rb +++ b/lib/redis/client.rb @@ -336,9 +336,6 @@ def _parse_options(options) end options = defaults.merge(options) - p options - p options[:port] - p options["port"] if options[:path] options[:scheme] = "unix"