Skip to content

Commit

Permalink
Accept Proc as a namespace (#203)
Browse files Browse the repository at this point in the history
Co-authored-by: Lokanadham <lokanadham.m@voonik.com>

Co-authored-by: Lokanadham <lokanadham.m@voonik.com>
  • Loading branch information
fatkodima and lokanadhamm committed Aug 13, 2022
1 parent 62a7f86 commit 640bc92
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## x.y.z

- Accept Proc as a namespace (#203)
- Fix deprecation warning of Redis.current (#189)
- Add support for getex

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ redis_connection.get('ns:foo')
# => nil
```

Redis::Namespace also supports `Proc` as a namespace and will take the result string as namespace at runtime.

```ruby
redis_connection = Redis.new
namespaced_redis = Redis::Namespace.new(Proc.new { Tenant.current_tenant }, redis: redis_connection)
```

Installation
============

Expand Down
12 changes: 6 additions & 6 deletions lib/redis/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,15 @@ def namespace(desired_namespace = nil)
:redis => @redis)
end

@namespace
@namespace.respond_to?(:call) ? @namespace.call : @namespace
end

def full_namespace
redis.is_a?(Namespace) ? "#{redis.full_namespace}:#{namespace}" : namespace.to_s
end

def connection
@redis.connection.tap { |info| info[:namespace] = @namespace }
@redis.connection.tap { |info| info[:namespace] = namespace }
end

def exec
Expand Down Expand Up @@ -541,7 +541,7 @@ def namespaced_block(command, &block)
end

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

case key
when Array
Expand All @@ -550,12 +550,12 @@ def add_namespace(key)
key.keys.each {|k| key[add_namespace(k)] = key.delete(k)}
key
else
"#{@namespace}:#{key}"
"#{namespace}:#{key}"
end
end

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

case key
when Array
Expand All @@ -567,7 +567,7 @@ def rem_namespace(key)
key.each { |k| yielder.yield rem_namespace(k) }
end
else
key.to_s.sub(/\A#{@namespace}:/, '')
key.to_s.sub(/\A#{namespace}:/, '')
end
end

Expand Down
11 changes: 11 additions & 0 deletions spec/redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@
expect(@namespaced.type('counter')).to eq('string')
end

it "should work with Proc namespaces" do
namespace = Proc.new { :dynamic_ns }
namespaced = Redis::Namespace.new(namespace, redis: @redis)

expect(namespaced.get('foo')).to eq(nil)
namespaced.set('foo', 'chris')
expect(namespaced.get('foo')).to eq('chris')
@redis.set('foo', 'bob')
expect(@redis.get('foo')).to eq('bob')
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

0 comments on commit 640bc92

Please sign in to comment.