Class: MockRedis::TransactionWrapper
- Inherits:
-
Object
- Object
- MockRedis::TransactionWrapper
- Includes:
- UndefRedisMethods
- Defined in:
- lib/mock_redis/transaction_wrapper.rb
Instance Method Summary collapse
- #discard ⇒ Object
- #exec ⇒ Object
- #in_multi? ⇒ Boolean
-
#initialize(db) ⇒ TransactionWrapper
constructor
A new instance of TransactionWrapper.
- #initialize_copy(source) ⇒ Object
- #multi ⇒ Object
- #pipelined {|_self| ... } ⇒ Object
- #pop_multi ⇒ Object
- #push_multi ⇒ Object
- #respond_to?(method, include_private = false) ⇒ Boolean
- #unwatch ⇒ Object
- #watch(*_) ⇒ Object
Methods included from UndefRedisMethods
Constructor Details
#initialize(db) ⇒ TransactionWrapper
Returns a new instance of TransactionWrapper.
12 13 14 15 16 |
# File 'lib/mock_redis/transaction_wrapper.rb', line 12 def initialize(db) @db = db @transaction_futures = [] @multi_stack = [] end |
Instance Method Details
#discard ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/mock_redis/transaction_wrapper.rb', line 36 def discard unless in_multi? raise Error.command_error('ERR DISCARD without MULTI', self) end pop_multi @transaction_futures = [] 'OK' end |
#exec ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/mock_redis/transaction_wrapper.rb', line 46 def exec unless in_multi? raise Error.command_error('ERR EXEC without MULTI', self) end pop_multi return if in_multi? responses = @transaction_futures.map do |future| result = send(*future.command) future.store_result(result) future.value end responses ensure # At this point, multi is done, so we can't call discard anymore. # Therefore, we need to clear the transaction futures manually. @transaction_futures = [] end |
#in_multi? ⇒ Boolean
67 68 69 |
# File 'lib/mock_redis/transaction_wrapper.rb', line 67 def in_multi? @multi_stack.any? end |
#initialize_copy(source) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/mock_redis/transaction_wrapper.rb', line 29 def initialize_copy(source) super @db = @db.clone @transaction_futures = @transaction_futures.clone @multi_stack = @multi_stack.clone end |
#multi ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/mock_redis/transaction_wrapper.rb', line 79 def multi raise Redis::BaseError, "Can't nest multi transaction" if in_multi? push_multi begin yield(self) exec rescue StandardError => e discard if in_multi? raise e end end |
#pipelined {|_self| ... } ⇒ Object
93 94 95 |
# File 'lib/mock_redis/transaction_wrapper.rb', line 93 def pipelined yield(self) if block_given? end |
#pop_multi ⇒ Object
75 76 77 |
# File 'lib/mock_redis/transaction_wrapper.rb', line 75 def pop_multi @multi_stack.pop end |
#push_multi ⇒ Object
71 72 73 |
# File 'lib/mock_redis/transaction_wrapper.rb', line 71 def push_multi @multi_stack.push(@multi_stack.size + 1) end |
#respond_to?(method, include_private = false) ⇒ Boolean
8 9 10 |
# File 'lib/mock_redis/transaction_wrapper.rb', line 8 def respond_to?(method, include_private = false) super || @db.respond_to?(method) end |
#unwatch ⇒ Object
97 98 99 |
# File 'lib/mock_redis/transaction_wrapper.rb', line 97 def unwatch 'OK' end |
#watch(*_) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/mock_redis/transaction_wrapper.rb', line 101 def watch(*_) if block_given? yield self else 'OK' end end |