Class: AttrValidator::ValidationErrors
- Inherits:
-
Object
- Object
- AttrValidator::ValidationErrors
- Defined in:
- lib/attr_validator/validation_errors.rb
Instance Attribute Summary collapse
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
Instance Method Summary collapse
-
#[](attribute) ⇒ Object
When passed a symbol or a name of a method, returns an array of errors for the method.
-
#[]=(attribute, error) ⇒ Object
Adds to the supplied attribute the supplied error message.
-
#add(attribute, message) ⇒ Object
Adds
message
to the error messages onattribute
. -
#add_all(attribute, errors) ⇒ Object
Adds
messages
to the error messages onattribute
. -
#added?(attribute, message) ⇒ Boolean
Returns
true
if an error on the attribute with the given message is present,false
otherwise. -
#clear ⇒ Object
Clear the error messages.
-
#count ⇒ Object
Returns the number of error messages.
-
#delete(key) ⇒ Object
Delete messages for
key
. -
#each ⇒ Object
Iterates through each error key, value pair in the error messages hash.
-
#empty? ⇒ Boolean
(also: #blank?)
Returns
true
if no errors are found,false
otherwise. -
#full_message(attribute, message) ⇒ Object
Returns a full message for a given attribute.
-
#full_messages ⇒ Object
Returns all the full error messages in an array.
-
#full_messages_for(attribute) ⇒ Object
Returns all the full error messages for a given attribute in an array.
-
#get(key) ⇒ Object
Get messages for
key
. -
#include?(attribute) ⇒ Boolean
(also: #has_key?)
Returns
true
if the error messages include an error for the given keyattribute
,false
otherwise. -
#initialize ⇒ ValidationErrors
constructor
A new instance of ValidationErrors.
-
#keys ⇒ Object
Returns all message keys.
-
#set(key, value) ⇒ Object
Set messages for
key
tovalue
. -
#size ⇒ Object
Returns the number of error messages.
-
#to_a ⇒ Object
Returns an array of error messages, with the attribute name included.
-
#to_hash(full_messages = false) ⇒ Object
Returns a Hash of attributes with their error messages.
-
#values ⇒ Object
Returns all message values.
Constructor Details
#initialize ⇒ ValidationErrors
Returns a new instance of ValidationErrors.
4 5 6 |
# File 'lib/attr_validator/validation_errors.rb', line 4 def initialize @messages = {} end |
Instance Attribute Details
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
2 3 4 |
# File 'lib/attr_validator/validation_errors.rb', line 2 def @messages end |
Instance Method Details
#[](attribute) ⇒ Object
When passed a symbol or a name of a method, returns an array of errors for the method.
errors[:name] # => ["can not be nil"]
errors['name'] # => ["can not be nil"]
61 62 63 |
# File 'lib/attr_validator/validation_errors.rb', line 61 def [](attribute) get(attribute.to_sym) || set(attribute.to_sym, []) end |
#[]=(attribute, error) ⇒ Object
Adds to the supplied attribute the supplied error message.
errors[:name] = "must be set"
errors[:name] # => ['must be set']
69 70 71 |
# File 'lib/attr_validator/validation_errors.rb', line 69 def []=(attribute, error) self[attribute] << error end |
#add(attribute, message) ⇒ Object
Adds message
to the error messages on attribute
. More than one error can be added to the same attribute
errors.add(:name, 'is invalid')
# => ["is invalid"]
errors.add(:name, 'must be implemented')
# => ["is invalid", "must be implemented"]
errors.
# => {:name=>["must be implemented", "is invalid"]}
If message
is a proc, it will be called, allowing for things like Time.now
to be used within an error.
errors. # => {}
181 182 183 |
# File 'lib/attr_validator/validation_errors.rb', line 181 def add(attribute, ) self[attribute] << end |
#add_all(attribute, errors) ⇒ Object
Adds messages
to the error messages on attribute
.
errors.add(:name, ['is invalid', 'must present'])
# => ["is invalid", "must present"]
189 190 191 192 |
# File 'lib/attr_validator/validation_errors.rb', line 189 def add_all(attribute, errors) [attribute] ||= [] [attribute] += errors end |
#added?(attribute, message) ⇒ Boolean
Returns true
if an error on the attribute with the given message is present, false
otherwise. message
is treated the same as for add
.
errors.add :name, :blank
errors.added? :name, :blank # => true
199 200 201 |
# File 'lib/attr_validator/validation_errors.rb', line 199 def added?(attribute, ) self[attribute].include? end |
#clear ⇒ Object
Clear the error messages.
errors. # => ["name can not be nil"]
errors.clear
errors. # => []
13 14 15 |
# File 'lib/attr_validator/validation_errors.rb', line 13 def clear .clear end |
#count ⇒ Object
Returns the number of error messages.
errors.add(:name, "can't be blank")
errors.count # => 1
errors.add(:name, "must be specified")
errors.count # => 2
134 135 136 |
# File 'lib/attr_validator/validation_errors.rb', line 134 def count to_a.size end |
#delete(key) ⇒ Object
Delete messages for key
. Returns the deleted messages.
errors.get(:name) # => ["can not be nil"]
errors.delete(:name) # => ["can not be nil"]
errors.get(:name) # => nil
52 53 54 |
# File 'lib/attr_validator/validation_errors.rb', line 52 def delete(key) .delete(key) end |
#each ⇒ Object
Iterates through each error key, value pair in the error messages hash. Yields the attribute and the error for that attribute. If the attribute has more than one error message, yields once for each error message.
errors.add(:name, "can't be blank")
errors.each do |attribute, error|
# Will yield :name and "can't be blank"
end
errors.add(:name, "must be specified")
errors.each do |attribute, error|
# Will yield :name and "can't be blank"
# then yield :name and "must be specified"
end
87 88 89 90 91 |
# File 'lib/attr_validator/validation_errors.rb', line 87 def each .each_key do |attribute| self[attribute].each { |error| yield attribute, error } end end |
#empty? ⇒ Boolean Also known as: blank?
Returns true
if no errors are found, false
otherwise. If the error message is a string it can be empty.
errors. # => ["name can not be nil"]
errors.empty? # => false
143 144 145 |
# File 'lib/attr_validator/validation_errors.rb', line 143 def empty? .all? { |k, v| v && v.empty? && !v.is_a?(String) } end |
#full_message(attribute, message) ⇒ Object
Returns a full message for a given attribute.
errors.(:name, 'is invalid') # => "Name is invalid"
234 235 236 237 238 239 240 241 242 243 |
# File 'lib/attr_validator/validation_errors.rb', line 234 def (attribute, ) return if attribute == :base attr_name = attribute.to_s.tr('.', '_').humanize attr_name = @base.class.human_attribute_name(attribute, default: attr_name) I18n.t(:"errors.format", { default: "%{attribute} %{message}", attribute: attr_name, message: }) end |
#full_messages ⇒ Object
Returns all the full error messages in an array.
class PersonValidator
validates :name, :address, :email, presence: true
validates :name, length: { min: 5, max: 30 }
end
= create(address: '123 First St.')
errors.full_messages
# => ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Email can't be blank"]
213 214 215 |
# File 'lib/attr_validator/validation_errors.rb', line 213 def map { |attribute, | (attribute, ) } end |
#full_messages_for(attribute) ⇒ Object
Returns all the full error messages for a given attribute in an array.
class PersonValidator
validates :name, :address, :email, presence: true
validates :name, length: { min: 5, max: 30 }
end
= create()
errors.full_messages_for(:name)
# => ["Name is too short (minimum is 5 characters)", "Name can't be blank"]
227 228 229 |
# File 'lib/attr_validator/validation_errors.rb', line 227 def (attribute) (get(attribute) || []).map { || (attribute, ) } end |
#get(key) ⇒ Object
Get messages for key
.
errors. # => {:name=>["can not be nil"]}
errors.get(:name) # => ["can not be nil"]
errors.get(:age) # => nil
34 35 36 |
# File 'lib/attr_validator/validation_errors.rb', line 34 def get(key) [key] end |
#include?(attribute) ⇒ Boolean Also known as: has_key?
Returns true
if the error messages include an error for the given key attribute
, false
otherwise.
errors. # => {:name=>["can not be nil"]}
errors.include?(:name) # => true
errors.include?(:age) # => false
23 24 25 |
# File 'lib/attr_validator/validation_errors.rb', line 23 def include?(attribute) (v = [attribute]) && v.any? end |
#keys ⇒ Object
Returns all message keys.
errors. # => {:name=>["can not be nil", "must be specified"]}
errors.keys # => [:name]
115 116 117 |
# File 'lib/attr_validator/validation_errors.rb', line 115 def keys .keys end |
#set(key, value) ⇒ Object
Set messages for key
to value
.
errors.get(:name) # => ["can not be nil"]
errors.set(:name, ["can't be nil"])
errors.get(:name) # => ["can't be nil"]
43 44 45 |
# File 'lib/attr_validator/validation_errors.rb', line 43 def set(key, value) [key] = value end |
#size ⇒ Object
Returns the number of error messages.
errors.add(:name, "can't be blank")
errors.size # => 1
errors.add(:name, "must be specified")
errors.size # => 2
99 100 101 |
# File 'lib/attr_validator/validation_errors.rb', line 99 def size values.flatten.size end |
#to_a ⇒ Object
Returns an array of error messages, with the attribute name included.
errors.add(:name, "can't be blank")
errors.add(:name, "must be specified")
errors.to_a # => ["name can't be blank", "name must be specified"]
124 125 126 |
# File 'lib/attr_validator/validation_errors.rb', line 124 def to_a end |
#to_hash(full_messages = false) ⇒ Object
Returns a Hash of attributes with their error messages. If full_messages
is true
, it will contain full messages (see full_message
).
errors.to_hash # => {:name=>["can not be nil"]}
errors.to_hash(true) # => {:name=>["name can not be nil"]}
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/attr_validator/validation_errors.rb', line 154 def to_hash( = false) if = {} self..each do |attribute, array| [attribute] = array.map { || (attribute, ) } end else self..dup end end |
#values ⇒ Object
Returns all message values.
errors. # => {:name=>["can not be nil", "must be specified"]}
errors.values # => [["can not be nil", "must be specified"]]
107 108 109 |
# File 'lib/attr_validator/validation_errors.rb', line 107 def values .values end |