Class: AdequateErrors::Errors

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

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Errors

Returns a new instance of Errors.

Parameters:

  • base (ActiveModel::Base)


17
18
19
20
# File 'lib/adequate_errors/errors.rb', line 17

def initialize(base)
  @base = base
  @errors = []
end

Instance Method Details

#add(attribute, type = :invalid, options = {}) ⇒ Object

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

Parameters:

  • attribute (Symbol)

    attribute that the error belongs to

  • type (Symbol) (defaults to: :invalid)

    error’s type, defaults to ‘:invalid`. As convenience, if type is String/Proc/Lambda, it will be moved to `options`, and type itself will be changed to the default `:invalid`.

  • options (Hash) (defaults to: {})

    extra conditions such as interpolated value



39
40
41
42
43
44
45
46
47
# 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) ⇒ Object

Delete errors of attribute



23
24
25
26
27
# 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 = {}) ⇒ Object

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`.

Parameters:

  • override_options (Hash) (defaults to: {})

Options Hash (override_options):

  • :attribute (Symbol)

    Override the attribute the error belongs to

  • :type (Symbol)

    Override type of the error.



57
58
59
# 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) ⇒ Boolean

Returns whether the given attribute contains error.

Returns:

  • (Boolean)

    whether the given attribute contains error.



91
92
93
# File 'lib/adequate_errors/errors.rb', line 91

def include?(attribute)
  @errors.any?{|error| error.attribute == attribute }
end

#messagesArray(String)

Returns all error messages.

Returns:

  • (Array(String))

    all error messages



62
63
64
# File 'lib/adequate_errors/errors.rb', line 62

def messages
  @errors.map(&:message)
end

#messages_for(params) ⇒ Array(String)

Convenience method to fetch error messages filtered by where condition.

Parameters:

  • params (Hash)

    filter condition, see #where for details.

Returns:

  • (Array(String))

    error messages



69
70
71
# File 'lib/adequate_errors/errors.rb', line 69

def messages_for(params)
  where(params).map(&:message)
end

#to_hashHash

Returns attributes with their error messages.

Returns:

  • (Hash)

    attributes with their error messages



96
97
98
99
100
101
102
103
104
105
106
# 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) ⇒ Array(AdequateErrors::Error)

Returns matching AdequateErrors::Error.

Parameters:

  • params (Hash)

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

Options Hash (params):

  • :attribute (Symbol)

    Filtering on attribute the error belongs to

  • :type (Symbol)

    Filter on type of error

Returns:



82
83
84
85
86
87
88
# File 'lib/adequate_errors/errors.rb', line 82

def where(params)
  return @errors.dup if params.blank?

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