Class: Redis::Attrs::Complex

Inherits:
Base
  • Object
show all
Defined in:
lib/redis-attrs/complex.rb

Direct Known Subclasses

Hash, List

Instance Attribute Summary

Attributes inherited from Base

#klass, #name, #options, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#redis, #redis_key

Constructor Details

#initialize(klass, name, type, options) ⇒ Complex

Returns a new instance of Complex.


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/redis-attrs/complex.rb', line 13

def initialize(klass, name, type, options)
  super
  attr = self
  attr_class = self.class.redis_object_class[type]

  # Define the getter
  klass.send(:define_method, name) do
    instance_variable_get("@#{name}") || begin
      obj = attr_class.new attr.redis_key(id), redis, options
      instance_variable_set("@#{name}", obj)
    end
  end

  # TODO: Add support for collection setters
  if self.respond_to?(:setter)
    klass.send(:define_method, "#{name}=") do |value|
      obj = send(name)
      attr.setter(id, obj, value)
    end
  end
end

Class Method Details

.redis_object_classObject


35
36
37
38
39
40
41
42
43
44
# File 'lib/redis-attrs/complex.rb', line 35

def self.redis_object_class
  @@redis_object_class ||= {
    lock:       Redis::Lock,
    counter:    Redis::Counter,
    hash:       Redis::HashKey,
    list:       Redis::Attrs::FilteredList,
    set:        Redis::Attrs::FilteredSet,
    sorted_set: Redis::SortedSet,
  }
end