Class: StrokeDB::Validations::InstanceMethods::Errors

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/document/validations.rb

Instance Method Summary collapse

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

#clearObject

Removes all errors that have been added.


471
472
473
# File 'lib/document/validations.rb', line 471

def clear
  @errors = {}
end

#eachObject

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.

Returns:

  • (Boolean)

466
467
468
# File 'lib/document/validations.rb', line 466

def empty?
  @errors.empty?
end

#invalid?(slot) ⇒ Boolean

Returns:

  • (Boolean)

453
454
455
# File 'lib/document/validations.rb', line 453

def invalid?(slot)
  !@errors[slot.to_s].nil?
end

#messagesObject

Returns all the error messages in an array.


476
477
478
# File 'lib/document/validations.rb', line 476

def messages
  @errors.values.inject([]) { |error_messages, slot| error_messages + 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

#sizeObject 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