Class: Reform::Contract::CustomError

Inherits:
Object
  • Object
show all
Defined in:
lib/reform/contract/custom_error.rb

Overview

a “fake” Dry schema object to add into the @results array super ugly hack required for 2.3.x version since we are creating a new Reform::Errors instance every time we call form.errors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, error_text, results) ⇒ CustomError

Returns a new instance of CustomError.



7
8
9
10
11
12
13
14
15
16
# File 'lib/reform/contract/custom_error.rb', line 7

def initialize(key, error_text, results)
  @key        = key
  @error_text = error_text
  @errors     = {key => Array(error_text)}
  @messages   = @errors
  @hint       = {}
  @results    = results

  merge!
end

Instance Attribute Details

#hintObject (readonly)

Returns the value of attribute hint.



18
19
20
# File 'lib/reform/contract/custom_error.rb', line 18

def hint
  @hint
end

#messagesObject (readonly)

Returns the value of attribute messages.



18
19
20
# File 'lib/reform/contract/custom_error.rb', line 18

def messages
  @messages
end

Instance Method Details

#errors(**_args) ⇒ Object

dry 1.x errors method has 1 kwargs argument



29
30
31
# File 'lib/reform/contract/custom_error.rb', line 29

def errors(**_args)
  @errors
end

#failure?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/reform/contract/custom_error.rb', line 24

def failure?
  true
end

#merge!Object



33
34
35
36
37
38
# File 'lib/reform/contract/custom_error.rb', line 33

def merge!
  # to_h required for dry_v 1.x since the errors are Dry object instead of an hash
  @results.map(&:errors)
          .detect { |hash| hash.to_h.key?(@key) }
          .tap { |hash| hash.nil? ? @results << self : hash.to_h[@key] |= Array(@error_text) }
end

#success?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/reform/contract/custom_error.rb', line 20

def success?
  false
end