Class: Definition::ConformError

Inherits:
Object
  • Object
show all
Defined in:
lib/definition/conform_error.rb

Direct Known Subclasses

KeyConformError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, message, sub_errors: [], **options) ⇒ ConformError

Returns a new instance of ConformError.



7
8
9
10
11
12
13
14
15
# File 'lib/definition/conform_error.rb', line 7

def initialize(definition, message, sub_errors: [], **options)
  self.definition = definition
  self.message = message
  self.sub_errors = sub_errors
  self.i18n_key = options.fetch(:i18n_key, definition.name)
  self.i18n_context = options.fetch(:i18n_context, {})
  self.translated_error = options.fetch(:translated_message, nil)
  assign_parents
end

Instance Attribute Details

#definitionObject

Returns the value of attribute definition.



17
18
19
# File 'lib/definition/conform_error.rb', line 17

def definition
  @definition
end

#i18n_contextObject

Returns the value of attribute i18n_context.



17
18
19
# File 'lib/definition/conform_error.rb', line 17

def i18n_context
  @i18n_context
end

#i18n_keyObject

Returns the value of attribute i18n_key.



17
18
19
# File 'lib/definition/conform_error.rb', line 17

def i18n_key
  @i18n_key
end

#messageObject



20
21
22
23
24
25
26
# File 'lib/definition/conform_error.rb', line 20

def message
  if sub_errors.empty?
    @message
  else
    "#{@message}: { " + sub_errors.map(&:message).join(", ") + " }"
  end
end

#parentObject

Returns the value of attribute parent.



17
18
19
# File 'lib/definition/conform_error.rb', line 17

def parent
  @parent
end

#sub_errorsObject

Returns the value of attribute sub_errors.



17
18
19
# File 'lib/definition/conform_error.rb', line 17

def sub_errors
  @sub_errors
end

#translated_error(namespace = "definition") ⇒ Object



55
56
57
# File 'lib/definition/conform_error.rb', line 55

def translated_error(namespace = "definition")
  @translated_error ||= definition.error_renderer.new(self, i18n_namespace: namespace).translated_error
end

Instance Method Details

#error_pathObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/definition/conform_error.rb', line 34

def error_path
  current = self
  path = current.is_a?(KeyConformError) ? [key] : []
  while (current = current.parent)
    next unless current.is_a?(KeyConformError)

    path += [current.key]
  end
  path.reverse
end

#json_pointerObject



45
46
47
# File 'lib/definition/conform_error.rb', line 45

def json_pointer
  "/#{error_path.join('/')}"
end

#leaf_errorsObject



49
50
51
52
53
# File 'lib/definition/conform_error.rb', line 49

def leaf_errors
  return [self] if sub_errors.empty?

  sub_errors.map(&:leaf_errors).flatten
end

#to_sObject Also known as: inspect



28
29
30
# File 'lib/definition/conform_error.rb', line 28

def to_s
  "<Definition::ConformError \n\t message: \"#{message}\", \n\t json_pointer: \"#{json_pointer}\">"
end