Method: Quince.define_constructor

Defined in:
lib/quince/singleton_methods.rb

.define_constructor(const, constructor_name = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/quince/singleton_methods.rb', line 3

def define_constructor(const, constructor_name = nil)
  if const.name
    parts = const.name.split("::")
    parent_namespace = Object.const_get(parts[0...-1].join("::")) if parts.length > 1
    constructor_name ||= parts.last
  end
  constructor_name ||= const.to_s

  HtmlTagComponents.instance_eval do
    mthd = lambda do |*children, **props, &block_children|
      new_props = {
        **props,
        Quince::Component::PARENT_SELECTOR_ATTR => __id,
      }
      const.create(*children, **new_props, &block_children)
    end

    if parent_namespace
      parent_namespace.instance_exec do
        define_method(constructor_name, &mthd)
      end
    else
      define_method(constructor_name, &mthd)
    end
  end
end