Class: Ippon::Validate::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/ippon/validate.rb

Overview

Stores information about errors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeErrors

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Errors.



381
382
383
384
# File 'lib/ippon/validate.rb', line 381

def initialize
  @steps = []
  @children = {}
end

Instance Attribute Details

#childrenHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns child results.

Returns:

  • (Hash)

    child results



378
379
380
# File 'lib/ippon/validate.rb', line 378

def children
  @children
end

#stepsArary<Step> (readonly)

Returns steps that produced an error.

Returns:

  • (Arary<Step>)

    steps that produced an error



374
375
376
# File 'lib/ippon/validate.rb', line 374

def steps
  @steps
end

Instance Method Details

#[](key) ⇒ Errors | nil

Returns a nested error.

Parameters:

  • key

Returns:

  • (Errors | nil)

    a nested error



402
403
404
405
406
# File 'lib/ippon/validate.rb', line 402

def [](key)
  if result = @children[key]
    result.errors
  end
end

#add_child(key, result) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds another Errors object as a child error.

Parameters:



439
440
441
442
443
444
445
# File 'lib/ippon/validate.rb', line 439

def add_child(key, result)
  if @children.has_key?(key)
    raise ArgumentError, "a result with key #{key.inspect} already exists"
  end
  @children[key] = result
  self
end

#add_step(step) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds a step as an error.

Parameters:



430
431
432
433
# File 'lib/ippon/validate.rb', line 430

def add_step(step)
  @steps << step
  self
end

#deep_freezeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deeply freezes the object.



388
389
390
391
392
393
# File 'lib/ippon/validate.rb', line 388

def deep_freeze
  @steps.freeze
  @children.freeze
  freeze
  self
end

#each_step_with_path {|step, path| ... } ⇒ self | Enumerator

Yields all steps (including nested steps) that caused an error.

Yields:

  • (step, path)

Yield Parameters:

  • step (Step)
  • path (Array)

Returns:

  • (self | Enumerator)

    an enumerator if no block is given.



413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/ippon/validate.rb', line 413

def each_step_with_path
  return enum_for(:each_step_with_path) if !block_given?

  @steps.each do |step|
    yield step, []
  end

  @children.each do |key, result|
    result.errors.each_step_with_path do |step, path|
      yield step, [key, *path]
    end
  end
end

#empty?Boolean

Returns true if there are no errors.

Returns:

  • (Boolean)

    true if there are no errors.



396
397
398
# File 'lib/ippon/validate.rb', line 396

def empty?
  @steps.empty? && @children.empty?
end

#merge!(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Merges another Errors object into this one.

Parameters:



450
451
452
453
454
455
456
# File 'lib/ippon/validate.rb', line 450

def merge!(other)
  @steps.concat(other.steps)
  @children.merge!(other.children) do |key|
    raise ArgumentError, "duplicate errors for key #{key.inspect}"
  end
  self
end