Sha256: 7a22c29a6996ab2399d396ecbd923302396c9a3e21a4b1695b302d4a7a1c2b2c

Contents?: true

Size: 785 Bytes

Versions: 1

Compression:

Stored size: 785 Bytes

Contents

require "erb"
require "json"

module TerraformTemplateRenderer
  class Renderer
    def initialize(template)
      # The third argument enables trim mode using a hyphen
      @erb_template = ERB.new(template, nil, "-")
    end

    # The passed in json_variables needs to be a JSON object (not array), all the keys will be used
    # as variables in the templates
    def render(json_variables)
      @erb_template.result(template_binding(json_variables))
    end

    private

    def template_binding(json_variables)
      Binding.new.tap { |binding_object| add_params_to_object(binding_object, JSON.parse(json_variables)) }.bind
    end

    def add_params_to_object(object, params)
      params.each do |key, value|
        object.add_param(key, value)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
terraform-template-renderer-0.2.0 lib/terraform_template_renderer/renderer.rb