lib/redis/objects/values.rb in redis-objects-0.2.1 vs lib/redis/objects/values.rb in redis-objects-0.2.2

- old
+ new

@@ -3,30 +3,47 @@ require 'redis/value' class Redis module Objects module Values def self.included(klass) - klass.instance_variable_set('@values', {}) klass.send :include, InstanceMethods klass.extend ClassMethods end # Class methods that appear in your class when you include Redis::Objects. module ClassMethods - attr_reader :values - # 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={}) - @values[name] = options - class_eval <<-EndMethods - def #{name} - @#{name} ||= Redis::Value.new(field_key(:#{name}), redis, self.class.values[:#{name}]) - end - def #{name}=(value) - #{name}.value = value - end - EndMethods + @redis_objects[name] = options.merge(:type => :value) + if options[:global] + instance_eval <<-EndMethods + def #{name} + @#{name} ||= Redis::Value.new(field_key(:#{name}, ''), redis, @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 + else + class_eval <<-EndMethods + def #{name} + @#{name} ||= Redis::Value.new(field_key(:#{name}), redis, self.class.redis_objects[:#{name}]) + end + def #{name}=(value) + #{name}.value = value + end + EndMethods + end + end end # Instance methods that appear in your class when you include Redis::Objects. module InstanceMethods \ No newline at end of file