Class: Producer::Core::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/producer/core/template.rb,
lib/producer/core/template/erb_renderer.rb,
lib/producer/core/template/raw_renderer.rb,
lib/producer/core/template/yaml_renderer.rb

Defined Under Namespace

Classes: ERBRenderer, RawRenderer, YAMLRenderer

Constant Summary collapse

SEARCH_PATH =
'templates'.freeze
RENDERERS =
{
  /\.erb\z/   => ERBRenderer,
  /\.yaml\z/  => YAMLRenderer,
  //          => RawRenderer
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(path, search_path: SEARCH_PATH, renderers: RENDERERS) ⇒ Template

Returns a new instance of Template.



11
12
13
14
15
# File 'lib/producer/core/template.rb', line 11

def initialize path, search_path: SEARCH_PATH, renderers: RENDERERS
  @path         = Pathname.new(path)
  @search_path  = Pathname.new(search_path)
  @renderers    = renderers
end

Instance Method Details

#render(variables = {}) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/producer/core/template.rb', line 17

def render variables = {}
  candidates.each do |c|
    _, r = @renderers.find { |k, _| c.to_s =~ k }
    return r.render c, variables if r
  end

  fail TemplateMissingError, "template `#{@path}' not found"
end