lib/redis/objects/values.rb in redis-objects-0.7.0 vs lib/redis/objects/values.rb in redis-objects-0.8.0
- old
+ new
@@ -13,38 +13,37 @@
module ClassMethods
# Define a new simple value. It will function like a regular instance
# method, so it can be used alongside ActiveRecord, DataMapper, etc.
def value(name, options={})
redis_objects[name.to_sym] = options.merge(:type => :value)
- klass_name = '::' + self.name
+
+ mod = Module.new do
+ define_method(name) do
+ instance_variable_get("@#{name}") or
+ instance_variable_set("@#{name}",
+ Redis::Value.new(
+ redis_field_key(name), redis, redis_objects[name.to_sym]
+ )
+ )
+ end
+ define_method("#{name}=") do |value|
+ public_send(name).value = value
+ end
+ end
+
if options[:global]
- instance_eval <<-EndMethods
- def #{name}
- @#{name} ||= Redis::Value.new(redis_field_key(:#{name}), #{klass_name}.redis, #{klass_name}.redis_objects[:#{name}])
- end
- def #{name}=(value)
- #{name}.value = value
- end
- EndMethods
- class_eval <<-EndMethods
- def #{name}
- self.class.#{name}
- end
- def #{name}=(value)
- self.class.#{name} = value
- end
- EndMethods
+ extend mod
+
+ # dispatch to class methods
+ define_method(name) do
+ self.class.public_send(name)
+ end
+ define_method("#{name}=") do |value|
+ self.class.public_send("#{name}=", value)
+ end
else
- class_eval <<-EndMethods
- def #{name}
- @#{name} ||= Redis::Value.new(redis_field_key(:#{name}), #{klass_name}.redis, #{klass_name}.redis_objects[:#{name}])
- end
- def #{name}=(value)
- #{name}.value = value
- end
- EndMethods
+ include mod
end
-
end
end
# Instance methods that appear in your class when you include Redis::Objects.
module InstanceMethods