Class: Etcher::Transformers::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/etcher/transformers/format.rb

Overview

Formats given key using existing and/or placeholder attributes.

Instance Method Summary collapse

Constructor Details

#initialize(key, *retainers, **mappings) ⇒ Format

Returns a new instance of Format.



11
12
13
14
15
16
# File 'lib/etcher/transformers/format.rb', line 11

def initialize key, *retainers, **mappings
  @key = key
  @retainers = retainers
  @mappings = mappings
  @pattern = /%<.+>s/o
end

Instance Method Details

#call(attributes) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/etcher/transformers/format.rb', line 18

def call attributes
  value = attributes[key]

  return Success attributes unless value && value.match?(pattern)

  Success attributes.merge!(key => format(value, **attributes, **pass_throughs))
rescue KeyError => error
  Failure step: :transform,
          constant: self.class,
          payload: "Unable to transform #{key.inspect}, missing specifier: " \
                   "\"#{error.message[/<.+>/]}\"."
end