Sha256: 3d0aaa51245f4c3394be679919ebe92f047d115261e98c26323c2cef98b26a4e

Contents?: true

Size: 1.67 KB

Versions: 6

Compression:

Stored size: 1.67 KB

Contents

# This is the class loader, for use as "include Redis::Objects::Hashes"
# For the object itself, see "Redis::Hash"
require 'redis/hash_key'
class Redis
  module Objects
    module Hashes
      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 hash key.  It will function like a regular instance
        # method, so it can be used alongside ActiveRecord, DataMapper, etc.
        def hash_key(name, options={})
          redis_objects[name.to_sym] = options.merge(:type => :dict)
          ivar_name = :"@#{name}"

          mod = Module.new do
            define_method(name) do
              instance_variable_get(ivar_name) or
                instance_variable_set(ivar_name,
                  Redis::HashKey.new(
                    redis_field_key(name), redis_field_redis(name), redis_options(name)
                  )
                )
            end

            define_method(:"#{name}=") do |values|
              hash_key = public_send(name)

              redis.pipelined do
                hash_key.clear
                hash_key.bulk_set(values)
              end
            end
          end

          if options[:global]
            extend mod

            # dispatch to class methods
            define_method(name) do
              self.class.public_send(name)
            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

6 entries across 6 versions & 2 rubygems

Version Path
redis-objects-2.0.0.beta lib/redis/objects/hashes.rb
redis-objects-2.0.0.alpha lib/redis/objects/hashes.rb
redis-objects-1.7.0 lib/redis/objects/hashes.rb
redis-objects-1.6.0 lib/redis/objects/hashes.rb
redis-objects-legacy-1.6.0 lib/redis/objects/hashes.rb
redis-objects-1.5.1 lib/redis/objects/hashes.rb