Class: Formalist::Form::Result::Many
- Inherits:
-
Object
- Object
- Formalist::Form::Result::Many
- Defined in:
- lib/formalist/form/result/many.rb
Instance Attribute Summary collapse
-
#child_template ⇒ Object
readonly
Returns the value of attribute child_template.
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#collection_rules ⇒ Object
readonly
Returns the value of attribute collection_rules.
-
#definition ⇒ Object
readonly
Returns the value of attribute definition.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#value_predicates ⇒ Object
readonly
Returns the value of attribute value_predicates.
-
#value_rules ⇒ Object
readonly
Returns the value of attribute value_rules.
Instance Method Summary collapse
-
#initialize(definition, input, rules, errors) ⇒ Many
constructor
A new instance of Many.
-
#to_ast ⇒ Array
Converts a collection of "many" repeating elements into an array format for including in a form's abstract syntax tree.
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:
- Collection array name
- Collection validation rules (if any)
- Collection error messages (if any)
- Collection configuration
- Child element "template" (i.e. the form elements comprising a single entry in the collection of "many" elements, without any user data associated)
- Child elements, one for each of the entries in the input data (or none, if there is no or empty input data)
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 |