Class: Metaractor::Errors

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/metaractor/errors.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Constructor Details

#initializeErrors

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
    if h.is_a? Metaractor::Errors
      tree = Sycamore::Tree.from(h.instance_variable_get(:@tree))
    else
      tree = 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 full_messages(tree = @tree)
  messages = []
  tree.each_path do |path|
    messages << message_from_path(path)
  end

  messages
end

#full_messages_for(*path) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/metaractor/errors.rb', line 117

def full_messages_for(*path)
  child_tree = @tree.fetch_path(path)

  if child_tree.strict_leaves?
    child_tree = @tree.fetch_path(path[0..-2])
  end

  full_messages(child_tree)
end

#include?(*elements) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/metaractor/errors.rb', line 138

def include?(*elements)
  if elements.size == 1 &&
      elements.first.is_a?(Hash)
    unwrapped_tree.include?(*elements)
  else
    if elements.all? {|e| e.is_a? String }
      full_messages.include?(*elements)
    else
      elements.all? do |element|
        @tree.include_path?(element)
      end
    end
  end
end

#inspectObject



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/metaractor/errors.rb', line 174

def inspect
  str = "<##{self.class.name}: "

  if !self.empty?
    str << "Errors:\n"
    str << Metaractor.format_hash(to_h(unwrap: false))
    str << "\n"
  end

  str << ">"
  str
end

#slice(*paths) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/metaractor/errors.rb', line 153

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



165
166
167
168
169
170
171
# File 'lib/metaractor/errors.rb', line 165

def to_h(unwrap: true)
  if unwrap
    unwrapped_tree.to_h
  else
    @tree.to_h
  end
end