class TerraformTemplateRenderer::Renderer

Renderer to remder an ERB template with variables taken from a json string instead of from the original binding

Public Class Methods

new(template, template_path) click to toggle source
# File lib/terraform_template_renderer/renderer.rb, line 10
def initialize(template, template_path)
  # The third argument enables trim mode using a hyphen
  @erb_template = ERB.new(template, nil, "-")
  @template_path = template_path
end

Public Instance Methods

render(json_variables) click to toggle source

The passed in json_variables needs to be a JSON object (not array), all the keys will be used as variables in the templates

# File lib/terraform_template_renderer/renderer.rb, line 18
def render(json_variables)
  binding_ = template_binding(json_variables)
  render_with_binding(binding_)
end
render_with_binding(binding_) click to toggle source
# File lib/terraform_template_renderer/renderer.rb, line 23
def render_with_binding(binding_)
  @erb_template.result(binding_.bind)
end

Private Instance Methods

add_params_to_object(object, params) click to toggle source
# File lib/terraform_template_renderer/renderer.rb, line 35
def add_params_to_object(object, params)
  params.each do |key, value|
    object.add_param(key, value)
  end
end
template_binding(json_variables) click to toggle source
# File lib/terraform_template_renderer/renderer.rb, line 29
def template_binding(json_variables)
  Binding.new(@template_path).tap do |binding_object|
    add_params_to_object(binding_object, JSON.parse(json_variables))
  end
end