Class: Diecut::Template
- Inherits:
-
Object
- Object
- Diecut::Template
- Defined in:
- lib/diecut/template.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#template_string ⇒ Object
readonly
Returns the value of attribute template_string.
Instance Method Summary collapse
- #add_subcontext(nesting, other) ⇒ Object
- #build_context_class ⇒ Object
- #context ⇒ Object
- #context_class ⇒ Object
-
#initialize(path, template_string) ⇒ Template
constructor
A new instance of Template.
- #partial_context(other) ⇒ Object
- #partials ⇒ Object
- #reduced ⇒ Object
- #render(renderer) ⇒ Object
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
#path ⇒ Object (readonly)
Returns the value of attribute path.
14 15 16 |
# File 'lib/diecut/template.rb', line 14 def path @path end |
#template_string ⇒ Object (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_class ⇒ Object
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 |
#context ⇒ Object
50 51 52 |
# File 'lib/diecut/template.rb', line 50 def context @context ||= context_class.new end |
#context_class ⇒ Object
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 |
#partials ⇒ Object
37 38 39 |
# File 'lib/diecut/template.rb', line 37 def partials reduced.partials end |
#reduced ⇒ Object
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 |