Sha256: d486436821f40eb6c5becaa460d3697a127e6242e6fc5190b10efe1678dd66b8

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require 'redis/objects'

class Redis
  autoload :Collection,   'redis/collection'
  
  module Collections

    def self.included(klass)
      klass.extend ClassMethods
      klass.send :include, Redis::Objects
    end

    module ClassMethods

      def collection(name, options={})
        redis_objects[name.to_sym] = options.merge(:type => :collection)
        klass_name = '::' + self.name
        if options[:global]
          instance_eval <<-EndMethods
            def #{name}
              @#{name} ||= Redis::Collection.new(redis_field_key(:#{name}), #{klass_name}.redis, #{klass_name}.redis_objects[:#{name}])
            end
          EndMethods
          class_eval <<-EndMethods
            def #{name}
              self.class.#{name}
            end
          EndMethods
        else
          class_eval <<-EndMethods
            def #{name}
              @#{name} ||= Redis::Collection.new(redis_field_key(:#{name}), #{klass_name}.redis, #{klass_name}.redis_objects[:#{name}])
            end
          EndMethods
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redis-collections-0.1.0 lib/redis/collections.rb