Class: Sukima::ErrorField

Inherits:
Object
  • Object
show all
Defined in:
lib/sukima/error_field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors: []) ⇒ ErrorField

Returns a new instance of ErrorField.



7
8
9
10
# File 'lib/sukima/error_field.rb', line 7

def initialize(errors: [])
  @children = {}
  @errors = errors
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



5
6
7
# File 'lib/sukima/error_field.rb', line 5

def errors
  @errors
end

Instance Method Details

#[](name) ⇒ Object



12
13
14
# File 'lib/sukima/error_field.rb', line 12

def [](name)
  @children[name]
end

#[]=(name, value) ⇒ Object



16
17
18
# File 'lib/sukima/error_field.rb', line 16

def []=(name, value)
  @children[name] = value
end

#digObject



20
21
22
# File 'lib/sukima/error_field.rb', line 20

def dig(*)
  @children.dig(*)
end

#messages(path = []) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/sukima/error_field.rb', line 31

def messages(path = [])
  prefix = path.empty? ? '' : "#{path.join('.')} "
  result = @errors.dup.map! { "#{prefix}#{_1}" }
  @children.map do |name, error_field|
    result.concat(error_field.messages([*path, name]))
  end
  result
end

#to_hObject



40
41
42
# File 'lib/sukima/error_field.rb', line 40

def to_h
  { errors: @errors, children: @children.transform_values(&:to_h) }
end

#valid?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
# File 'lib/sukima/error_field.rb', line 24

def valid?
  return false if @errors.any?

  @children.each_value { return false unless _1.valid? }
  true
end