Class: DescendantsDescribable::DescendantsDescriptor

Inherits:
Object
  • Object
show all
Defined in:
lib/descendants_describable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, description_module, description_proc = -> {}) ⇒ DescendantsDescriptor

Returns a new instance of DescendantsDescriptor.



20
21
22
23
24
25
26
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



76
77
78
79
80
81
82
83
84
# 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

Instance Attribute Details

#new_classObject

Returns the value of attribute new_class.



18
19
20
# File 'lib/descendants_describable.rb', line 18

def new_class
  @new_class
end

Instance Method Details

#add_module(mod) ⇒ Object



54
55
56
# File 'lib/descendants_describable.rb', line 54

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

#describe_descendantsObject



28
29
30
# File 'lib/descendants_describable.rb', line 28

def describe_descendants
  instance_exec(&@description_proc)
end

#register_reload_hookObject



45
46
47
48
49
50
51
52
# 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_parentObject



39
40
41
42
43
# 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) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/descendants_describable.rb', line 86

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

#type(name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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_descendantsObject



32
33
34
35
36
37
# 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