Sha256: a4b0f9477653d480239deb331d7209e7ab8081d461d7d06c34ab1b2e91fca907

Contents?: true

Size: 1.42 KB

Versions: 5

Compression:

Stored size: 1.42 KB

Contents

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

          mod = Module.new do
            define_method(name) do
              instance_variable_get(ivar_name) or
                instance_variable_set(ivar_name,
                  Redis::Set.new(
                    redis_field_key(name), redis_field_redis(name), redis_options(name)
                  )
                )
            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

5 entries across 5 versions & 1 rubygems

Version Path
redis-objects-1.5.0 lib/redis/objects/sets.rb
redis-objects-1.4.3 lib/redis/objects/sets.rb
redis-objects-1.4.2 lib/redis/objects/sets.rb
redis-objects-1.4.0 lib/redis/objects/sets.rb
redis-objects-1.3.1 lib/redis/objects/sets.rb