lib/redis/objects.rb in redis-objects-0.7.0 vs lib/redis/objects.rb in redis-objects-0.8.0

- old
+ new

@@ -107,52 +107,50 @@ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). downcase end - def redis_field_key(name, id=nil) #:nodoc: + 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 - key = klass.redis_objects[name.to_sym][:key] - if key && key.respond_to?(:call) - key = key.call self + 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 - 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 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 + + def redis_id_field(id=nil) + @redis_id_field = id || @redis_id_field || :id + end end # Instance methods that appear in your class when you include Redis::Objects. module InstanceMethods - def redis() self.class.redis end + # Map up one level to make modular extend/include approach sane + def redis() self.class.redis end + def redis_objects() self.class.redis_objects end + def redis_field_key(name) #:nodoc: - klass = self.class.first_ancestor_with(name) - if key = klass.redis_objects[name.to_sym][:key] - if key.respond_to?(:call) - key.call self - else - eval "%(#{key})" - end - else - if id.nil? and !klass.redis_objects[name.to_sym][:global] - raise NilObjectId, - "Attempt to address redis-object :#{name} on class #{klass.name} with nil id (unsaved record?) [object_id=#{object_id}]" - end - # don't try to refactor into class redis_field_key because fucks up eval context - "#{klass.redis_prefix}:#{id}:#{name}" - end + self.class.redis_field_key(name, send(self.class.redis_id_field), self) end end end end