Class: RedisClassy

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_classy.rb,
lib/redis_classy/version.rb

Constant Summary collapse

KEYLESS_COMMANDS =
[:multi, :pipelined, :exec, :eval, :unwatch]
Error =
Class.new(StandardError)
VERSION =
'2.4.1'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ RedisClassy

Returns a new instance of RedisClassy.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/redis_classy.rb', line 63

def initialize(object)
  @redis = self.class.redis
  @object = object

  case object
  when String, Symbol, Integer
    @key = object.to_s
  else
    if object.respond_to?(:id)
      @key = object.id.to_s
    else
      raise ArgumentError, 'object must be a string, symbol, integer or respond to :id method'
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(command, *args, &block) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/redis_classy.rb', line 81

def method_missing(command, *args, &block)
  if @redis.respond_to?(command)
    case command
    when *KEYLESS_COMMANDS
      @redis.send(command, *args, &block)
    else
      @redis.send(command, @key, *args, &block)
    end
  else
    super
  end
end

Class Attribute Details

.redisObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/redis_classy.rb', line 15

def redis
  @redis ||= begin
    if self == RedisClassy
      # only RedisClassy itself holds the raw non-namespaced Redis instance
      nil
    else
      # subclasses of RedisClassy
      raise Error.new('RedisClassy.redis must be assigned first') if RedisClassy.redis.nil?
      Redis::Namespace.new(self.name, redis: RedisClassy.redis)
    end
  end
end

.singletons_keysObject (readonly)

Singletons



42
43
44
# File 'lib/redis_classy.rb', line 42

def singletons_keys
  @singletons_keys
end

Instance Attribute Details

#keyObject

Instance methods



61
62
63
# File 'lib/redis_classy.rb', line 61

def key
  @key
end

#objectObject

Instance methods



61
62
63
# File 'lib/redis_classy.rb', line 61

def object
  @object
end

#redisObject

Instance methods



61
62
63
# File 'lib/redis_classy.rb', line 61

def redis
  @redis
end

Class Method Details

.method_missing(command, *args, &block) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/redis_classy.rb', line 32

def method_missing(command, *args, &block)
  if @singleton
    new('singleton').send(command, *args, &block)
  else
    super
  end
end

.on(key) ⇒ Object



28
29
30
# File 'lib/redis_classy.rb', line 28

def on(key)
  new(key)
end

.singletonObject



54
55
56
# File 'lib/redis_classy.rb', line 54

def singleton
  @singleton = true
end

.singletons(*args) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/redis_classy.rb', line 44

def singletons(*args)
  args.each do |key|
    @singletons_keys ||= []
    @singletons_keys << key
    define_singleton_method(key) do
      new key
    end
  end
end