Module: Redis::Objects::Hashes::ClassMethods

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

Overview

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

Instance Method Summary collapse

Instance Method Details

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

Define a new hash key. 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
# File 'lib/redis/objects/hashes.rb', line 16

def hash_key(name, options={})
  redis_objects[name.to_sym] = options.merge(:type => :dict)
  ivar_name = :"@#{name}"

  mod = Module.new do
    define_method(name) do
      instance_variable_get(ivar_name) or
        instance_variable_set(ivar_name,
          Redis::HashKey.new(
            redis_field_key(name), redis_field_redis(name), redis_options(name)
          )
        )
    end
  end

  if options[:global]
    extend mod

    # dispatch to class methods
    define_method(name) do
      self.class.public_send(name)
    end
  else
    include mod
  end
end