Sha256: d5f07fbaf9a7505f45570681b54b5970b2b3c8088dc00889b828b192c651922d

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

require "descendants_describable/version"

module DescendantsDescribable
  extend ActiveSupport::Concern

  module ClassMethods
    def describe_descendants_with(description_module, &block)
      DescendantsDescriptor.new(self, description_module).instance_exec &block
    end
  end


  class DescendantsDescriptor

    attr_accessor :new_class

    def initialize(parent, description_module)
      @common_modules = []
      @parent = parent
      @description_module = description_module
    end

    def add_module(mod)
      self.new_class.send(:include, mod)
    end

    def type(name)
      self.new_class = Class.new(@parent)

      @common_modules.each { |m| self.new_class.send(:include, m) } if @common_modules.any?

      yield if block_given?

      Object.const_set(name.to_s.camelize, self.new_class)
      self.new_class = nil
    end

    def method_missing(method, *args)
      if self.new_class.present?
        add_module @description_module.const_get(method.to_s.camelize)
      else
        @common_modules << @description_module.const_get(method.to_s.camelize)
        yield if block_given?
        @common_modules.shift
      end
    end

    def respond_to?(method)
      @description_module.const_get(method.to_s.camelize) rescue false
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
descendants_describable-0.0.1 lib/descendants_describable.rb