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



92
93
94
# File 'lib/redis/objects.rb', line 92

def redis
  @redis || Objects.redis
end

#redis_objectsObject



98
99
100
# File 'lib/redis/objects.rb', line 98

def redis_objects
  @redis_objects ||= {}
end

Instance Method Details

#first_ancestor_with(name) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/redis/objects.rb', line 127

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:



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/redis/objects.rb', line 112

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:



104
105
106
107
108
109
110
# File 'lib/redis/objects.rb', line 104

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



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

def redis_prefix=(redis_prefix) @redis_prefix = redis_prefix end