Sha256: 0a61fa38a73e8a878fc09239adc782232abfb1bc5a5bcd109777def33f3d6ca2

Contents?: true

Size: 1.8 KB

Versions: 7

Compression:

Stored size: 1.8 KB

Contents

# This is the class loader, for use as "include Redis::Objects::Values"
# For the object itself, see "Redis::Value"
require 'redis/value'
class Redis
  module Objects
    module Values
      def self.included(klass)
        klass.send :include, InstanceMethods
        klass.extend ClassMethods
      end

      # Class methods that appear in your class when you include Redis::Objects.
      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
          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
          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
          end
          
        end
      end
      
      # Instance methods that appear in your class when you include Redis::Objects.
      module InstanceMethods
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
redis-objects-0.6.1 lib/redis/objects/values.rb
redis-objects-0.6.0 lib/redis/objects/values.rb
redis-objects-0.5.3 lib/redis/objects/values.rb
redis-objects-0.5.2 lib/redis/objects/values.rb
redis-objects-0.5.1 lib/redis/objects/values.rb
redis-objects-0.5.0 lib/redis/objects/values.rb
redis-objects-0.4.1 lib/redis/objects/values.rb