File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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+
3643Installation
3744============
3845
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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' )
You can’t perform that action at this time.
0 commit comments