Class: ChefApply::Text::ErrorTranslation

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_apply/text/error_translation.rb

Defined Under Namespace

Classes: InvalidDisplayAttributes

Constant Summary collapse

ATTRIBUTES =
%i{decorations header footer stack log}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(id, params: []) ⇒ ErrorTranslation

Returns a new instance of ErrorTranslation.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/chef_apply/text/error_translation.rb', line 24

def initialize(id, params: [])
  # To get access to the metadata we'll go directly through the parsed yaml.
  # Accessing via R18n is unnecessarily complicated
  yml = Text._error_table

  # We'll still use our Text mechanism for the text itself so that
  # parameters, pluralization, etc will still work.
  # This will raise if the key doesn't exist.
  @message = Text.errors.send(id).text(*params)
  options = yml[:display_defaults]

  # Override any defaults if display metadata is given
  display_opts = yml[id.to_sym][:display]
  options = options.merge(display_opts) unless display_opts.nil?

  ATTRIBUTES.each do |attribute|
    instance_variable_set("@#{attribute}", options.delete(attribute))
  end

  if options.length > 0
    # Anything not in ATTRIBUTES is not supported. This will also catch
    # typos in attr names
    raise InvalidDisplayAttributes.new(id, options)
  end
end

Instance Method Details

#inspectObject



50
51
52
53
54
55
56
57
# File 'lib/chef_apply/text/error_translation.rb', line 50

def inspect
  inspection = "#{self}: "
  ATTRIBUTES.each do |attribute|
    inspection << "#{attribute}: #{send(attribute.to_s)}; "
  end
  inspection << "message: #{message.gsub("\n", "\\n")}"
  inspection
end