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: [], i18n_key: definition.name) ⇒ ConformError

Returns a new instance of ConformError.



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

def initialize(definition, message, sub_errors: [], i18n_key: definition.name)
  self.definition = definition
  self.message = message
  self.sub_errors = sub_errors
  self.i18n_key = i18n_key
  assign_parents
end

Instance Attribute Details

#definitionObject

Returns the value of attribute definition.



15
16
17
# File 'lib/definition/conform_error.rb', line 15

def definition
  @definition
end

#i18n_keyObject

Returns the value of attribute i18n_key.



15
16
17
# File 'lib/definition/conform_error.rb', line 15

def i18n_key
  @i18n_key
end

#messageObject



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

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

#parentObject

Returns the value of attribute parent.



15
16
17
# File 'lib/definition/conform_error.rb', line 15

def parent
  @parent
end

#sub_errorsObject

Returns the value of attribute sub_errors.



15
16
17
# File 'lib/definition/conform_error.rb', line 15

def sub_errors
  @sub_errors
end

Instance Method Details

#error_pathObject



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

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



43
44
45
# File 'lib/definition/conform_error.rb', line 43

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

#leaf_errorsObject



47
48
49
50
51
# File 'lib/definition/conform_error.rb', line 47

def leaf_errors
  return [self] if sub_errors.empty?

  sub_errors.map(&:leaf_errors).flatten
end

#to_sObject Also known as: inspect



26
27
28
# File 'lib/definition/conform_error.rb', line 26

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

#translated_error(namespace = "definition", vars: {}) ⇒ Object



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

def translated_error(namespace = "definition", vars: {})
  namespace ||= "definition"
  vars[:key] = key if respond_to?(:key)
  ::I18n.t("#{namespace}.#{i18n_key}", definition.context.merge!(vars))
end