Class: MockRedis::TransactionWrapper

Inherits:
Object
  • Object
show all
Includes:
UndefRedisMethods
Defined in:
lib/mock_redis/transaction_wrapper.rb

Instance Method Summary collapse

Methods included from UndefRedisMethods

included

Constructor Details

#initialize(db) ⇒ TransactionWrapper

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 = []
  @in_multi = false
  @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

#discardObject



40
41
42
43
44
45
46
47
48
# File 'lib/mock_redis/transaction_wrapper.rb', line 40

def discard
  unless @in_multi
    raise Redis::CommandError, 'ERR DISCARD without MULTI'
  end
  @in_multi = false
  @multi_block_given = false
  @transaction_futures = []
  'OK'
end

#execObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mock_redis/transaction_wrapper.rb', line 50

def exec
  unless @in_multi
    raise Redis::CommandError, 'ERR EXEC without MULTI'
  end
  @in_multi = false
  @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

#initialize_copy(source) ⇒ Object



34
35
36
37
38
# File 'lib/mock_redis/transaction_wrapper.rb', line 34

def initialize_copy(source)
  super
  @db = @db.clone
  @transaction_futures = @transaction_futures.clone
end

#multiObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mock_redis/transaction_wrapper.rb', line 71

def multi
  if @in_multi
    raise Redis::CommandError, 'ERR MULTI calls can not be nested'
  end
  @in_multi = true
  if block_given?
    @multi_block_given = true
    begin
      yield(self)
      exec
    rescue StandardError => e
      discard
      raise e
    end
  else
    'OK'
  end
end

#pipelined {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



90
91
92
# File 'lib/mock_redis/transaction_wrapper.rb', line 90

def pipelined
  yield(self) if block_given?
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (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

#unwatchObject



94
95
96
# File 'lib/mock_redis/transaction_wrapper.rb', line 94

def unwatch
  'OK'
end

#watch(_) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/mock_redis/transaction_wrapper.rb', line 98

def watch(_)
  if block_given?
    yield self
  else
    'OK'
  end
end