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

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

Instance Method Summary collapse

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

#clearObject

Removes all errors that have been added.



473
474
475
# File 'lib/strokedb/document/dsl/validations.rb', line 473

def clear
  @errors = {}
end

#eachObject

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.

Returns:

  • (Boolean)


468
469
470
# File 'lib/strokedb/document/dsl/validations.rb', line 468

def empty?
  @errors.empty?
end

#invalid?(slot) ⇒ Boolean

Returns:

  • (Boolean)


455
456
457
# File 'lib/strokedb/document/dsl/validations.rb', line 455

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

#messagesObject

Returns all the error messages in an array.



478
479
480
# File 'lib/strokedb/document/dsl/validations.rb', line 478

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

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



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