Class: Roart::Errors
- Inherits:
-
Object
- Object
- Roart::Errors
- Includes:
- Enumerable
- Defined in:
- lib/roart/validations.rb
Instance Method Summary collapse
- #add(field, message) ⇒ Object
- #add_to_base(msg) ⇒ Object
-
#clear ⇒ Object
Removes all errors that have been added.
- #each ⇒ Object
-
#empty? ⇒ Boolean
Returns true if no errors have been added.
-
#initialize(obj) ⇒ Errors
constructor
A new instance of Errors.
- #on(field) ⇒ Object (also: #[])
- #on_base ⇒ Object
-
#size ⇒ Object
(also: #count, #length)
Returns the total number of errors added.
Constructor Details
#initialize(obj) ⇒ Errors
Returns a new instance of Errors.
6 7 8 |
# File 'lib/roart/validations.rb', line 6 def initialize(obj) @base, @errors = obj, {} end |
Instance Method Details
#add(field, message) ⇒ Object
14 15 16 17 |
# File 'lib/roart/validations.rb', line 14 def add(field, ) @errors[field.to_sym] ||= [] @errors[field.to_sym] << end |
#add_to_base(msg) ⇒ Object
10 11 12 |
# File 'lib/roart/validations.rb', line 10 def add_to_base(msg) add(:base, msg) end |
#clear ⇒ Object
Removes all errors that have been added.
41 42 43 |
# File 'lib/roart/validations.rb', line 41 def clear @errors = {} end |
#each ⇒ Object
31 32 33 |
# File 'lib/roart/validations.rb', line 31 def each @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } } end |
#empty? ⇒ Boolean
Returns true if no errors have been added.
36 37 38 |
# File 'lib/roart/validations.rb', line 36 def empty? @errors.empty? end |
#on(field) ⇒ Object Also known as: []
23 24 25 26 27 |
# File 'lib/roart/validations.rb', line 23 def on(field) errors = @errors[field.to_sym] return nil if errors.nil? errors end |
#on_base ⇒ Object
19 20 21 |
# File 'lib/roart/validations.rb', line 19 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.
46 47 48 |
# File 'lib/roart/validations.rb', line 46 def size @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size } end |