Class: SignatureGenerator::Processor

Inherits:
Object
  • Object
show all
Includes:
EasyAppHelper, EasyAppHelper::Input
Defined in:
lib/signature_generator/processor.rb

Constant Summary collapse

MAX_RETRY =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context: {}, max_retry: MAX_RETRY) ⇒ Processor

Returns a new instance of Processor.



14
15
16
17
# File 'lib/signature_generator/processor.rb', line 14

def initialize(context: {}, max_retry: MAX_RETRY)
  self.context = context
  self.max_retry = max_retry
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



11
12
13
# File 'lib/signature_generator/processor.rb', line 11

def context
  @context
end

#max_retryObject

Returns the value of attribute max_retry.



12
13
14
# File 'lib/signature_generator/processor.rb', line 12

def max_retry
  @max_retry
end

#resultsObject (readonly)

Returns the value of attribute results.



11
12
13
# File 'lib/signature_generator/processor.rb', line 11

def results
  @results
end

Instance Method Details

#transform(template, context = self.context) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/signature_generator/processor.rb', line 19

def transform(template, context = self.context)
  counters = {}
  begin
    if config[:'inline-images']
      template = SignatureGenerator::Inliner.new(template).inlined
    end
    @results = ERB.new(template, nil, '-').result(context_as_binding context)
  rescue NameError => e
    missing_var = e.name
    counters[missing_var] ||= 0
    counters[missing_var] += 1
    debug_msg = "Variable not provided: #{missing_var} (attempt ##{counters[missing_var]})"
    logger.debug debug_msg
    if counters[missing_var] > max_retry
      logger.error 'Maximum retry number exceeded. Aborting !'
      raise e
    end
    user_input = get_user_input("Please enter value for '#{missing_var}' > ").gsub /\\n/, '<br>'
    context[missing_var] = user_input unless user_input.nil? or user_input.empty?
    retry
  end
end