class Formalist::Definition

Constants

DuplicateElementError

Attributes

config[R]
elements[R]
form[R]

Public Class Methods

new(form, config, &block) click to toggle source
# File lib/formalist/definition.rb, line 9
def initialize(form, config, &block)
  @form = form
  @config = config
  @elements = []

  instance_eval(&block) if block
end

Public Instance Methods

method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/formalist/definition.rb, line 25
def method_missing(name, *args, &block)
  if element_type?(name)
    add_element(name, *args, &block)
  elsif form.respond_to?(name, include_private = true)
    form.send(name, *args, &block)
  else
    super
  end
end
with(**new_options, &block) click to toggle source
# File lib/formalist/definition.rb, line 17
def with(**new_options, &block)
  new_config = new_options.each_with_object(config.dup) { |(key, value), config|
    config.send :"#{key}=", value
  }

  self.class.new(form, new_config, &block)
end

Private Instance Methods

add_element(type, *args, &block) click to toggle source
# File lib/formalist/definition.rb, line 45
def add_element(type, *args, &block)
  element_name = args.shift unless args.first.is_a?(Hash)
  element_attrs = args.last.is_a?(Hash) ? args.last : {}

  if element_name && elements.any? { |element| element.name == element_name }
    raise DuplicateElementError, "element +#{element_name}+ is already defined in this context"
  end

  element_class = config.elements_container[type]
  element_children = with(&block).elements

  element = element_class.build(
    name: element_name,
    attributes: element_attrs,
    children: element_children,
  )

  elements << element
end
element_type?(type) click to toggle source
# File lib/formalist/definition.rb, line 41
def element_type?(type)
  config.elements_container.key?(type)
end
respond_to_missing?(name, _include_private = false) click to toggle source
Calls superclass method
# File lib/formalist/definition.rb, line 37
def respond_to_missing?(name, _include_private = false)
  element_type?(name) || form.respond_to?(name, _include_private = true) || super
end