class Proforma::HtmlRenderer::Writer

This class can render ERB templates.

Constants

BACK_DIR
ERB_OPTIONS
TEMPLATE_DIR

Attributes

directory[R]
files[R]

Public Class Methods

new(directory: nil, files: {}) click to toggle source
# File lib/proforma/html_renderer/writer.rb, line 27
def initialize(directory: nil, files: {})
  @directory  = directory || default_directory
  @files      = to_erb_hash(files)
end

Public Instance Methods

render(name, context = {}) click to toggle source
# File lib/proforma/html_renderer/writer.rb, line 32
def render(name, context = {})
  erb_template(name).result(hash_to_binding(context))
end

Private Instance Methods

default_directory() click to toggle source
# File lib/proforma/html_renderer/writer.rb, line 53
def default_directory
  File.join(
    __dir__,
    BACK_DIR,
    BACK_DIR,
    BACK_DIR,
    TEMPLATE_DIR
  )
end
erb_template(name) click to toggle source
# File lib/proforma/html_renderer/writer.rb, line 44
def erb_template(name)
  files[name.to_s] ||= read(name)
end
hash_to_binding(hash) click to toggle source
# File lib/proforma/html_renderer/writer.rb, line 40
def hash_to_binding(hash)
  ErbBinding.new(hash).expose_binding
end
path(name) click to toggle source
# File lib/proforma/html_renderer/writer.rb, line 63
def path(name)
  File.join(directory, name.to_s)
end
read(name) click to toggle source
# File lib/proforma/html_renderer/writer.rb, line 48
def read(name)
  text = IO.read(path(name)).chop
  ERB.new(text, nil, ERB_OPTIONS)
end
to_erb_hash(hash) click to toggle source
# File lib/proforma/html_renderer/writer.rb, line 67
def to_erb_hash(hash)
  (hash || {}).map do |(name, file)|
    [
      name.to_s,
      file.is_a?(ERB) ? file : ERB.new(file)
    ]
  end.to_h
end