module PmdTester::LiquidRenderer

A module to include in classes that use a Liquid template to generate content.

Public Instance Methods

copy_resource(dir, to_root) click to toggle source
# File lib/pmdtester/builders/liquid_renderer.rb, line 35
def copy_resource(dir, to_root)
  src = ResourceLocator.resource(dir)
  dest = "#{to_root}/#{dir}"
  FileUtils.copy_entry(src, dest)
end
render_and_write(template_path, target_file, env) click to toggle source
# File lib/pmdtester/builders/liquid_renderer.rb, line 20
def render_and_write(template_path, target_file, env)
  write_file(target_file, render_liquid(template_path, env))
end
render_liquid(template_path, env) click to toggle source
# File lib/pmdtester/builders/liquid_renderer.rb, line 12
def render_liquid(template_path, env)
  to_render = File.read(ResourceLocator.resource(template_path))
  includes = Liquid::LocalFileSystem.new(ResourceLocator.resource('_includes'), '%s.html')
  Liquid::Template.file_system = includes
  template = Liquid::Template.parse(to_render, error_mode: :strict)
  template.render!(env, { strict_variables: true })
end
write_file(target_file, contents) click to toggle source
# File lib/pmdtester/builders/liquid_renderer.rb, line 24
def write_file(target_file, contents)
  dir = File.dirname(target_file)
  FileUtils.mkdir_p(dir) unless File.directory?(dir)

  index = File.new(target_file, 'w')
  index&.puts contents # may be nil when stubbing
  logger&.info "Written #{target_file}"
ensure
  index&.close
end