class AdequateErrors::Errors

Collection of {Error} objects. Provides convenience methods to access these errors. It is accessed via model.errors.adequate

Public Class Methods

new(base) click to toggle source

@param base [ActiveModel::Base]

# File lib/adequate_errors/errors.rb, line 17
def initialize(base)
  @base = base
  @errors = []
end

Public Instance Methods

add(attribute, type = :invalid, options = {}) click to toggle source

Adds error. More than one error can be added to the same `attribute`. If no `type` is supplied, `:invalid` is assumed.

@param attribute [Symbol] attribute that the error belongs to @param type [Symbol] error's type, defaults to `:invalid`.

As convenience, if type is String/Proc/Lambda,
it will be moved to `options[:message]`,
and type itself will be changed to the default `:invalid`.

@param options [Hash] extra conditions such as interpolated value

# File lib/adequate_errors/errors.rb, line 39
def add(attribute, type = :invalid, options = {})

  if !type.is_a? Symbol
    options[:message] = type
    type = :invalid
  end

  @errors.append(::AdequateErrors::Error.new(@base, attribute, type, options))
end
delete(attribute) click to toggle source

Delete errors of attribute

# File lib/adequate_errors/errors.rb, line 23
def delete(attribute)
  @errors.delete_if do |error|
    error.attribute == attribute
  end
end
import(error, override_options = {}) click to toggle source

Imports error For copying nested model's errors back to base model. The provided error will be wrapped, and its attribute/type will be copied across. If attribute or type needs to be overriden, use `override_options`.

@param override_options [Hash] @option override_options [Symbol] :attribute Override the attribute the error belongs to @option override_options [Symbol] :type Override type of the error.

# File lib/adequate_errors/errors.rb, line 57
def import(error, override_options = {})
  @errors.append(::AdequateErrors::NestedError.new(@base, error, override_options))
end
include?(attribute) click to toggle source

@return [Boolean] whether the given attribute contains error.

# File lib/adequate_errors/errors.rb, line 91
def include?(attribute)
  @errors.any?{|error| error.attribute == attribute }
end
messages() click to toggle source

@return [Array(String)] all error messages

# File lib/adequate_errors/errors.rb, line 62
def messages
  @errors.map(&:message)
end
messages_for(params) click to toggle source

Convenience method to fetch error messages filtered by where condition. @param params [Hash] filter condition, see {#where} for details. @return [Array(String)] error messages

# File lib/adequate_errors/errors.rb, line 69
def messages_for(params)
  where(params).map(&:message)
end
to_hash() click to toggle source

@return [Hash] attributes with their error messages

# File lib/adequate_errors/errors.rb, line 96
def to_hash
  hash = {}
  @errors.each do |error|
    if hash.has_key?(error.attribute)
      hash[error.attribute] << error.message
    else
      hash[error.attribute] = [error.message]
    end
  end
  hash
end
where(params) click to toggle source

@param params [Hash]

filter condition
The most common keys are +:attribute+ and +:type+,
but other custom keys given during {Errors#add} can also be used.
If params is empty, all errors are returned.

@option params [Symbol] :attribute Filtering on attribute the error belongs to @option params [Symbol] :type Filter on type of error

@return [Array(AdequateErrors::Error)] matching {Error}.

# File lib/adequate_errors/errors.rb, line 82
def where(params)
  return @errors.dup if params.blank?

  @errors.select {|error|
    error.match?(params)
  }
end