Class: AdequateErrors::Errors
- Inherits:
-
Object
- Object
- AdequateErrors::Errors
- 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
-
#add(attribute, type = :invalid, options = {}) ⇒ Object
Adds error.
-
#delete(attribute) ⇒ Object
Delete errors of attribute.
-
#import(error, override_options = {}) ⇒ Object
Imports error For copying nested model’s errors back to base model.
-
#include?(attribute) ⇒ Boolean
Whether the given attribute contains error.
-
#initialize(base) ⇒ Errors
constructor
A new instance of Errors.
-
#messages ⇒ Array(String)
All error messages.
-
#messages_for(params) ⇒ Array(String)
Convenience method to fetch error messages filtered by where condition.
-
#to_hash ⇒ Hash
Attributes with their error messages.
-
#where(params) ⇒ Array(AdequateErrors::Error)
Matching Error.
Constructor Details
#initialize(base) ⇒ Errors
Returns a new instance of Errors.
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.
39 40 41 42 43 44 45 46 47 |
# File 'lib/adequate_errors/errors.rb', line 39 def add(attribute, type = :invalid, = {}) if !type.is_a? Symbol [:message] = type type = :invalid end @errors.append(::AdequateErrors::Error.new(@base, attribute, type, )) 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`.
57 58 59 |
# File 'lib/adequate_errors/errors.rb', line 57 def import(error, = {}) @errors.append(::AdequateErrors::NestedError.new(@base, error, )) end |
#include?(attribute) ⇒ Boolean
Returns 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 |
#messages ⇒ Array(String)
Returns all error messages.
62 63 64 |
# File 'lib/adequate_errors/errors.rb', line 62 def @errors.map(&:message) end |
#messages_for(params) ⇒ Array(String)
Convenience method to fetch error messages filtered by where condition.
69 70 71 |
# File 'lib/adequate_errors/errors.rb', line 69 def (params) where(params).map(&:message) end |
#to_hash ⇒ Hash
Returns 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. else hash[error.attribute] = [error.] end end hash end |
#where(params) ⇒ Array(AdequateErrors::Error)
Returns matching AdequateErrors::Error.
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 |