Class: Formalist::Elements::Attr

Inherits:
Formalist::Element show all
Defined in:
lib/formalist/elements/attr.rb

Instance Attribute Summary

Attributes inherited from Formalist::Element

#attributes, #children, #errors, #input, #name

Instance Method Summary collapse

Methods inherited from Formalist::Element

#==, build, fill, #initialize, #type

Methods included from Formalist::Element::ClassInterface

#attribute, #attributes_schema, #type

Constructor Details

This class inherits a constructor from Formalist::Element

Instance Method Details

#fill(input:, errors:) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/formalist/elements/attr.rb', line 8

def fill(input:, errors:)
  input = input[name] || {}
  errors = errors[name] || {}

  children = self.children.map { |child|
    child.fill(input: input, errors: errors)
  }

  super(input: input, errors: errors, children: children)
end

#to_astArray

Converts the attribute into an abstract syntax tree.

It takes the following format:

[:attr, [params]]

With the following parameters:

  1. Attribute name
  2. Custom element type (or :attr otherwise)
  3. Error messages
  4. Form element attributes
  5. Child form elements

Examples:

"metadata" attr

attr.to_ast
# => [:attr, [
  :metadata,
  :attr,
  ["metadata is missing"],
  [:object, []],
  [...child elements...]
]]

Returns:

  • (Array)

    the attribute as an abstract syntax tree.

See Also:



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/formalist/elements/attr.rb', line 48

def to_ast
  local_errors = errors.is_a?(Array) ? errors : []

  [:attr, [
    name,
    type,
    local_errors,
    Element::Attributes.new(attributes).to_ast,
    children.map(&:to_ast),
  ]]
end