Sha256: 55c9033e062cbe69d8156cf4615c19a29bca424d90ef0d0a37fe047c35ff7796

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

require 'erb'

module SignatureGenerator

  class Processor

    include EasyAppHelper
    include EasyAppHelper::Input
    MAX_RETRY = 3

    attr_reader :results, :context
    attr_accessor :max_retry

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

    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

    def context=(context)
      raise SignatureGenerator::Error, 'Context cannot be nil' if context.nil?
      @context = case context
                   when Hash
                     SignatureGenerator::Context.new context
                   when SignatureGenerator::Context
                     context
                 end
    end

    private

    def context_as_binding(context)
      context.instance_eval do
        binding
      end
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
signature_generator-0.1.6 lib/signature_generator/processor.rb
signature_generator-0.1.5 lib/signature_generator/processor.rb