Module: SinatraToTheMoon::Templates

Defined in:
lib/sinatra_to_the_moon/templates.rb

Constant Summary collapse

ROOT =
File.expand_path("templates", __dir__)

Class Method Summary collapse

Class Method Details

.files_for(profile) ⇒ Object



11
12
13
14
15
# File 'lib/sinatra_to_the_moon/templates.rb', line 11

def files_for(profile)
  template_files("base")
    .merge(template_files(profile))
    .transform_keys { |path| path.delete_suffix(".tt") }
end

.render(template, locals) ⇒ Object



17
18
19
# File 'lib/sinatra_to_the_moon/templates.rb', line 17

def render(template, locals)
  ERB.new(File.read(template), trim_mode: "-").result_with_hash(**locals)
end

.template_files(directory) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/sinatra_to_the_moon/templates.rb', line 21

def template_files(directory)
  root = File.join(ROOT, directory)
  return {} unless Dir.exist?(root)

  Dir.glob(File.join(root, "**", "*.tt"), File::FNM_DOTMATCH).sort.to_h do |file|
    [file.delete_prefix("#{root}/"), file]
  end
end