Class: Formalist::Form::Result::Section

Inherits:
Object
  • Object
show all
Defined in:
lib/formalist/form/result/section.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, input, rules, errors) ⇒ Section

Returns a new instance of Section.



8
9
10
11
12
13
14
# File 'lib/formalist/form/result/section.rb', line 8

def initialize(definition, input, rules, errors)
  @definition = definition
  @input = input
  @rules = rules
  @errors = errors
  @children = definition.children.map { |el| el.(input, rules, errors) }
end

Instance Attribute Details

#children ⇒ Object (readonly)

Returns the value of attribute children.



6
7
8
# File 'lib/formalist/form/result/section.rb', line 6

def children
  @children
end

#definition ⇒ Object (readonly)

Returns the value of attribute definition.



5
6
7
# File 'lib/formalist/form/result/section.rb', line 5

def definition
  @definition
end

#errors ⇒ Object (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/formalist/form/result/section.rb', line 5

def errors
  @errors
end

#input ⇒ Object (readonly)

Returns the value of attribute input.



5
6
7
# File 'lib/formalist/form/result/section.rb', line 5

def input
  @input
end

#rules ⇒ Object (readonly)

Returns the value of attribute rules.



5
6
7
# File 'lib/formalist/form/result/section.rb', line 5

def rules
  @rules
end

Instance Method Details

#to_ast ⇒ Array

Converts the section into an array format for including in a form's abstract syntax tree.

The array takes the following format:

[:section, [params]]

With the following parameters:

  1. Section name
  2. Section configuration
  3. Child form elements

Examples:

"content" section

section.to_ast # =>
# [:section, [
#   :content,
#   [
#     [:some_config_name, :some_config_value]
#   ],
#   [
#     ...child elements...
#   ]
# ]]

Returns:

  • (Array) —

    the section as an array.



44
45
46
47
48
49
50
# File 'lib/formalist/form/result/section.rb', line 44

def to_ast
  [:section, [
    definition.name,
    definition.config.to_a,
    children.map(&:to_ast),
  ]]
end