Skip to content

Commit 640bc92

Browse files
Accept Proc as a namespace (#203)
Co-authored-by: Lokanadham <lokanadham.m@voonik.com> Co-authored-by: Lokanadham <lokanadham.m@voonik.com>
1 parent 62a7f86 commit 640bc92

4 files changed

Lines changed: 25 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## x.y.z
44

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

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ redis_connection.get('ns:foo')
3333
# => nil
3434
```
3535

36+
Redis::Namespace also supports `Proc` as a namespace and will take the result string as namespace at runtime.
37+
38+
```ruby
39+
redis_connection = Redis.new
40+
namespaced_redis = Redis::Namespace.new(Proc.new { Tenant.current_tenant }, redis: redis_connection)
41+
```
42+
3643
Installation
3744
============
3845

lib/redis/namespace.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,15 @@ def namespace(desired_namespace = nil)
309309
:redis => @redis)
310310
end
311311

312-
@namespace
312+
@namespace.respond_to?(:call) ? @namespace.call : @namespace
313313
end
314314

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

319319
def connection
320-
@redis.connection.tap { |info| info[:namespace] = @namespace }
320+
@redis.connection.tap { |info| info[:namespace] = namespace }
321321
end
322322

323323
def exec
@@ -541,7 +541,7 @@ def namespaced_block(command, &block)
541541
end
542542

543543
def add_namespace(key)
544-
return key unless key && @namespace
544+
return key unless key && namespace
545545

546546
case key
547547
when Array
@@ -550,12 +550,12 @@ def add_namespace(key)
550550
key.keys.each {|k| key[add_namespace(k)] = key.delete(k)}
551551
key
552552
else
553-
"#{@namespace}:#{key}"
553+
"#{namespace}:#{key}"
554554
end
555555
end
556556

557557
def rem_namespace(key)
558-
return key unless key && @namespace
558+
return key unless key && namespace
559559

560560
case key
561561
when Array
@@ -567,7 +567,7 @@ def rem_namespace(key)
567567
key.each { |k| yielder.yield rem_namespace(k) }
568568
end
569569
else
570-
key.to_s.sub(/\A#{@namespace}:/, '')
570+
key.to_s.sub(/\A#{namespace}:/, '')
571571
end
572572
end
573573

spec/redis_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@
4141
expect(@namespaced.type('counter')).to eq('string')
4242
end
4343

44+
it "should work with Proc namespaces" do
45+
namespace = Proc.new { :dynamic_ns }
46+
namespaced = Redis::Namespace.new(namespace, redis: @redis)
47+
48+
expect(namespaced.get('foo')).to eq(nil)
49+
namespaced.set('foo', 'chris')
50+
expect(namespaced.get('foo')).to eq('chris')
51+
@redis.set('foo', 'bob')
52+
expect(@redis.get('foo')).to eq('bob')
53+
end
54+
4455
context 'when sending capital commands (issue 68)' do
4556
it 'should be able to use a namespace' do
4657
@namespaced.send('SET', 'fubar', 'quux')

0 commit comments

Comments
 (0)