Module: Redis::Objects::Values::ClassMethods

Defined in:
lib/redis/objects/values.rb

Overview

Class methods that appear in your class when you include Redis::Objects.

Instance Method Summary collapse

Instance Method Details

#value(name, options = {}) ⇒ Object

Define a new simple value. It will function like a regular instance method, so it can be used alongside ActiveRecord, DataMapper, etc.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/redis/objects/values.rb', line 16

def value(name, options={})
  @redis_objects[name.to_sym] = options.merge(:type => :value)
  if options[:global]
    instance_eval <<-EndMethods
      def #{name}
        @#{name} ||= Redis::Value.new(field_key(:#{name}), redis, @redis_objects[:#{name}])
      end
      def #{name}=(value)
        #{name}.value = value
      end
    EndMethods
    class_eval <<-EndMethods
      def #{name}
        self.class.#{name}
      end
      def #{name}=(value)
        self.class.#{name} = value
      end
    EndMethods
  else
    class_eval <<-EndMethods
      def #{name}
        @#{name} ||= Redis::Value.new(field_key(:#{name}), redis, self.class.redis_objects[:#{name}])
      end
      def #{name}=(value)
        #{name}.value = value
      end
    EndMethods
  end
  
end