Class: DataModel::Error
- Inherits:
-
Object
- Object
- DataModel::Error
- Extended by:
- T::Sig
- Defined in:
- lib/data_model/error.rb
Overview
Error is a class that holds errors.
Constant Summary collapse
- TErrorList =
T.type_alias { T::Array[TError] }
- TErrorMap =
T.type_alias { T::Hash[Symbol, TErrorList] }
Instance Method Summary collapse
- #add(err, child: nil) ⇒ Object
- #all ⇒ Object (also: #to_h)
- #any?(&blk) ⇒ Boolean
- #base ⇒ Object
- #children ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ Error
constructor
A new instance of Error.
- #merge_child(name, child) ⇒ Object
- #transform_child_context(&blk) ⇒ Object
- #transform_context(&blk) ⇒ Object
Constructor Details
#initialize ⇒ Error
Returns a new instance of Error.
12 13 14 15 |
# File 'lib/data_model/error.rb', line 12 def initialize @base = T.let([], TErrorList) @children = T.let({}, TErrorMap) end |
Instance Method Details
#add(err, child: nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/data_model/error.rb', line 63 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 ⇒ Object Also known as: to_h
31 32 33 |
# File 'lib/data_model/error.rb', line 31 def all return children.merge(base:) end |
#any?(&blk) ⇒ Boolean
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/data_model/error.rb', line 39 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 ⇒ Object
19 20 21 |
# File 'lib/data_model/error.rb', line 19 def base return @base end |
#children ⇒ Object
25 26 27 |
# File 'lib/data_model/error.rb', line 25 def children return @children end |
#merge_child(name, child) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/data_model/error.rb', line 77 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 |
#transform_child_context(&blk) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/data_model/error.rb', line 98 def transform_child_context(&blk) for error_list in @children.values for error in error_list key, context = error error[1] = blk.call(context, key) end end end |
#transform_context(&blk) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/data_model/error.rb', line 90 def transform_context(&blk) for error in @base key, context = error error[1] = blk.call(context, key) end end |