Sha256: 262a832112943b890f494aa395c82d0f96924d969a6c3c012d3332825eaf27d0

Contents?: true

Size: 1.12 KB

Versions: 13

Compression:

Stored size: 1.12 KB

Contents

module Toy
  module Collection
    extend ActiveSupport::Concern

    included do
      attr_accessor :model, :name, :options
    end

    def initialize(model, name, *args, &block)
      @model   = model
      @name    = name.to_sym
      @options = args.extract_options!
      @type    = args.shift

      model.send(list_method)[name] = self

      options[:extensions] = modularized_extensions(block, options[:extensions])
      after_initialize
    end

    def type
      @type ||= name.to_s.classify.constantize
    end

    def instance_variable
      @instance_variable ||= :"@_#{name}"
    end

    def new_proxy(owner)
      proxy_class.new(self, owner)
    end

    def extensions
      options[:extensions]
    end

    def eql?(other)
      self.class.eql?(other.class) &&
        model == other.model &&
        name  == other.name
    end
    alias :== :eql?

    private
      def proxy_class
        raise('Not Implemented')
      end

      def modularized_extensions(*extensions)
        extensions.flatten.compact.map do |extension|
          Proc === extension ? Module.new(&extension) : extension
        end
      end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
toystore-0.8.3 lib/toy/collection.rb
toystore-0.8.2 lib/toy/collection.rb
toystore-0.8.1 lib/toy/collection.rb
toystore-0.8.0 lib/toy/collection.rb
toystore-0.7.0 lib/toy/collection.rb
toystore-0.6.6 lib/toy/collection.rb
toystore-0.6.5 lib/toy/collection.rb
toystore-0.6.4 lib/toy/collection.rb
toystore-0.6.3 lib/toy/collection.rb
toystore-0.6.2 lib/toy/collection.rb
toystore-0.6.1 lib/toy/collection.rb
toystore-0.6 lib/toy/collection.rb
toystore-0.5 lib/toy/collection.rb