Class: RedisClassy
- Inherits:
-
Object
show all
- Defined in:
- lib/redis_classy.rb,
lib/redis_classy/version.rb
Constant Summary
collapse
- KEYLESS_COMMANDS =
[:multi, :pipelined, :exec, :eval]
- Error =
Class.new(StandardError)
- VERSION =
'2.2.0'
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of RedisClassy.
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/redis_classy.rb', line 55
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
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/redis_classy.rb', line 73
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
.redis ⇒ Object
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/redis_classy.rb', line 15
def redis
@redis ||= begin
if self == RedisClassy
nil
else
raise Error.new('RedisClassy.redis is not assigned') if RedisClassy.redis.nil?
Redis::Namespace.new(self.name, redis: RedisClassy.redis)
end
end
end
|
Instance Attribute Details
#key ⇒ Object
53
54
55
|
# File 'lib/redis_classy.rb', line 53
def key
@key
end
|
#object ⇒ Object
53
54
55
|
# File 'lib/redis_classy.rb', line 53
def object
@object
end
|
#redis ⇒ Object
53
54
55
|
# File 'lib/redis_classy.rb', line 53
def redis
@redis
end
|
Class Method Details
.method_missing(command, *args, &block) ⇒ Object
42
43
44
45
46
47
48
|
# File 'lib/redis_classy.rb', line 42
def method_missing(command, *args, &block)
if @singleton
new('singleton').send(command, *args, &block)
else
super
end
end
|
.on(key) ⇒ Object
38
39
40
|
# File 'lib/redis_classy.rb', line 38
def on(key)
new(key)
end
|
.singleton ⇒ Object
34
35
36
|
# File 'lib/redis_classy.rb', line 34
def singleton
@singleton = true
end
|
.singletons(*args) ⇒ Object
26
27
28
29
30
31
32
|
# File 'lib/redis_classy.rb', line 26
def singletons(*args)
args.each do |key|
define_singleton_method(key) do
new key
end
end
end
|