Class: StrokeDB::Validations::InstanceMethods::Errors
- Includes:
- Enumerable
- Defined in:
- lib/document/validations.rb
Instance Method Summary collapse
- #add(slot, msg) ⇒ Object
-
#clear ⇒ Object
Removes all errors that have been added.
-
#each ⇒ Object
Yields each attribute and associated message per error added.
-
#empty? ⇒ Boolean
Returns true if no errors have been added.
-
#initialize(base) ⇒ Errors
constructor
A new instance of Errors.
- #invalid?(slot) ⇒ Boolean
-
#messages ⇒ Object
Returns all the error messages in an array.
- #on(slot) ⇒ Object (also: #[])
-
#size ⇒ Object
(also: #count, #length)
Returns the total number of errors added.
Methods included from Enumerable
#each_consecutive_pair, #group_by, #map_with_index, #trigger_partition
Constructor Details
#initialize(base) ⇒ Errors
Returns a new instance of Errors.
443 444 445 |
# File 'lib/document/validations.rb', line 443 def initialize(base) @base, @errors = base, {} end |
Instance Method Details
#add(slot, msg) ⇒ Object
447 448 449 450 451 |
# File 'lib/document/validations.rb', line 447 def add(slot, msg) slot = slot.to_s @errors[slot] = [] if @errors[slot].nil? @errors[slot] << msg end |
#clear ⇒ Object
Removes all errors that have been added.
471 472 473 |
# File 'lib/document/validations.rb', line 471 def clear @errors = {} end |
#each ⇒ Object
Yields each attribute and associated message per error added
487 488 489 490 491 492 493 |
# File 'lib/document/validations.rb', line 487 def each @errors.each_key do |slot| @errors[slot].each do |msg| yield [ msg ] end end end |
#empty? ⇒ Boolean
Returns true if no errors have been added.
466 467 468 |
# File 'lib/document/validations.rb', line 466 def empty? @errors.empty? end |
#invalid?(slot) ⇒ Boolean
453 454 455 |
# File 'lib/document/validations.rb', line 453 def invalid?(slot) !@errors[slot.to_s].nil? end |
#messages ⇒ Object
Returns all the error messages in an array.
476 477 478 |
# File 'lib/document/validations.rb', line 476 def @errors.values.inject([]) { |, slot| + slot } end |
#on(slot) ⇒ Object Also known as: []
457 458 459 460 461 |
# File 'lib/document/validations.rb', line 457 def on(slot) errors = @errors[slot.to_s] return nil if errors.nil? errors.size == 1 ? errors.first : errors end |
#size ⇒ Object Also known as: count, length
Returns the total number of errors added. Two errors added to the same slot will be counted as such.
482 483 484 |
# File 'lib/document/validations.rb', line 482 def size @errors.values.inject(0) { |error_count, slot| error_count + slot.size } end |