Skip to content

Commit c88c8ce

Browse files
committed
Prioritize hash in connection parameter detection
Allow to detect hashes responding to `#call`, like `ActiveSupport::OrderedOptions` This will enable out of the box compatibility with `Rails.application.config_for` method and should not break existing use cases Close #152
1 parent f176e57 commit c88c8ce

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

lib/redlock/client.rb

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,12 @@ class RedisInstance
160160
def initialize(connection)
161161
@monitor = Monitor.new
162162

163-
if connection.respond_to?(:call)
164-
@redis = connection
165-
else
166-
if connection.respond_to?(:client)
167-
@redis = connection
168-
elsif connection.respond_to?(:key?)
169-
@redis = initialize_client(connection)
163+
@redis =
164+
if connection.is_a?(Hash)
165+
initialize_client(connection)
170166
else
171-
@redis = connection
167+
connection
172168
end
173-
end
174169
end
175170

176171
def initialize_client(options)

spec/client_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@
6565
lock_manager.unlock(lock_info)
6666
end
6767

68+
it 'accepts hashes responding to call like ActiveSupport::OrderedOptions' do
69+
callable_hash = Class.new(Hash) do
70+
def call; end
71+
end
72+
73+
config = callable_hash.new.merge(url: "redis://#{redis1_host}:#{redis1_port}")
74+
redlock = Redlock::Client.new([config])
75+
76+
lock_info = redlock.lock(resource_key, ttl)
77+
expect(lock_info).to be_a(Hash)
78+
expect(resource_key).to_not be_lockable(lock_manager, ttl)
79+
redlock.unlock(lock_info)
80+
end
81+
6882
it 'does not load scripts' do
6983
redis_client.call('SCRIPT', 'FLUSH')
7084

0 commit comments

Comments
 (0)