Class: Diecut::TemplateSet

Inherits:
Object
  • Object
show all
Includes:
TSort
Defined in:
lib/diecut/template-set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTemplateSet

Returns a new instance of TemplateSet.



10
11
12
13
14
15
16
17
18
# File 'lib/diecut/template-set.rb', line 10

def initialize
  @templates = {}
  @path_templates = {}
  @breaking_cycles = {}
  @partials = {}
  @context_class = nil
  @context = nil
  @renderer = nil
end

Instance Attribute Details

#contextObject



39
40
41
# File 'lib/diecut/template-set.rb', line 39

def context
  @context ||= context_class.new
end

#partialsObject (readonly)

Returns the value of attribute partials.



19
20
21
# File 'lib/diecut/template-set.rb', line 19

def partials
  @partials
end

#path_templatesObject (readonly)

Returns the value of attribute path_templates.



19
20
21
# File 'lib/diecut/template-set.rb', line 19

def path_templates
  @path_templates
end

#templatesObject (readonly)

Returns the value of attribute templates.



19
20
21
# File 'lib/diecut/template-set.rb', line 19

def templates
  @templates
end

Instance Method Details

#add(path, contents) ⇒ Object



21
22
23
24
25
26
27
28
29
# 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_templatesObject



31
32
33
# File 'lib/diecut/template-set.rb', line 31

def all_templates
  @templates.values + @path_templates.values
end

#associate_partialsObject



87
88
89
90
91
92
93
94
95
96
97
# 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_contextObject



99
100
101
102
103
104
105
106
# 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_classObject



35
36
37
# File 'lib/diecut/template-set.rb', line 35

def context_class
  @context_class ||= Configurable.build_subclass("General context")
end

#is_partial?(tmpl) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/diecut/template-set.rb', line 44

def is_partial?(tmpl)
  @partials.has_key?(tmpl.path)
end

#prepareObject



62
63
64
65
# File 'lib/diecut/template-set.rb', line 62

def prepare
  associate_partials
  build_context
end

#rendererObject



67
68
69
70
71
# File 'lib/diecut/template-set.rb', line 67

def renderer
  @renderer ||= Mustache.new.tap do |mustache|
    mustache.partials_hash = partials
  end
end

#resultsObject



73
74
75
76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object



53
54
55
56
57
58
59
60
# 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) ⇒ Object



48
49
50
51
# File 'lib/diecut/template-set.rb', line 48

def tsort_each_node(&block)
  @breaking_cycles.clear
  @templates.each_value(&block)
end