class Mongomatic::Errors

Public Class Methods

new() click to toggle source
# File lib/mongomatic/errors.rb, line 3
def initialize
  @errors = HashWithIndifferentAccess.new
end

Public Instance Methods

<<(error_array) click to toggle source
# File lib/mongomatic/errors.rb, line 12
def <<(error_array)
  error_array = Array(error_array)
  if error_array.size == 2
    add error_array[0], error_array[1]
  else
    add_to_base error_array[0]
  end
end
[](field) click to toggle source
# File lib/mongomatic/errors.rb, line 56
def [](field)
  @errors[field] || []
end
add(field, message) click to toggle source
# File lib/mongomatic/errors.rb, line 7
def add(field, message)
  @errors[field] ||= []
  @errors[field] << message
end
add_to_base(message) click to toggle source
# File lib/mongomatic/errors.rb, line 21
def add_to_base(message)
  @errors["base"] ||= []
  @errors["base"] << message
end
any?() click to toggle source
# File lib/mongomatic/errors.rb, line 35
def any?
  !empty?
end
count() click to toggle source
# File lib/mongomatic/errors.rb, line 39
def count
  @errors.values.inject(0) { |sum, errors| sum += errors.size }
end
empty?() click to toggle source
# File lib/mongomatic/errors.rb, line 31
def empty?
  !(@errors.any? { |k,v| v && !v.empty? })
end
full_messages() click to toggle source
# File lib/mongomatic/errors.rb, line 43
def full_messages
  full_messages = []
  @errors.each do |field, messages|
    messages.each do |message|
      msg = []
      msg << field unless field == "base"
      msg << message
      full_messages << msg.join(" ")
    end
  end
  full_messages
end
on(field) click to toggle source
# File lib/mongomatic/errors.rb, line 64
def on(field)
  self[field]
end
remove(field, message) click to toggle source
# File lib/mongomatic/errors.rb, line 26
def remove(field, message)
  @errors[field] ||= []
  @errors[field].delete message
end
to_hash() click to toggle source
# File lib/mongomatic/errors.rb, line 60
def to_hash
  @errors
end