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
# File 'lib/mock_redis/transaction_wrapper.rb', line 11

def initialize(db)
  @db = db
  @queued_commands = []
  @in_multi = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/mock_redis/transaction_wrapper.rb', line 17

def method_missing(method, *args)
  if @in_multi
    @queued_commands << [method, *args]
    'QUEUED'
  else
    @db.expire_keys
    @db.send(method, *args)
  end
end

Instance Method Details

#discardObject



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

def discard
  unless @in_multi
    raise RuntimeError, "ERR DISCARD without MULTI"
  end
  @in_multi = false
  @queued_commands = []
  'OK'
end

#execObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mock_redis/transaction_wrapper.rb', line 42

def exec
  unless @in_multi
    raise RuntimeError, "ERR EXEC without MULTI"
  end
  @in_multi = false
  responses = @queued_commands.map do |cmd|
    begin
      send(*cmd)
    rescue => e
      e
    end
  end
  @queued_commands = []
  responses
end

#initialize_copy(source) ⇒ Object



27
28
29
30
31
# File 'lib/mock_redis/transaction_wrapper.rb', line 27

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

#multiObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mock_redis/transaction_wrapper.rb', line 58

def multi
  if @in_multi
    raise RuntimeError, "ERR MULTI calls can not be nested"
  end
  @in_multi = true
  if block_given?
    yield(self)
    self.exec
  else
    'OK'
  end
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



71
72
73
# File 'lib/mock_redis/transaction_wrapper.rb', line 71

def unwatch
  'OK'
end

#watch(_) ⇒ Object



75
76
77
# File 'lib/mock_redis/transaction_wrapper.rb', line 75

def watch(_)
  'OK'
end