Class: Formalist::Form::Result::Many

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Many.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/formalist/form/result/many.rb', line 12

def initialize(definition, input, rules, errors)
  value_rules_compiler = Validation::ValueRulesCompiler.new(definition.name)
  value_predicates_compiler = Validation::PredicateListCompiler.new
  collection_rules_compiler = Validation::CollectionRulesCompiler.new(definition.name)

  @definition = definition
  @input = input.fetch(definition.name, [])
  @value_rules = value_rules_compiler.(rules)
  @value_predicates = value_predicates_compiler.(@value_rules)
  @collection_rules = collection_rules_compiler.(rules)
  @errors = errors.fetch(definition.name, [])[0] || []
  @child_template = build_child_template
  @children = build_children
end

Instance Attribute Details

#child_template ⇒ Object (readonly)

Returns the value of attribute child_template.



10
11
12
# File 'lib/formalist/form/result/many.rb', line 10

def child_template
  @child_template
end

#children ⇒ Object (readonly)

Returns the value of attribute children.



10
11
12
# File 'lib/formalist/form/result/many.rb', line 10

def children
  @children
end

#collection_rules ⇒ Object (readonly)

Returns the value of attribute collection_rules.



9
10
11
# File 'lib/formalist/form/result/many.rb', line 9

def collection_rules
  @collection_rules
end

#definition ⇒ Object (readonly)

Returns the value of attribute definition.



9
10
11
# File 'lib/formalist/form/result/many.rb', line 9

def definition
  @definition
end

#errors ⇒ Object (readonly)

Returns the value of attribute errors.



9
10
11
# File 'lib/formalist/form/result/many.rb', line 9

def errors
  @errors
end

#input ⇒ Object (readonly)

Returns the value of attribute input.



9
10
11
# File 'lib/formalist/form/result/many.rb', line 9

def input
  @input
end

#value_predicates ⇒ Object (readonly)

Returns the value of attribute value_predicates.



9
10
11
# File 'lib/formalist/form/result/many.rb', line 9

def value_predicates
  @value_predicates
end

#value_rules ⇒ Object (readonly)

Returns the value of attribute value_rules.



9
10
11
# File 'lib/formalist/form/result/many.rb', line 9

def value_rules
  @value_rules
end

Instance Method Details

#to_ast ⇒ Array

Converts a collection of "many" repeating elements into an array format for including in a form's abstract syntax tree.

The array takes the following format:

[:many, [params]]

With the following parameters:

  1. Collection array name
  2. Collection validation rules (if any)
  3. Collection error messages (if any)
  4. Collection configuration
  5. Child element "template" (i.e. the form elements comprising a single entry in the collection of "many" elements, without any user data associated)
  6. Child elements, one for each of the entries in the input data (or none, if there is no or empty input data)

Examples:

"locations" collection

many.to_ast # =>
# [:many, [
#   :locations,
#   [[:predicate, [:min_size?, [3]]]],
#   ["locations size cannot be less than 3"],
#   [
#     [:allow_create, true],
#     [:allow_update, true],
#     [:allow_destroy, true],
#     [:allow_reorder, true]
#   ],
#   [
#     [:field, [:name, "string", "default", nil, [], [], []]],
#     [:field, [:address, "string", "default", nil, [], [], []]]
#   [
#     [
#       [:field, [:name, "string", "default", "Icelab Canberra", [], [], []]],
#       [:field, [:address, "string", "default", "Canberra, ACT, Australia", [], [], []]]
#     ],
#     [
#       [:field, [:name, "string", "default", "Icelab Melbourne", [], [], []]],
#       [:field, [:address, "string", "default", "Melbourne, VIC, Australia", [], [], []]]
#     ]
#   ]
# ]]

Returns:

  • (Array) —

    the collection as an array.



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/formalist/form/result/many.rb', line 76

def to_ast
  local_errors = errors.select { |e| e.is_a?(String) }

  [:many, [
    definition.name,
    value_predicates,
    local_errors,
    definition.config.to_a,
    child_template.map(&:to_ast),
    children.map { |el_list| el_list.map(&:to_ast) },
  ]]
end