Class: Karafka::Core::Contractable::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/core/contractable/result.rb

Overview

Representation of a validaton result with resolved error messages

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors, contract) ⇒ Result

Builds a result object and remaps (if needed) error keys to proper error messages

Parameters:

  • errors (Array<Array>)

    array with sub-arrays with paths and error keys

  • contract (Object)

    contract that generated the error



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/karafka/core/contractable/result.rb', line 14

def initialize(errors, contract)
  # Short track to skip object allocation for the happy path
  if errors.empty?
    @errors = errors
    return
  end

  hashed = {}

  errors.each do |error|
    scope = error.first.map(&:to_s).join('.').to_sym

    # This will allow for usage of custom messages instead of yaml keys if needed
    hashed[scope] = if error.last.is_a?(String)
                      error.last
                    else
                      build_message(contract, scope, error.last)
                    end
  end

  @errors = hashed
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/karafka/core/contractable/result.rb', line 8

def errors
  @errors
end

Instance Method Details

#success?Boolean

Returns true if no errors.

Returns:

  • (Boolean)

    true if no errors



38
39
40
# File 'lib/karafka/core/contractable/result.rb', line 38

def success?
  errors.empty?
end