Sha256: d63a88bfd7994da15c092bdc918aadc18d6e6dec5803b830261753ae7be242ea

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 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)

          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_field_redis(name), redis_objects[name.to_sym]
                  )
                )
            end
            define_method("#{name}=") do |value|
              public_send(name).value = value
            end
          end

          if options[:global]
            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
            include mod
          end
        end
      end

      # Instance methods that appear in your class when you include Redis::Objects.
      module InstanceMethods
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
redis-objects-1.0.1 lib/redis/objects/values.rb
redis-objects-1.0.0 lib/redis/objects/values.rb
redis-objects-0.9.1 lib/redis/objects/values.rb
redis-objects-0.9.0 lib/redis/objects/values.rb