module MediaWiktory::Generator::Renderable

Public Instance Methods

get_binding() click to toggle source
# File lib/mediawiktory/generator/renderable.rb, line 46
def get_binding # rubocop:disable Style/AccessorMethodName
  binding
end
partial(template, context = nil) click to toggle source
# File lib/mediawiktory/generator/renderable.rb, line 30
def partial(template, context = nil)
  # puts "Rendering #{template} with #{context || self}"

  # Never repeat this at home, dear children.
  if context
    instance_variables.each { |var| context.instance_variable_set(var, instance_variable_get(var)) }
  end
  path = File.expand_path("../templates/#{template}.erb", __FILE__)
  ERB.new(File.read(path))
     .tap { |tpl| tpl.filename = path }
     .result(context ? context.get_binding : binding)
rescue => e
  puts "#{e} while #{template} with #{context || self}"
  raise
end
render(template, **vars) click to toggle source
# File lib/mediawiktory/generator/renderable.rb, line 21
def render(template, **vars)
  # puts "Rendering #{template} with #{self}#{vars}"
  vars.each { |name, val| instance_variable_set("@#{name}", val) }
  path = File.expand_path("../templates/#{template}.erb", __FILE__)
  ERB.new(File.read(path))
     .tap { |tpl| tpl.filename = path }
     .result(binding)
end
render_to(path, **vars) click to toggle source
# File lib/mediawiktory/generator/renderable.rb, line 12
def render_to(path, **vars)
  FileUtils.mkdir_p File.dirname(path)
  File.write(path, to_html(**vars))
end
to_h() click to toggle source
Calls superclass method
# File lib/mediawiktory/generator/renderable.rb, line 8
def to_h
  stringify_hash(super, recursive: true)
end
to_html(**vars) click to toggle source
# File lib/mediawiktory/generator/renderable.rb, line 17
def to_html(**vars)
  render(vars.fetch(:template, main_template), **vars)
end

Private Instance Methods

stringify_hash(hash, recursive: false) click to toggle source
# File lib/mediawiktory/generator/renderable.rb, line 52
def stringify_hash(hash, recursive: false)
  hash.map { |k, v|
    [k.to_s, v.is_a?(Hash) && recursive ? stringify_hash(v, recursive: true) : v.to_s]
  }.to_h
end