Class: ActiveInteraction::ArrayInput

Inherits:
Input
  • Object
show all
Defined in:
lib/active_interaction/array_input.rb

Overview

Represents a processed array input.

Instance Attribute Summary collapse

Attributes inherited from Input

#value

Instance Method Summary collapse

Constructor Details

#initialize(filter, value: nil, error: nil, index_errors: false, children: []) ⇒ ArrayInput

Returns a new instance of ArrayInput.



7
8
9
10
11
12
13
# File 'lib/active_interaction/array_input.rb', line 7

def initialize(filter, value: nil, error: nil, index_errors: false, children: [])
  super(filter, value: value, error: error)

  @filter = filter
  @index_errors = index_errors
  @children = children
end

Instance Attribute Details

#childrenArray<Input, ArrayInput, HashInput> (readonly)

Child inputs if a nested filter is used.

Returns:



19
20
21
# File 'lib/active_interaction/array_input.rb', line 19

def children
  @children
end

Instance Method Details

#errorsFilter::Error

Any errors that occurred during processing.

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_interaction/array_input.rb', line 24

def errors
  return @errors if defined?(@errors)

  return @errors = super if @error

  child_errors = get_errors_by_index(children)

  return @errors = super if child_errors.empty?

  @errors ||=
    if @index_errors
      child_errors.map do |(error, i)|
        name = attach_child_name(:"#{@filter.name}[#{i}]", error)
        Filter::Error.new(error.filter, error.type, name: name)
      end.freeze
    else
      error, = child_errors.first
      [Filter::Error.new(@filter, error.type)].freeze
    end
end