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) ⇒ DescendantsDescriptor

Returns a new instance of DescendantsDescriptor.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/descendants_describable.rb', line 44

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.



15
16
17
# File 'lib/descendants_describable.rb', line 15

def new_class
  @new_class
end

Instance Method Details

#add_module(mod) ⇒ Object



23
24
25
# File 'lib/descendants_describable.rb', line 23

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

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#type(name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/descendants_describable.rb', line 27

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)
    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