Class: Sukima::ErrorField
- Inherits:
-
Object
- Object
- Sukima::ErrorField
- Defined in:
- lib/sukima/error_field.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
Returns the value of attribute errors.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #dig ⇒ Object
-
#initialize(errors: []) ⇒ ErrorField
constructor
A new instance of ErrorField.
- #messages(path = []) ⇒ Object
- #to_h ⇒ Object
- #valid? ⇒ Boolean
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
#errors ⇒ Object
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 |
#dig ⇒ Object
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 (path = []) prefix = path.empty? ? '' : "#{path.join('.')} " result = @errors.dup.map! { "#{prefix}#{_1}" } @children.map do |name, error_field| result.concat(error_field.([*path, name])) end result end |
#to_h ⇒ Object
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
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 |