Class: StrokeDB::Validations::InstanceMethods::Errors
- Includes:
- Enumerable
- Defined in:
- lib/strokedb/document/dsl/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
Constructor Details
#initialize(base) ⇒ Errors
Returns a new instance of Errors.
445 446 447 |
# File 'lib/strokedb/document/dsl/validations.rb', line 445 def initialize(base) @base, @errors = base, {} end |
Instance Method Details
#add(slot, msg) ⇒ Object
449 450 451 452 453 |
# File 'lib/strokedb/document/dsl/validations.rb', line 449 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.
473 474 475 |
# File 'lib/strokedb/document/dsl/validations.rb', line 473 def clear @errors = {} end |
#each ⇒ Object
Yields each attribute and associated message per error added
489 490 491 492 493 494 495 |
# File 'lib/strokedb/document/dsl/validations.rb', line 489 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.
468 469 470 |
# File 'lib/strokedb/document/dsl/validations.rb', line 468 def empty? @errors.empty? end |
#invalid?(slot) ⇒ Boolean
455 456 457 |
# File 'lib/strokedb/document/dsl/validations.rb', line 455 def invalid?(slot) !@errors[slot.to_s].nil? end |
#messages ⇒ Object
Returns all the error messages in an array.
478 479 480 |
# File 'lib/strokedb/document/dsl/validations.rb', line 478 def @errors.values.inject([]) { |, slot| + slot } end |
#on(slot) ⇒ Object Also known as: []
459 460 461 462 463 |
# File 'lib/strokedb/document/dsl/validations.rb', line 459 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.
484 485 486 |
# File 'lib/strokedb/document/dsl/validations.rb', line 484 def size @errors.values.inject(0) { |error_count, slot| error_count + slot.size } end |