Class: DataModel::Error
- Inherits:
-
Object
- Object
- DataModel::Error
- Includes:
- Errors
- Defined in:
- lib/data_model/error.rb
Overview
Error is a class that holds errors. Errors are a tuple of [name, ctx]
- name is a symbol that identifies the error
- ctx is contextual information about the error which can be used to build an error message
The error object is a structured way to store, modify, and add errors in that intermediary format.
To turn an error into a human readable message, use #to_messages, which delegates to a registry
Base errors are errors that are related to the object as a whole, and not to any specific child
Child errors are errors that are related to a specific child of the object, which may or may not apply depending on the type
Instance Method Summary collapse
-
#add(err, child: nil) ⇒ void
Add an error to the error list.
-
#all ⇒ Hash{Symbol => Array<Array(Symbol, untyped)>}
(also: #to_h)
all errors.
-
#any?(&blk) ⇒ Boolean
Returns true if any errors are present.
-
#base ⇒ Array<Array(Symbol, untyped)>
errors related to the object as a whole.
-
#children ⇒ Hash{Symbol => Array<Array(Symbol, untyped)>}
errors related children.
-
#empty? ⇒ Boolean
Returns true if no errors are present.
-
#initialize ⇒ Error
constructor
Create a new error Object.
-
#merge_child(name, child) ⇒ void
Merge another error object into this one for child Errors.
-
#to_messages(registry: Registry.instance) ⇒ Hash{Symbol => Array[String]}
Get human readable error messages from error tuples.
Methods included from Errors
#blank_error, #blank_error_message, #coerce_error, #coerce_error_message, #earliest_error, #early_error_message, #error_messages, #exclusion_error, #exclusion_error_message, #extra_keys_error, #extra_keys_error_message, #format_error, #format_error_message, #inclusion_error, #inclusion_error_message, #late_error_message, #latest_error, #max_error, #max_error_message, #min_error, #min_error_message, #missing_error, #missing_error_message, #type_error, #type_error_message
Constructor Details
#initialize ⇒ Error
Create a new error Object
16 17 18 19 |
# File 'lib/data_model/error.rb', line 16 def initialize @base = [] @children = {} end |
Instance Method Details
#add(err, child: nil) ⇒ void
This method returns an undefined value.
Add an error to the error list.
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/data_model/error.rb', line 71 def add(err, child: nil) if child.is_a?(Array) child = child.join(".").to_sym end if child == :base raise "child errors may not be named :base" end errs = child ? @children[child] ||= [] : @base errs.push(err) end |
#all ⇒ Hash{Symbol => Array<Array(Symbol, untyped)>} Also known as: to_h
all errors
35 36 37 |
# File 'lib/data_model/error.rb', line 35 def all return children.merge(base:) end |
#any?(&blk) ⇒ Boolean
Returns true if any errors are present.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/data_model/error.rb', line 44 def any?(&blk) if !blk return !@base.empty? || !@children.empty? end any = T.let(false, T::Boolean) for error_list in all.values any = error_list.any?(&blk) if any break end end return any end |
#base ⇒ Array<Array(Symbol, untyped)>
errors related to the object as a whole
23 24 25 |
# File 'lib/data_model/error.rb', line 23 def base return @base end |
#children ⇒ Hash{Symbol => Array<Array(Symbol, untyped)>}
errors related children
29 30 31 |
# File 'lib/data_model/error.rb', line 29 def children return @children end |
#empty? ⇒ Boolean
Returns true if no errors are present.
63 64 65 |
# File 'lib/data_model/error.rb', line 63 def empty? !any? end |
#merge_child(name, child) ⇒ void
This method returns an undefined value.
Merge another error object into this one for child Errors
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/data_model/error.rb', line 88 def merge_child(name, child) if !child.any? return end for (key, error_list) in child.all for error in error_list add(error, child: [name, key]) end end end |