class NebulousStomp::RedisHandler

A class to deal with talking to Redis via the Redis gem

You shouldn’t need to instantiate this yourself. If you want to use NebulousStomp to talk to redis directly, try NebulousStomp::RedisHelper.

Attributes

redis[R]

This is most likely useless to anything except spec/redis_handler_spec – at least, I hope so…

Public Class Methods

new(connectHash=nil, testRedis=nil) click to toggle source

Initialise an instance of the handler by passing it the connection hash

If no hash is passed, we try and get it from Nebulous::Params

We use the optional testRedis parameter to mock connections to Redis, for testing. It’s probably of no use to anyone else.

# File lib/nebulous_stomp/redis_handler.rb, line 27
def initialize(connectHash=nil, testRedis=nil)
  @redis_hash = connectHash ? connectHash.dup : nil
  @test_redis = testRedis 
  @redis      = nil
end

Public Instance Methods

connect() click to toggle source

Connect to the Redis key/value store. Raise Nebulous error if connection fails.

# File lib/nebulous_stomp/redis_handler.rb, line 36
def connect
  @redis = @test_redis || Redis.new(@redis_hash.dup)

  @redis.client.connect
  fail ConnectionError, "Redis Connection failed" unless @redis.connected?

  self
rescue => err
  raise ConnectionError, err.to_s
end
connected?() click to toggle source

return whether we are connected to Redis.

# File lib/nebulous_stomp/redis_handler.rb, line 62
def connected?
  @redis && @redis.connected?
end
method_missing(meth, *args) click to toggle source

Cover all the other methods on @redis that we are basically forwarding to it. I could use Forwardable here – except that would not allow us to raise Nebulous::ConnectionError if @redis.nil?

Possible candidates for future inclusion: exists, expire, ping

Calls superclass method
# File lib/nebulous_stomp/redis_handler.rb, line 85
def method_missing(meth, *args)
  super unless [:set,:get,:del].include?(meth)
  fail ConnectionError, "Redis not connected, sent #{meth}" unless connected?

  @redis.__send__(meth, *args)
end
connected? → (boolean) click to toggle source

Call @redis.quit if appropriate; raise nothing if we are not connected etc

# File lib/nebulous_stomp/redis_handler.rb, line 53
def quit
  @redis.quit if connected?
  @redis = nil
  self
end
redis_on? → (boolean) click to toggle source

Return whether the Redis is turned “on” in the connect hash, the config file.

The rest of nebulous should just let RedisHandler worry about this detail.

# File lib/nebulous_stomp/redis_handler.rb, line 74
def redis_on?
  @redis_hash && !@redis_hash.empty?
end