Class: Formalist::Form::Result::Attr

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Attr.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/formalist/form/result/attr.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] || []
  @children = build_children
end

Instance Attribute Details

#children ⇒ Object (readonly)

Returns the value of attribute children.



10
11
12
# File 'lib/formalist/form/result/attr.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/attr.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/attr.rb', line 9

def definition
  @definition
end

#errors ⇒ Object (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#input ⇒ Object (readonly)

Returns the value of attribute input.



9
10
11
# File 'lib/formalist/form/result/attr.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/attr.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/attr.rb', line 9

def value_rules
  @value_rules
end

Instance Method Details

#to_ast ⇒ Array

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

The array takes the following format:

[:attr, [params]]

With the following parameters:

  1. Attribute name
  2. Validation rules (if any)
  3. Validation error messages (if any)
  4. Child form elements

Examples:

"metadata" attr

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

Returns:

  • (Array) —

    the attribute as an array.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/formalist/form/result/attr.rb', line 56

def to_ast
  # Errors, if the attr hash is present and its members have errors:
  # {:meta=>[[{:pages=>[["pages is missing"], nil]}], {}]}

  # Errors, if the attr hash hasn't been provided
  # {:meta=>[["meta is missing"], nil]}

  local_errors = errors[0].is_a?(Hash) ? [] : errors

  [:attr, [
    definition.name,
    value_predicates,
    local_errors,
    children.map(&:to_ast),
  ]]
end