Class: AntMapper::Errors
- Includes:
- Enumerable
- Defined in:
- lib/ant_mapper/validations.rb
Constant Summary collapse
- @@default_error_messages =
{ :inclusion => "is not included in the list", :exclusion => "is reserved", :invalid => "is invalid", :confirmation => "doesn't match confirmation", :accepted => "must be accepted", :empty => "can't be empty", :blank => "can't be blank", :too_long => "is too long (maximum is %d characters)", :too_short => "is too short (minimum is %d characters)", :wrong_length => "is the wrong length (should be %d characters)", :taken => "has already been taken", :not_a_number => "is not a number" }
Instance Method Summary collapse
-
#add(attribute, msg = @@default_error_messages[:invalid]) ⇒ Object
Adds an error message (
msg
) to theattribute
, which will be returned on a call toon(attribute)
for the same attribute and ensure that this error object returns false when asked ifempty?
. -
#add_on_blank(attributes, msg = ) ⇒ Object
Will add an error message to each of the attributes in
attributes
that is blank (using Object#blank?). -
#add_on_boundary_breaking(attributes, range, too_long_msg = , too_short_msg = ) ⇒ Object
(also: #add_on_boundry_breaking)
Will add an error message to each of the attributes in
attributes
that has a length outside of the passed boundaryrange
. -
#add_on_empty(attributes, msg = ) ⇒ Object
Will add an error message to each of the attributes in
attributes
that is empty. -
#add_to_base(msg) ⇒ Object
Adds an error to the base object instead of any particular attribute.
-
#clear ⇒ Object
Removes all errors that have been added.
-
#each ⇒ Object
Yields each attribute and associated message per error added.
-
#each_full ⇒ Object
Yields each full error message added.
-
#empty? ⇒ Boolean
Returns true if no errors have been added.
-
#full_messages(options = {}) ⇒ Object
Returns all the full error messages in an array.
-
#initialize(base) ⇒ Errors
constructor
:nodoc:.
- #invalid?(attribute) ⇒ Boolean
- #on(attribute) ⇒ Object (also: #[])
-
#on_base ⇒ Object
Returns errors assigned to the base object through
add_to_base
according to the normal rules ofon(attribute)
. -
#size ⇒ Object
(also: #count, #length)
Returns the total number of errors added.
Methods included from Enumerable
#each_with_object, #index_by, #many?, #sum
Constructor Details
#initialize(base) ⇒ Errors
:nodoc:
20 21 22 |
# File 'lib/ant_mapper/validations.rb', line 20 def initialize(base) # :nodoc: @base, @errors = base, {} end |
Instance Method Details
#add(attribute, msg = @@default_error_messages[:invalid]) ⇒ Object
Adds an error message (msg
) to the attribute
, which will be returned on a call to on(attribute)
for the same attribute and ensure that this error object returns false when asked if empty?
. More than one error can be added to the same attribute
in which case an array will be returned on a call to on(attribute)
. If no msg
is supplied, “invalid” is assumed.
54 55 56 57 |
# File 'lib/ant_mapper/validations.rb', line 54 def add(attribute, msg = @@default_error_messages[:invalid]) @errors[attribute.to_s] = [] if @errors[attribute.to_s].nil? @errors[attribute.to_s] << msg end |
#add_on_blank(attributes, msg = ) ⇒ Object
Will add an error message to each of the attributes in attributes
that is blank (using Object#blank?).
69 70 71 72 73 74 |
# File 'lib/ant_mapper/validations.rb', line 69 def add_on_blank(attributes, msg = @@default_error_messages[:blank]) for attr in [attributes].flatten value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s] add(attr, msg) if value.blank? end end |
#add_on_boundary_breaking(attributes, range, too_long_msg = , too_short_msg = ) ⇒ Object Also known as: add_on_boundry_breaking
Will add an error message to each of the attributes in attributes
that has a length outside of the passed boundary range
. If the length is above the boundary, the too_long_msg message will be used. If below, the too_short_msg.
78 79 80 81 82 83 84 |
# File 'lib/ant_mapper/validations.rb', line 78 def add_on_boundary_breaking(attributes, range, too_long_msg = @@default_error_messages[:too_long], too_short_msg = @@default_error_messages[:too_short]) for attr in [attributes].flatten value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s] add(attr, too_short_msg % range.begin) if value && value.length < range.begin add(attr, too_long_msg % range.end) if value && value.length > range.end end end |
#add_on_empty(attributes, msg = ) ⇒ Object
Will add an error message to each of the attributes in attributes
that is empty.
60 61 62 63 64 65 66 |
# File 'lib/ant_mapper/validations.rb', line 60 def add_on_empty(attributes, msg = @@default_error_messages[:empty]) for attr in [attributes].flatten value = @base.respond_to?(attr.to_s) ? @base.send(attr.to_s) : @base[attr.to_s] is_empty = value.respond_to?("empty?") ? value.empty? : false add(attr, msg) unless !value.nil? && !is_empty end end |
#add_to_base(msg) ⇒ Object
Adds an error to the base object instead of any particular attribute. This is used to report errors that don’t tie to any specific attribute, but rather to the object as a whole. These error messages don’t get prepended with any field name when iterating with each_full
, so they should be complete sentences.
46 47 48 |
# File 'lib/ant_mapper/validations.rb', line 46 def add_to_base(msg) add(:base, msg) end |
#clear ⇒ Object
Removes all errors that have been added.
172 173 174 |
# File 'lib/ant_mapper/validations.rb', line 172 def clear @errors = {} end |
#each ⇒ Object
Yields each attribute and associated message per error added.
class Company < AntMapper::Base
validates_presence_of :name, :address, :email
validates_length_of :name, :in => 5..30
end
company = Company.create(:address => '123 First St.')
company.errors.each{|attr,msg| puts "#{attr} - #{msg}" }
# => name - is too short (minimum is 5 characters)
# name - can't be blank
# address - can't be blank
118 119 120 |
# File 'lib/ant_mapper/validations.rb', line 118 def each @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } } end |
#each_full ⇒ Object
Yields each full error message added. So Person.errors.add("first_name", "can't be empty")
will be returned through iteration as “First name can’t be empty”.
class Company < AntMapper::Base
validates_presence_of :name, :address, :email
validates_length_of :name, :in => 5..30
end
company = Company.create(:address => '123 First St.')
company.errors.each_full{|msg| puts msg }
# => Name is too short (minimum is 5 characters)
# Name can't be blank
# Address can't be blank
135 136 137 |
# File 'lib/ant_mapper/validations.rb', line 135 def each_full .each { |msg| yield msg } end |
#empty? ⇒ Boolean
Returns true if no errors have been added.
167 168 169 |
# File 'lib/ant_mapper/validations.rb', line 167 def empty? @errors.empty? end |
#full_messages(options = {}) ⇒ Object
Returns all the full error messages in an array.
class Company < AntMapper::Base
validates_presence_of :name, :address, :email
validates_length_of :name, :in => 5..30
end
company = Company.create(:address => '123 First St.')
company.errors. # =>
["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"]
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/ant_mapper/validations.rb', line 149 def ( = {}) = [] @errors.each_key do |attr| @errors[attr].each do || next unless if attr == "base" << else << attr.to_s + ' ' + .to_s end end end end |
#invalid?(attribute) ⇒ Boolean
88 89 90 |
# File 'lib/ant_mapper/validations.rb', line 88 def invalid?(attribute) !@errors[attribute.to_s].nil? end |
#on(attribute) ⇒ Object Also known as: []
93 94 95 96 97 |
# File 'lib/ant_mapper/validations.rb', line 93 def on(attribute) errors = @errors[attribute.to_s] return nil if errors.nil? errors.size == 1 ? errors.first : errors end |
#on_base ⇒ Object
Returns errors assigned to the base object through add_to_base
according to the normal rules of on(attribute)
.
102 103 104 |
# File 'lib/ant_mapper/validations.rb', line 102 def on_base on(:base) end |
#size ⇒ Object Also known as: count, length
Returns the total number of errors added. Two errors added to the same attribute will be counted as such.
177 178 179 |
# File 'lib/ant_mapper/validations.rb', line 177 def size @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size } end |