Class: ActiveModel::BetterErrors::ErrorCollection
- Inherits:
-
Object
- Object
- ActiveModel::BetterErrors::ErrorCollection
- Includes:
- Enumerable
- Defined in:
- lib/active_model/better_errors/error_collection.rb
Overview
ErrorCollection
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
Instance Method Summary collapse
- #[](attribute) ⇒ Object
- #[]=(attribute, error) ⇒ Object
- #add(attribute, message, options = {}) ⇒ Object
- #added?(attribute, message = nil, options = {}) ⇒ Boolean
- #clear ⇒ Object
- #delete(attribute) ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
- #get(attribute) ⇒ Object
- #include?(attribute) ⇒ Boolean
-
#initialize(base) ⇒ ErrorCollection
constructor
A new instance of ErrorCollection.
- #keys ⇒ Object
- #set(attribute, errors) ⇒ Object
- #size ⇒ Object (also: #count)
- #to_a ⇒ Object
- #to_hash ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(base) ⇒ ErrorCollection
Returns a new instance of ErrorCollection.
12 13 14 15 |
# File 'lib/active_model/better_errors/error_collection.rb', line 12 def initialize(base) @base = base @collection = {} end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
11 12 13 |
# File 'lib/active_model/better_errors/error_collection.rb', line 11 def base @base end |
Instance Method Details
#[](attribute) ⇒ Object
38 39 40 |
# File 'lib/active_model/better_errors/error_collection.rb', line 38 def [](attribute) get(attribute.to_sym) || set(attribute.to_sym, []) end |
#[]=(attribute, error) ⇒ Object
42 43 44 |
# File 'lib/active_model/better_errors/error_collection.rb', line 42 def []=(attribute, error) self[attribute] << error end |
#add(attribute, message, options = {}) ⇒ Object
84 85 86 |
# File 'lib/active_model/better_errors/error_collection.rb', line 84 def add(attribute, , = {}) self[attribute] << [, ] end |
#added?(attribute, message = nil, options = {}) ⇒ Boolean
88 89 90 91 |
# File 'lib/active_model/better_errors/error_collection.rb', line 88 def added?(attribute, = nil, = {}) = ErrorMessage.build(base, attribute, , ) self[attribute].include? end |
#clear ⇒ Object
17 18 19 |
# File 'lib/active_model/better_errors/error_collection.rb', line 17 def clear @collection.clear end |
#delete(attribute) ⇒ Object
34 35 36 |
# File 'lib/active_model/better_errors/error_collection.rb', line 34 def delete(attribute) @collection.delete attribute end |
#each ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/active_model/better_errors/error_collection.rb', line 46 def each @collection.each_key do |attribute| self[attribute].each do || yield attribute, end end end |
#empty? ⇒ Boolean
80 81 82 |
# File 'lib/active_model/better_errors/error_collection.rb', line 80 def empty? size == 0 end |
#get(attribute) ⇒ Object
25 26 27 |
# File 'lib/active_model/better_errors/error_collection.rb', line 25 def get(attribute) @collection[attribute] end |
#include?(attribute) ⇒ Boolean
21 22 23 |
# File 'lib/active_model/better_errors/error_collection.rb', line 21 def include?(attribute) ( v = @collection[attribute]) && v.any? end |
#keys ⇒ Object
63 64 65 |
# File 'lib/active_model/better_errors/error_collection.rb', line 63 def keys @collection.keys end |
#set(attribute, errors) ⇒ Object
29 30 31 32 |
# File 'lib/active_model/better_errors/error_collection.rb', line 29 def set(attribute, errors) return delete attribute if errors.nil? @collection[attribute] = ErrorMessageSet.new(base, attribute, errors) end |
#size ⇒ Object Also known as: count
54 55 56 |
# File 'lib/active_model/better_errors/error_collection.rb', line 54 def size values.reduce(0) { |a, e| a + e.size } end |
#to_a ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/active_model/better_errors/error_collection.rb', line 67 def to_a array = [] @collection.each_key do |attribute| self[attribute].each { || array << } end array end |
#to_hash ⇒ Object
76 77 78 |
# File 'lib/active_model/better_errors/error_collection.rb', line 76 def to_hash @collection.dup end |
#values ⇒ Object
59 60 61 |
# File 'lib/active_model/better_errors/error_collection.rb', line 59 def values @collection.values end |