Skip to content

Commit

Permalink
Handle block within multi block (#185)
Browse files Browse the repository at this point in the history
* Handle block within multi block

* Fix RuboCop warnings

Co-authored-by: Hieu Nguyen <hieuk09@gmail.com>
  • Loading branch information
sds and hieuk09 committed Apr 27, 2020
1 parent 80c1297 commit 09747cb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/mock_redis/future.rb
Expand Up @@ -17,7 +17,7 @@ def value

def store_result(result)
@result_set = true
@result = result
@result = @block ? @block.call(result) : result
end
end
end
4 changes: 2 additions & 2 deletions lib/mock_redis/transaction_wrapper.rb
Expand Up @@ -17,7 +17,7 @@ def initialize(db)

def method_missing(method, *args, &block)
if in_multi?
future = MockRedis::Future.new([method, *args])
future = MockRedis::Future.new([method, *args], block)
@transaction_futures << future

if @multi_block_given
Expand Down Expand Up @@ -60,7 +60,7 @@ def exec
begin
result = send(*future.command)
future.store_result(result)
result
future.value
rescue StandardError => e
e
end
Expand Down
12 changes: 11 additions & 1 deletion spec/commands/future_spec.rb
Expand Up @@ -3,7 +3,12 @@
describe MockRedis::Future do
let(:command) { [:get, 'foo'] }
let(:result) { 'bar' }
before { @future = MockRedis::Future.new(command) }
let(:block) { ->(value) { value.upcase } }

before do
@future = MockRedis::Future.new(command)
@future2 = MockRedis::Future.new(command, block)
end

it 'remembers the command' do
@future.command.should eq(command)
Expand All @@ -17,4 +22,9 @@
@future.store_result(result)
@future.value.should eq(result)
end

it 'executes the block on the value if block is passed in' do
@future2.store_result(result)
@future2.value.should eq('BAR')
end
end
16 changes: 16 additions & 0 deletions spec/transactions_spec.rb
Expand Up @@ -67,6 +67,22 @@
@redises.get('counter').should eq '6'
@redises.get('test').should eq '1'
end

it 'allows blocks within multi blocks' do
@redises.set('foo', 'bar')
@redises.set('fuu', 'baz')

result = nil

@redises.multi do |r|
result = r.mget('foo', 'fuu') { |reply| reply.map(&:upcase) }
r.del('foo', 'fuu')
end

result.value.should eq %w[BAR BAZ]
@redises.get('foo').should eq nil
@redises.get('fuu').should eq nil
end
end

context '#discard' do
Expand Down

0 comments on commit 09747cb

Please sign in to comment.