Module: Redis::Objects::ClassMethods

Defined in:
lib/redis/objects.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#redisObject



80
# File 'lib/redis/objects.rb', line 80

def redis() @redis ||= Objects.redis end

#redis_objectsObject

Returns the value of attribute redis_objects.



79
80
81
# File 'lib/redis/objects.rb', line 79

def redis_objects
  @redis_objects
end

Instance Method Details

#first_ancestor_with(name) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/redis/objects.rb', line 107

def first_ancestor_with(name)
  if redis_objects && redis_objects.key?(name.to_sym)
    self
  elsif superclass && superclass.respond_to?(:redis_objects)
    superclass.first_ancestor_with(name)
  end
end

#redis_field_key(name, id = nil) ⇒ Object

:nodoc:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/redis/objects.rb', line 92

def redis_field_key(name, id=nil) #:nodoc:
  klass = first_ancestor_with(name)
  # READ THIS: This can never ever ever ever change or upgrades will corrupt all data
  # I don't think people were using Proc as keys before (that would create a weird key). Should be ok
  key = klass.redis_objects[name.to_sym][:key]
  if key && key.respond_to?(:call)
    key = key.call self
  end
  if id.nil? and !klass.redis_objects[name.to_sym][:global]
    raise NilObjectId,
      "[#{klass.redis_objects[name.to_sym]}] Attempt to address redis-object :#{name} on class #{klass.name} with nil id (unsaved record?) [object_id=#{object_id}]"
  end
  key || "#{redis_prefix(klass)}:#{id}:#{name}"
end

#redis_prefix(klass = self) ⇒ Object

:nodoc:



84
85
86
87
88
89
90
# File 'lib/redis/objects.rb', line 84

def redis_prefix(klass = self) #:nodoc:
  @redis_prefix ||= klass.name.to_s.
    sub(%r{(.*::)}, '').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    downcase
end

#redis_prefix=(redis_prefix) ⇒ Object

Set the Redis redis_prefix to use. Defaults to model_name



83
# File 'lib/redis/objects.rb', line 83

def redis_prefix=(redis_prefix) @redis_prefix = redis_prefix end