class Definition::ConformResult

Attributes

conform_errors[RW]
value[RW]

Public Class Methods

new(value, errors: []) click to toggle source
# File lib/definition/conform_result.rb, line 7
def initialize(value, errors: [])
  self.value = value
  self.conform_errors = errors
end

Public Instance Methods

conformed?()
Alias for: passed?
error_hash() click to toggle source
# File lib/definition/conform_result.rb, line 33
def error_hash
  {}.tap do |error_hash|
    errors.each do |error|
      path_hash = if error.error_path.empty?
                    { "" => error }
                  else
                    error.error_path.reverse
                         .inject([error]) { |errors, key| { key => errors } }
                  end

      merge_error_hash(error_hash, path_hash)
    end
  end
end
error_message() click to toggle source
# File lib/definition/conform_result.rb, line 19
def error_message
  error_tree.map(&:message).join(", ")
end
error_tree() click to toggle source
# File lib/definition/conform_result.rb, line 48
def error_tree
  conform_errors
end
errors() click to toggle source
# File lib/definition/conform_result.rb, line 27
def errors
  leaf_errors.map do |error|
    find_next_parent_key_error(error) || error
  end.compact.uniq
end
leaf_errors() click to toggle source
# File lib/definition/conform_result.rb, line 23
def leaf_errors
  conform_errors.map(&:leaf_errors).flatten
end
passed?() click to toggle source
# File lib/definition/conform_result.rb, line 14
def passed?
  conform_errors.empty?
end
Also aliased as: conformed?

Private Instance Methods

find_next_parent_key_error(error) click to toggle source
# File lib/definition/conform_result.rb, line 64
def find_next_parent_key_error(error)
  current = error
  loop do
    return current if current.is_a?(KeyConformError)

    current = current.parent
    break unless current
  end
  nil
end
merge_error_hash(hash, new_hash) click to toggle source
# File lib/definition/conform_result.rb, line 54
def merge_error_hash(hash, new_hash)
  hash.deep_merge!(new_hash) do |_key, old, new|
    if old.is_a?(Array) && new.is_a?(Hash) # Dont replace Hashes with arrays
      new
    elsif old.is_a?(Array) && new.is_a?(Array) # concat arrays during deep_merge
      old + new
    end
  end
end