Class: Interactor::Validation::Errors

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/interactor/validation/errors.rb

Overview

Minimal error collection

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Constructor Details

#initialize(halt_checker: nil) ⇒ Errors

Returns a new instance of Errors.



11
12
13
14
# File 'lib/interactor/validation/errors.rb', line 11

def initialize(halt_checker: nil)
  @errors = []
  @halt_checker = halt_checker
end

Instance Method Details

#add(attribute, type = :invalid, message: nil, **options) ⇒ Object

Raises:



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/interactor/validation/errors.rb', line 16

def add(attribute, type = :invalid, message: nil, **options)
  @errors << Error.new(
    attribute: attribute,
    type: type,
    message: message || type.to_s,
    options: options
  )

  # Raise HaltValidation if halt is configured
  raise HaltValidation if @halt_checker&.call
end

#any?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/interactor/validation/errors.rb', line 32

def any?
  !empty?
end

#clearObject



36
37
38
# File 'lib/interactor/validation/errors.rb', line 36

def clear
  @errors.clear
end

#eachObject



40
41
42
# File 'lib/interactor/validation/errors.rb', line 40

def each(&)
  @errors.each(&)
end

#empty?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/interactor/validation/errors.rb', line 28

def empty?
  @errors.empty?
end

#to_aObject



44
45
46
# File 'lib/interactor/validation/errors.rb', line 44

def to_a
  @errors.dup
end