Class: Stencil::DynamicTemplate

Inherits:
Template
  • Object
show all
Defined in:
lib/stencil/dynamic-template.rb

Direct Known Subclasses

JSONTemplate

Constant Summary collapse

@@formatters =
Hash.new

Constants inherited from Template

Template::BeginDirective, Template::DefaultFileOpener, Template::EndDirective, Template::FinishDirective, Template::UntilDirective

Class Attribute Summary collapse

Attributes inherited from Template

#file_opener

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Template

file, #render, #render_with, string

Constructor Details

#initialize(&file_opener) ⇒ DynamicTemplate

Returns a new instance of DynamicTemplate.



21
22
23
24
25
26
# File 'lib/stencil/dynamic-template.rb', line 21

def initialize(&file_opener)
  @hash_template = parse(*self.class.hash_template)
  @array_template = parse(*self.class.array_template)
  @item_template = parse(*self.class.item_template)
  @file_opener = file_opener
end

Class Attribute Details

.array_templateObject (readonly)

Returns the value of attribute array_template.



64
65
66
# File 'lib/stencil/dynamic-template.rb', line 64

def array_template
  @array_template
end

.hash_templateObject (readonly)

Returns the value of attribute hash_template.



64
65
66
# File 'lib/stencil/dynamic-template.rb', line 64

def hash_template
  @hash_template
end

.item_templateObject (readonly)

Returns the value of attribute item_template.



64
65
66
# File 'lib/stencil/dynamic-template.rb', line 64

def item_template
  @item_template
end

Class Method Details

.formatter(kind, &file_opener) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/stencil/dynamic-template.rb', line 6

def self.formatter(kind, &file_opener)
  file_opener ||= DefaultFileOpener
  formatter = @@formatters[kind.to_s]
  if Class === formatter
    formatter = formatter.new(&file_opener)
    @@formatters[kind] = formatter
  end
  formatter
end

.hash(string) ⇒ Object



79
80
81
# File 'lib/stencil/dynamic-template.rb', line 79

def hash(string)
  @hash_template = [string] + line_and_file
end

.item(string) ⇒ Object



83
84
85
# File 'lib/stencil/dynamic-template.rb', line 83

def item(string)
  @item_template = [string] + line_and_file
end

.line_and_fileObject



66
67
68
69
70
71
72
73
# File 'lib/stencil/dynamic-template.rb', line 66

def line_and_file
  m = %r{^([^:]+):(\d+)}.match caller[1]
  if m.nil?
    return []
  else
    return [m[1], m[2].to_i]
  end
end

.list(string) ⇒ Object



75
76
77
# File 'lib/stencil/dynamic-template.rb', line 75

def list(string)
  @array_template = [string] + line_and_file
end

.register(kind) ⇒ Object



16
17
18
19
# File 'lib/stencil/dynamic-template.rb', line 16

def self.register(kind)
  @@formatters[kind.to_s] = self
  define_method(:my_name){kind.to_s}
end

Instance Method Details

#array_render(data) ⇒ Object



32
33
34
35
36
# File 'lib/stencil/dynamic-template.rb', line 32

def array_render(data)
  state = State.new(data, &@file_opener)
  res = render_with(state, @array_template)
  return res
end

#hash_render(data) ⇒ Object



38
39
40
41
42
43
# File 'lib/stencil/dynamic-template.rb', line 38

def hash_render(data)
  pairs = data.access("").map{|k,v| {"key" => k, "value" => v}}
  state = State.new(data, &@file_opener) 
  state.data.inject_dataset("pair", pairs)
  render_with(state, @hash_template)
end

#item_render(data) ⇒ Object



45
46
47
48
# File 'lib/stencil/dynamic-template.rb', line 45

def item_render(data)
  state = State.new(data, &@file_opener) 
  res = render_with(state, @item_template)
end

#parse(string, file, line) ⇒ Object



28
29
30
# File 'lib/stencil/dynamic-template.rb', line 28

def parse(string, file, line)
  super(string.gsub(/\[;\s*reapply/, "[;apply #{my_name}"), file, line)
end

#render_raw(data) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/stencil/dynamic-template.rb', line 50

def render_raw(data)
  res = nil
  case data.access("")
  when Array
    res = array_render(data)
  when Hash
    res = hash_render(data)
  else
    res = item_render(data)
  end
  return res
end