Class: Krane::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/krane/renderer.rb

Defined Under Namespace

Classes: InvalidPartialError, PartialNotFound, TemplateContext

Instance Method Summary collapse

Constructor Details

#initialize(current_sha:, template_dir:, logger:, bindings: {}) ⇒ Renderer

Returns a new instance of Renderer.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/krane/renderer.rb', line 19

def initialize(current_sha:, template_dir:, logger:, bindings: {})
  @current_sha = current_sha
  @template_dir = template_dir
  @partials_dirs =
    %w(partials ../partials).map { |d| File.expand_path(File.join(@template_dir, d)) }
  @logger = logger
  @bindings = bindings
  # Max length of podname is only 63chars so try to save some room by truncating sha to 8 chars
  @id = if ENV["TASK_ID"]
    ENV["TASK_ID"]
  elsif current_sha
    current_sha[0...8] + "-#{SecureRandom.hex(4)}"
  end
end

Instance Method Details

#render_partial(partial, locals) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/krane/renderer.rb', line 49

def render_partial(partial, locals)
  variables = template_variables.merge(locals)
  erb_binding = TemplateContext.new(self).template_binding
  bind_template_variables(erb_binding, variables)
  erb_binding.local_variable_set("locals", locals)

  partial_path = find_partial(partial)
  template = File.read(partial_path)
  expanded_template = ERB.new(template, nil, '-').result(erb_binding)

  docs = Psych.parse_stream(expanded_template, partial_path)
  # If the partial contains multiple documents or has an explicit document header,
  # we know it cannot validly be indented in the parent, so return it immediately.
  return expanded_template unless docs.children.one? && docs.children.first.implicit
  # Make sure indentation isn't a problem by producing a single line of parseable YAML.
  # Note that JSON is a subset of YAML.
  JSON.generate(docs.children.first.to_ruby)
rescue PartialNotFound => err
  # get the filename from the first parent, not the missing partial itself
  raise err if err.filename == partial
  raise InvalidPartialError.new(err.message, filename: partial, content: expanded_template || template)
rescue InvalidPartialError => err
  err.parents = err.parents.dup.unshift(File.basename(partial_path))
  raise err
rescue StandardError => err
  raise InvalidPartialError.new(err.message, filename: partial_path, content: expanded_template || template)
end

#render_template(filename, raw_template) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/krane/renderer.rb', line 34

def render_template(filename, raw_template)
  return raw_template unless File.extname(filename) == ".erb"

  erb_binding = TemplateContext.new(self).template_binding
  bind_template_variables(erb_binding, template_variables)

  ERB.new(raw_template, nil, '-').result(erb_binding)
rescue InvalidPartialError => err
  err.parents = err.parents.dup.unshift(filename)
  err.filename = "#{err.filename} (partial included from: #{err.parents.join(' -> ')})"
  raise err
rescue StandardError => err
  raise InvalidTemplateError.new(err.message, filename: filename, content: raw_template)
end