class RedisCall::Connection

Public Class Methods

new(host, port) click to toggle source
# 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

Public Instance Methods

call(*args) click to toggle source
# 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
Also aliased as: method_missing
connected?() click to toggle source
# File lib/redis-call/redis_call.rb, line 61
def connected?
  @connection.connected?
end
discard() click to toggle source
# 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
disconnect() click to toggle source
# File lib/redis-call/redis_call.rb, line 65
def disconnect
  @connection.disconnect
end
exec() click to toggle source
# 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?() click to toggle source
# File lib/redis-call/redis_call.rb, line 69
def inside_transaction?
  @multi_depth != 0
end
method_missing(*args)
Alias for: call
multi() { || ... } click to toggle source
# 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) { |result| ... } click to toggle source
# 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