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

- (Object) value(name, options = {})

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)

  mod = Module.new do
    define_method(name) do
      instance_variable_get("@#{name}") or
        instance_variable_set("@#{name}",
          Redis::Value.new(
            redis_field_key(name), redis_field_redis(name), redis_objects[name.to_sym]
          )
        )
    end
    define_method("#{name}=") do |value|
      public_send(name).value = value
    end
  end

  if options[:global]
    extend mod

    # dispatch to class methods
    define_method(name) do
      self.class.public_send(name)
    end
    define_method("#{name}=") do |value|
      self.class.public_send("#{name}=", value)
    end
  else
    include mod
  end
end