class Graphiti::Util::SimpleErrors

Attributes

details[R]
messages[R]

Public Class Methods

new(validation_target) click to toggle source
# File lib/graphiti/util/simple_errors.rb, line 11
def initialize(validation_target)
  @target = validation_target
  @messages = apply_default_array({})
  @details = apply_default_array({})
  @errors = apply_default_array({})
end

Public Instance Methods

[](attribute) click to toggle source
# File lib/graphiti/util/simple_errors.rb, line 23
def [](attribute)
  messages[attribute.to_sym]
end
add(attribute, code, message: nil) click to toggle source
# File lib/graphiti/util/simple_errors.rb, line 53
def add(attribute, code, message: nil)
  message ||= "is #{code.to_s.humanize.downcase}"

  details[attribute.to_sym] << {error: code}
  messages[attribute.to_sym] << message
end
added?(attribute, code) click to toggle source
# File lib/graphiti/util/simple_errors.rb, line 60
def added?(attribute, code)
  details[attribute.to_sym].include?({error: code})
end
blank?()
Alias for: empty?
clear() click to toggle source
# File lib/graphiti/util/simple_errors.rb, line 18
def clear
  messages.clear
  details.clear
end
count()
Alias for: size
each() { |attribute, error| ... } click to toggle source
# File lib/graphiti/util/simple_errors.rb, line 27
def each
  messages.each_key do |attribute|
    messages[attribute].each { |error| yield attribute, error }
  end
end
empty?() click to toggle source
# File lib/graphiti/util/simple_errors.rb, line 48
def empty?
  size.zero?
end
Also aliased as: blank?
full_message(attribute, message) click to toggle source
# File lib/graphiti/util/simple_errors.rb, line 74
def full_message(attribute, message)
  return message if attribute == :base
  "#{attribute} #{message}"
end
full_messages() click to toggle source
# File lib/graphiti/util/simple_errors.rb, line 64
def full_messages
  map { |attribute, message| full_message(attribute, message) }
end
Also aliased as: to_a
full_messages_for(attribute) click to toggle source
# File lib/graphiti/util/simple_errors.rb, line 69
def full_messages_for(attribute)
  attribute = attribute.to_sym
  messages[attribute].map { |message| full_message(attribute, message) }
end
keys() click to toggle source
# File lib/graphiti/util/simple_errors.rb, line 42
def keys
  messages.select { |key, value|
    !value.empty?
  }.keys
end
size() click to toggle source
# File lib/graphiti/util/simple_errors.rb, line 33
def size
  values.flatten.size
end
Also aliased as: count
to_a()
Alias for: full_messages
values() click to toggle source
# File lib/graphiti/util/simple_errors.rb, line 38
def values
  messages.values.reject(&:empty?)
end

Private Instance Methods

apply_default_array(hash) click to toggle source
# File lib/graphiti/util/simple_errors.rb, line 81
def apply_default_array(hash)
  hash.default_proc = proc { |h, key| h[key] = [] }
  hash
end