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

#redis_objectsObject


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

def redis_objects
  @redis_objects ||= {}
end

Instance Method Details

#first_ancestor_with(name) ⇒ Object


151
152
153
154
155
156
157
# File 'lib/redis/objects.rb', line 151

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

#redisObject


96
97
98
# File 'lib/redis/objects.rb', line 96

def redis
  @redis || Objects.redis
end

#redis=(conn) ⇒ Object

Enable per-class connections (eg, User and Post can use diff redis-server)


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

def redis=(conn)
  @redis = Objects::ConnectionPoolProxy.proxy_if_needed(conn)
end

#redis_field_key(name, id = nil, context = self) ⇒ Object

:nodoc:


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/redis/objects.rb', line 131

def redis_field_key(name, id=nil, context=self) #: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
  if key = klass.redis_objects[name.to_sym][:key]
    if key.respond_to?(:call)
      key = key.call context
    else
      context.instance_eval "%(#{key})"
    end
  else
    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
    "#{redis_prefix(klass)}:#{id}:#{name}"
  end
end

#redis_field_redis(name) ⇒ Object

:nodoc:


121
122
123
124
125
126
127
128
129
# File 'lib/redis/objects.rb', line 121

def redis_field_redis(name) #:nodoc:
  klass = first_ancestor_with(name)
  override_redis = klass.redis_objects[name.to_sym][:redis]
  if override_redis
    Objects::ConnectionPoolProxy.proxy_if_needed(override_redis)
  else
    self.redis
  end
end

#redis_id_field(id = nil) ⇒ Object


159
160
161
162
163
164
165
166
167
# File 'lib/redis/objects.rb', line 159

def redis_id_field(id=nil)
  @redis_id_field = id || @redis_id_field

  if superclass && superclass.respond_to?(:redis_id_field)
    @redis_id_field ||= superclass.redis_id_field
  end

  @redis_id_field ||= :id
end

#redis_options(name) ⇒ Object


116
117
118
119
# File 'lib/redis/objects.rb', line 116

def redis_options(name)
  klass = first_ancestor_with(name)
  return klass.redis_objects[name.to_sym] || {}
end

#redis_prefix(klass = self) ⇒ Object

:nodoc:


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

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


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

def redis_prefix=(redis_prefix) @redis_prefix = redis_prefix end