Class: NebulousStomp::RedisHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/nebulous_stomp/redis_handler.rb

Overview

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.

Direct Known Subclasses

RedisHandlerNull

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connectHash = nil, testRedis = nil) ⇒ RedisHandler

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.



27
28
29
30
31
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

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



85
86
87
88
89
90
# 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

Instance Attribute Details

#redisObject (readonly)

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



17
18
19
# File 'lib/nebulous_stomp/redis_handler.rb', line 17

def redis
  @redis
end

Instance Method Details

#connectObject

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



36
37
38
39
40
41
42
43
44
45
# 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?Boolean

return whether we are connected to Redis.

Returns:

  • (Boolean)


62
63
64
# File 'lib/nebulous_stomp/redis_handler.rb', line 62

def connected?
  @redis && @redis.connected?
end

#quitObject

:call-seq:

handler.connected? -> (boolean)

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



53
54
55
56
57
# File 'lib/nebulous_stomp/redis_handler.rb', line 53

def quit
  @redis.quit if connected?
  @redis = nil
  self
end

#redis_on?Boolean

:call-seq:

handler.redis_on? -> (boolean)

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.

Returns:

  • (Boolean)


74
75
76
# File 'lib/nebulous_stomp/redis_handler.rb', line 74

def redis_on?
  @redis_hash && !@redis_hash.empty?
end