class Project::Template

Attributes

path[RW]
project[RW]

Public Class Methods

new(project:, path: @project = project) click to toggle source
# File app/project/template.rb, line 4
def initialize project:, path:
  @project = project
  @path    = path
  @content = File.read path
end

Public Instance Methods

clean_filename() click to toggle source
# File app/project/template.rb, line 41
def clean_filename
  File.basename(clean_path)
end
clean_full_path() click to toggle source
# File app/project/template.rb, line 29
def clean_full_path
  path.gsub(
    /#{engine_names.reverse.collect{|e| ".#{e}"}.join}$/, ''
  )
end
clean_path() click to toggle source
# File app/project/template.rb, line 35
def clean_path
  path.gsub(/^#{project.path}/, '').gsub(
    /#{engine_names.reverse.collect{|e| ".#{e}"}.join}$/, ''
  )
end
engine_names() click to toggle source
# File app/project/template.rb, line 45
def engine_names
  path.split(".").reverse.collect{ |layer|
    layer.to_sym if @@engines.include? layer.to_sym
  }.compact
end
render(context: {}) click to toggle source
# File app/project/template.rb, line 12
def render context: {}
  cache = @content
  engine_names.each do |layer|
    if layer == :mustache
      require "mustache"
      cache = Mustache.render(@content, **context)
    elsif layer == :erb
      # https://zaiste.net/rendering_erb_template_with_bindings_from_hash/
      require "ostruct"; require "erb"
      cache = ERB.new(@content).result(
        OpenStruct.new(context).instance_eval{binding}
      )
    end
  end
  return cache
end