Class: RedisCall::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/redis-call/redis_call.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Connection

Returns a new instance of Connection.



54
55
56
57
58
59
# File 'lib/redis-call/redis_call.rb', line 54

def initialize(host, port)
  @connection = Hiredis::Connection.new
  @connection.connect(host, port)
  
  @multi_depth = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missingObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/redis-call/redis_call.rb', line 92

def call *args
           @connection.write(args)
  result = @connection.read
  
  @call_index += 1 if @call_index

  raise result if result.is_a?(Exception)
  
  result
  
rescue RuntimeError => exception
  if exception.message == "not connected"
    raise(IOError, "Not connected")
  else
    raise(exception)
  end
end

Instance Method Details

#call(*args) ⇒ Object Also known as: method_missing



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/redis-call/redis_call.rb', line 74

def call *args
           @connection.write(args)
  result = @connection.read
  
  @call_index += 1 if @call_index

  raise result if result.is_a?(Exception)
  
  result
  
rescue RuntimeError => exception
  if exception.message == "not connected"
    raise(IOError, "Not connected")
  else
    raise(exception)
  end
end

#connected?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/redis-call/redis_call.rb', line 61

def connected?
  @connection.connected?
end

#discardObject



134
135
136
137
138
139
140
141
142
# File 'lib/redis-call/redis_call.rb', line 134

def discard
  if (@multi_depth -= 1) == 0
    begin
      call(:DISCARD) if @connection.connected?
    ensure
      @call_index = @queued_handlers = nil
    end
  end
end

#disconnectObject



65
66
67
# File 'lib/redis-call/redis_call.rb', line 65

def disconnect
  @connection.disconnect
end

#execObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/redis-call/redis_call.rb', line 103

def exec
  if (@multi_depth -= 1) == 0
    begin
      unless result = call(:EXEC)
        raise RedisCall::TransactionAborted
      end
      
      drop_results = []

      @queued_handlers.each do |index, handlers|
        result[index] = handlers.inject(result[index]) do |data, handler|
          if handler
            handler.call(data)
          else
            drop_results.push index
            data
          end
        end
      end
      
      drop_results.each {|index| result.delete_at index }
      
      (result.length == 1) ? result.first : result
      
    ensure
      @call_index = @queued_handlers = nil
    end
  end
end

#inside_transaction?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/redis-call/redis_call.rb', line 69

def inside_transaction?
  @multi_depth != 0
end

#multiObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/redis-call/redis_call.rb', line 145

def multi
  call(:MULTI) if (@multi_depth += 1) == 1
  
  @call_index = -1
  @queued_handlers = {}
  
  if block_given?
    begin
      yield
    rescue ScriptError, StandardError => exception
      begin
        discard
      rescue ScriptError, StandardError => discard_exception
        # It is not important to report this error
        discard_exception.report! if discard_exception.respond_to? :report!
      ensure
        raise exception
      end
    end
    exec
  end
end

#queued(result, &block) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/redis-call/redis_call.rb', line 95

def queued result, &block
  if @queued_handlers
    (@queued_handlers[@call_index] ||= []).push(block)
  else
    yield(result)
  end
end