class DescendantsDescribable::DescendantsDescriptor

Attributes

new_class[RW]

Public Class Methods

new(parent, description_module, description_proc = -> {} click to toggle source
# File lib/descendants_describable.rb, line 20
def initialize(parent, description_module, description_proc = -> {})
  @common_modules = []
  @descendant_names = []
  @parent = parent
  @description_module = description_module
  @description_proc = description_proc
end

Public Instance Methods

add_module(mod) click to toggle source
# File lib/descendants_describable.rb, line 54
def add_module(mod)
  self.new_class.send(:include, mod)
end
describe_descendants() click to toggle source
# File lib/descendants_describable.rb, line 28
def describe_descendants
  instance_exec(&@description_proc)
end
method_missing(method, *args) { || ... } click to toggle source
# File lib/descendants_describable.rb, line 76
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.pop
  end
end
register_reload_hook() click to toggle source
# File lib/descendants_describable.rb, line 45
def register_reload_hook
  descriptor = self
  ActiveSupport::Reloader.to_run do
    descriptor.undefine_descendants
    descriptor.reload_parent
    descriptor.describe_descendants
  end
end
reload_parent() click to toggle source
# File lib/descendants_describable.rb, line 39
def reload_parent
  parent_name = @parent.name.to_sym
  Object.send(:remove_const, parent_name) if Object.const_defined?(parent_name)
  @parent = ActiveSupport::Dependencies.load_missing_constant(Object, parent_name)
end
respond_to?(method) click to toggle source
# File lib/descendants_describable.rb, line 86
def respond_to?(method)
  @description_module.const_get(method.to_s.camelize) rescue false
end
type(name) { || ... } click to toggle source
# File lib/descendants_describable.rb, line 58
def type(name)

  self.new_class = begin
    Object.const_get(name.to_s.camelize)
  rescue NameError
    new_class = Class.new(@parent)
    Object.const_set(name.to_s.camelize, new_class)
    @descendant_names << name.to_s.camelize.to_sym
    new_class
  end

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

  yield if block_given?

  self.new_class = nil
end
undefine_descendants() click to toggle source
# File lib/descendants_describable.rb, line 32
def undefine_descendants
  @descendant_names.each do |descendant_name|
    Object.send(:remove_const, descendant_name) if Object.const_defined?(descendant_name)
  end
  @descendant_names = []
end