Class: MockRedis::TransactionWrapper
- Inherits:
-
Object
- Object
- MockRedis::TransactionWrapper
show all
- Includes:
- UndefRedisMethods
- Defined in:
- lib/mock_redis/transaction_wrapper.rb
Instance Method Summary
collapse
included
Constructor Details
Returns a new instance of TransactionWrapper.
11
12
13
14
15
16
|
# File 'lib/mock_redis/transaction_wrapper.rb', line 11
def initialize(db)
@db = db
@transaction_futures = []
@multi_stack = []
@multi_block_given = false
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/mock_redis/transaction_wrapper.rb', line 18
def method_missing(method, *args, &block)
if in_multi?
future = MockRedis::Future.new([method, *args])
@transaction_futures << future
if @multi_block_given
future
else
'QUEUED'
end
else
@db.expire_keys
@db.send(method, *args, &block)
end
end
|
Instance Method Details
#discard ⇒ Object
41
42
43
44
45
46
47
48
49
|
# File 'lib/mock_redis/transaction_wrapper.rb', line 41
def discard
unless in_multi?
raise Redis::CommandError, 'ERR DISCARD without MULTI'
end
pop_multi
@transaction_futures = []
'OK'
end
|
#exec ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/mock_redis/transaction_wrapper.rb', line 51
def exec
unless in_multi?
raise Redis::CommandError, 'ERR EXEC without MULTI'
end
pop_multi
return if in_multi?
@multi_block_given = false
responses = @transaction_futures.map do |future|
begin
result = send(*future.command)
future.store_result(result)
result
rescue StandardError => e
e
end
end
@transaction_futures = []
responses
end
|
#in_multi? ⇒ Boolean
73
74
75
|
# File 'lib/mock_redis/transaction_wrapper.rb', line 73
def in_multi?
@multi_stack.any?
end
|
#initialize_copy(source) ⇒ Object
34
35
36
37
38
39
|
# File 'lib/mock_redis/transaction_wrapper.rb', line 34
def initialize_copy(source)
super
@db = @db.clone
@transaction_futures = @transaction_futures.clone
@multi_stack = @multi_stack.clone
end
|
#multi ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/mock_redis/transaction_wrapper.rb', line 85
def multi
if block_given?
push_multi
@multi_block_given = true
begin
yield(self)
exec
rescue StandardError => e
discard
raise e
end
else
raise Redis::CommandError, 'ERR MULTI calls can not be nested' if in_multi?
push_multi
'OK'
end
end
|
#pipelined {|_self| ... } ⇒ Object
103
104
105
|
# File 'lib/mock_redis/transaction_wrapper.rb', line 103
def pipelined
yield(self) if block_given?
end
|
#pop_multi ⇒ Object
81
82
83
|
# File 'lib/mock_redis/transaction_wrapper.rb', line 81
def pop_multi
@multi_stack.pop
end
|
#push_multi ⇒ Object
77
78
79
|
# File 'lib/mock_redis/transaction_wrapper.rb', line 77
def push_multi
@multi_stack.push(@multi_stack.size + 1)
end
|
#respond_to?(method, include_private = false) ⇒ Boolean
7
8
9
|
# File 'lib/mock_redis/transaction_wrapper.rb', line 7
def respond_to?(method, include_private = false)
super || @db.respond_to?(method)
end
|
#unwatch ⇒ Object
107
108
109
|
# File 'lib/mock_redis/transaction_wrapper.rb', line 107
def unwatch
'OK'
end
|
#watch(*_) ⇒ Object
111
112
113
114
115
116
117
|
# File 'lib/mock_redis/transaction_wrapper.rb', line 111
def watch(*_)
if block_given?
yield self
else
'OK'
end
end
|