Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

namespace nil key #107

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/redis/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Namespace
# MSET key1 value1 key2 value2 =>
# MSET namespace:key1 value1 namespace:key2 value2
# :sort
# Add namespace to first argument if it is non-nil
# Add namespace to first argument
# Add namespace to second arg's :by and :store if second arg is a Hash
# Add namespace to each element in second arg's :get if second arg is
# a Hash; forces second arg's :get to be an Array if present.
Expand Down Expand Up @@ -366,7 +366,7 @@ def call_with_namespace(command, *args, &block)
# Add the namespace to any parameters that are keys.
case before
when :first
args[0] = add_namespace(args[0]) if args[0]
args[0] = add_namespace(args[0])
when :all
args = add_namespace(args)
when :exclude_first
Expand All @@ -388,7 +388,7 @@ def call_with_namespace(command, *args, &block)
when :alternate
args.each_with_index { |a, i| args[i] = add_namespace(a) if i.even? }
when :sort
args[0] = add_namespace(args[0]) if args[0]
args[0] = add_namespace(args[0])
if args[1].is_a?(Hash)
[:by, :store].each do |key|
args[1][key] = add_namespace(args[1][key]) if args[1][key]
Expand Down Expand Up @@ -464,7 +464,7 @@ def namespaced_block(command, &block)
end

def add_namespace(key)
return key unless key && @namespace
return key unless @namespace

case key
when Array
Expand Down
12 changes: 12 additions & 0 deletions spec/redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
@namespaced['foo'].should eq(nil)
@namespaced['foo'] = 'chris'
@namespaced['foo'].should eq('chris')
@redis['foo'].should eq('bar')
@redis['foo'] = 'bob'
@redis['foo'].should eq('bob')

Expand All @@ -41,6 +42,17 @@
@namespaced.type('counter').should eq('string')
end

context 'when key is nil' do
it 'should still be able to use a namespace' do
@namespaced[nil].should eq(nil)
@namespaced[nil] = 'chris'
@namespaced[nil].should eq('chris')
@redis[nil].should eq(nil)
@redis[nil] = 'bob'
@redis[nil].should eq('bob')
end
end

context 'when sending capital commands (issue 68)' do
it 'should be able to use a namespace' do
@namespaced.send('SET', 'fubar', 'quux')
Expand Down