Class: Diecut::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/diecut/template.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, template_string) ⇒ Template

Returns a new instance of Template.



6
7
8
9
10
11
12
# File 'lib/diecut/template.rb', line 6

def initialize(path, template_string)
  @path = path
  @template_string = template_string
  @reduced = nil
  @context_class = nil
  @context = nil
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/diecut/template.rb', line 14

def path
  @path
end

#template_stringObject (readonly)

Returns the value of attribute template_string.



14
15
16
# File 'lib/diecut/template.rb', line 14

def template_string
  @template_string
end

Instance Method Details

#add_subcontext(nesting, other) ⇒ Object



23
24
25
26
27
# File 'lib/diecut/template.rb', line 23

def add_subcontext(nesting, other)
  other.field_names.each do |field|
    context_class.build_setting(nesting + [field])
  end
end

#build_context_classObject



41
42
43
44
45
46
47
48
# File 'lib/diecut/template.rb', line 41

def build_context_class
  klass = Configurable.build_subclass(path)

  reduced.leaf_fields.each do |field|
    klass.build_setting(field, reduced.sections.include?(field))
  end
  klass
end

#contextObject



50
51
52
# File 'lib/diecut/template.rb', line 50

def context
  @context ||= context_class.new
end

#context_classObject



29
30
31
# File 'lib/diecut/template.rb', line 29

def context_class
  @context_class ||= build_context_class
end

#partial_context(other) ⇒ Object



16
17
18
19
20
21
# File 'lib/diecut/template.rb', line 16

def partial_context(other)
  reduced.partials.each do |path, nesting|
    next unless path == other.path
    add_subcontext(nesting, other.context_class)
  end
end

#partialsObject



37
38
39
# File 'lib/diecut/template.rb', line 37

def partials
  reduced.partials
end

#reducedObject



33
34
35
# File 'lib/diecut/template.rb', line 33

def reduced
  @reduced ||= TemplateReducer.new(Mustache::Parser.new.compile(template_string))
end

#render(renderer) ⇒ Object



54
55
56
# File 'lib/diecut/template.rb', line 54

def render(renderer)
  renderer.render(template_string, context.to_hash)
end