class Diecut::TemplateSet

Attributes

context[W]
partials[R]
path_templates[R]
templates[R]

Public Class Methods

new() click to toggle source
# File lib/diecut/template-set.rb, line 10
def initialize
  @templates = {}
  @path_templates = {}
  @breaking_cycles = {}
  @partials = {}
  @context_class = nil
  @context = nil
  @renderer = nil
end

Public Instance Methods

add(path, contents) click to toggle source
# File lib/diecut/template-set.rb, line 21
def add(path, contents)
  template = Diecut::Template.new(path, contents)
  @templates[path] = template
  path_template = Diecut::Template.new("path for " + path, path)
  @path_templates[path] = path_template
  template.partials.each do |name, _|
    @partials[name] = template
  end
end
all_templates() click to toggle source
# File lib/diecut/template-set.rb, line 31
def all_templates
  @templates.values + @path_templates.values
end
associate_partials() click to toggle source
# File lib/diecut/template-set.rb, line 87
def associate_partials
  partials = []
  tsort_each do |template|
    partials.each do |partial|
      template.partial_context(partial)
    end
    if is_partial?(template)
      partials << template
    end
  end
end
build_context() click to toggle source
# File lib/diecut/template-set.rb, line 99
def build_context
  tsort_each do |template|
    context_class.absorb_context(template.context_class)
  end
  @path_templates.each_value do |template|
    context_class.absorb_context(template.context_class)
  end
end
context() click to toggle source
# File lib/diecut/template-set.rb, line 39
def context
  @context ||= context_class.new
end
context_class() click to toggle source
# File lib/diecut/template-set.rb, line 35
def context_class
  @context_class ||= Configurable.build_subclass("General context")
end
is_partial?(tmpl) click to toggle source
# File lib/diecut/template-set.rb, line 44
def is_partial?(tmpl)
  @partials.has_key?(tmpl.path)
end
prepare() click to toggle source
# File lib/diecut/template-set.rb, line 62
def prepare
  associate_partials
  build_context
end
renderer() click to toggle source
# File lib/diecut/template-set.rb, line 67
def renderer
  @renderer ||= Mustache.new.tap do |mustache|
    mustache.partials_hash = partials
  end
end
results() { |render, render| ... } click to toggle source
# File lib/diecut/template-set.rb, line 73
def results
  context.check_required

  tsort_each do |template|
    next if is_partial?(template)

    path = @path_templates[template.path]
    context.copy_settings_to(template.context)
    context.copy_settings_to(path.context)

    yield path.render(renderer), template.render(renderer)
  end
end
tsort_each_child(node) { |templates| ... } click to toggle source
# File lib/diecut/template-set.rb, line 53
def tsort_each_child(node)
  node.partials.each do |name, _|
    unless @breaking_cycles[name]
      @breaking_cycles[name] = true
      yield @templates[name]
    end
  end
end
tsort_each_node(&block) click to toggle source
# File lib/diecut/template-set.rb, line 48
def tsort_each_node(&block)
  @breaking_cycles.clear
  @templates.each_value(&block)
end