Class: Metaractor::Errors
- Inherits:
-
Object
- Object
- Metaractor::Errors
- Extended by:
- Forwardable
- Defined in:
- lib/metaractor/errors.rb
Defined Under Namespace
Classes: Error
Instance Method Summary collapse
- #add(error: {}, errors: {}, object: nil) ⇒ Object
- #dig(*path) ⇒ Object (also: #[])
- #full_messages(tree = @tree) ⇒ Object (also: #to_a)
- #full_messages_for(*path) ⇒ Object
- #include?(*elements) ⇒ Boolean
-
#initialize ⇒ Errors
constructor
A new instance of Errors.
- #inspect ⇒ Object
- #slice(*paths) ⇒ Object
- #to_h(unwrap: true) ⇒ Object (also: #to_hash)
Constructor Details
#initialize ⇒ Errors
Returns a new instance of Errors.
66 67 68 |
# File 'lib/metaractor/errors.rb', line 66 def initialize @tree = Sycamore::Tree.new end |
Instance Method Details
#add(error: {}, errors: {}, object: nil) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/metaractor/errors.rb', line 72 def add(error: {}, errors: {}, object: nil) trees = [] [error, errors].each do |h| tree = nil tree = if h.is_a? Metaractor::Errors Sycamore::Tree.from(h.instance_variable_get(:@tree)) else Sycamore::Tree.from(normalize_error_hash(h)) end unless tree.empty? if tree.nodes.any? { |node| tree.strict_leaf?(node) } raise ArgumentError, "Invalid hash!" end trees << tree end end trees.each do |tree| tree.each_path do |path| node = path.node unless node.is_a?(Error) node = Error.new( value: path.node, object: object ) end @tree[path.parent] << node end end @tree.compact end |
#dig(*path) ⇒ Object Also known as: []
127 128 129 130 131 132 133 134 135 |
# File 'lib/metaractor/errors.rb', line 127 def dig(*path) result = @tree.dig(*path) if result.strict_leaves? unwrapped_enum(result.nodes) else unwrapped_tree(result).to_h end end |
#full_messages(tree = @tree) ⇒ Object Also known as: to_a
107 108 109 110 111 112 113 114 |
# File 'lib/metaractor/errors.rb', line 107 def (tree = @tree) = [] tree.each_path do |path| << (path) end end |
#full_messages_for(*path) ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/metaractor/errors.rb', line 117 def (*path) child_tree = @tree.fetch_path(path) if child_tree.strict_leaves? child_tree = @tree.fetch_path(path[0..-2]) end (child_tree) end |
#include?(*elements) ⇒ Boolean
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/metaractor/errors.rb', line 138 def include?(*elements) if elements.size == 1 && elements.first.is_a?(Hash) unwrapped_tree.include?(*elements) elsif elements.all? { |e| e.is_a? String } .include?(*elements) else elements.all? do |element| @tree.include_path?(element) end end end |
#inspect ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/metaractor/errors.rb', line 172 def inspect str = "<##{self.class.name}: " if !empty? str << "Errors:\n" str << Metaractor.format_hash(to_h(unwrap: false)) str << "\n" end str << ">" str end |
#slice(*paths) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/metaractor/errors.rb', line 151 def slice(*paths) new_tree = Sycamore::Tree.new paths.each do |path| if @tree.include_path?(path) new_tree[path] = @tree[path].dup end end unwrapped_tree(new_tree).to_h end |
#to_h(unwrap: true) ⇒ Object Also known as: to_hash
163 164 165 166 167 168 169 |
# File 'lib/metaractor/errors.rb', line 163 def to_h(unwrap: true) if unwrap unwrapped_tree.to_h else @tree.to_h end end |