Class: AWS::Record::Errors
- Inherits:
-
Core::IndifferentHash
- Object
- Hash
- Core::IndifferentHash
- AWS::Record::Errors
- Includes:
- Enumerable
- Defined in:
- lib/aws/record/errors.rb
Instance Method Summary collapse
-
#[](attribute_name) ⇒ Array<String>
(also: #on)
Returns the errors for the atttibute in an array.
-
#[]=(attribute_name, message = 'is invalid') ⇒ String
(also: #add)
Adds an error message to the named attribute.
-
#add_to_base(message) ⇒ String
Adds a general error message (not associated with any particular attribute).
-
#clear! ⇒ nil
Removes all error messages.
-
#count ⇒ Integer
(also: #size)
Returns the number of error messages.
-
#each {|attribute_name, error_message| ... } ⇒ Object
Yields once for each error message added.
-
#full_messages ⇒ Array of Strings
(also: #to_a)
Returns the errors prefixed by a humanized version of the attribute name.
-
#to_hash ⇒ Object
Returns a hash of of errors messages.
Instance Method Details
#[](attribute_name) ⇒ Array<String> Also known as: on
Returns the errors for the atttibute in an array.
errors.add(:name, 'may not be blank')
errors.add(:name, 'must be less than 30 characters')
errors[:name]
#=> ['may not be blank', 'must be less than 30 characters']
41 42 43 |
# File 'lib/aws/record/errors.rb', line 41 def [] attribute_name super(attribute_name) || [] end |
#[]=(attribute_name, message = 'is invalid') ⇒ String Also known as: add
Adds an error message to the named attribute.
errors.add(:name, 'may not be blank')
errors.on(:name)
#=> ['may not be blank']
If you want to add a general error message, then pass :base
for attribute_name
, or call #add_to_base.
59 60 61 62 63 64 65 66 |
# File 'lib/aws/record/errors.rb', line 59 def []= attribute_name, = 'is invalid' if has_key?(attribute_name) self[attribute_name] << else super(attribute_name, []) end self[attribute_name] end |
#add_to_base(message) ⇒ String
Adds a general error message (not associated with any particular attribute).
74 75 76 |
# File 'lib/aws/record/errors.rb', line 74 def add_to_base add(:base, ) end |
#clear! ⇒ nil
Removes all error messages.
141 142 143 144 145 146 |
# File 'lib/aws/record/errors.rb', line 141 def clear! keys.each do |key| delete(key) end nil end |
#count ⇒ Integer Also known as: size
Returns the number of error messages.
79 80 81 |
# File 'lib/aws/record/errors.rb', line 79 def count values.flatten.length end |
#each {|attribute_name, error_message| ... } ⇒ Object
Yields once for each error message added.
An attribute_name may yield more than once if there are more than one errors associated with that attirbute.
93 94 95 96 97 98 99 |
# File 'lib/aws/record/errors.rb', line 93 def each &block super do |attribute_name, | .each do || yield(attribute_name, ) end end end |
#full_messages ⇒ Array of Strings Also known as: to_a
Returns the errors prefixed by a humanized version of the attribute name.
errors.add(:name, 'may not be blank')
errors.
#=> ['Name may not be blank']
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/aws/record/errors.rb', line 109 def = [] each do |attr_name, | << case attr_name when 'base' then .dup else "#{attr_name.capitalize.gsub(/_/, ' ')} #{}" end end end |
#to_hash ⇒ Object
Returns a hash of of errors messages. Keys are attribute names and values are arrays of error messages.
errors.add(:name, 'may not be blank')
errors.to_hash
#=> { 'name' => ['may not be blank'] }
Please note that the hash values are always arrays, even if there is only one error message for the attribute.
130 131 132 133 134 135 136 137 |
# File 'lib/aws/record/errors.rb', line 130 def to_hash hash = {} each do |attr_name, | hash[attr_name] ||= [] hash[attr_name] << .dup end hash end |