Class: ThisTown::Template

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

Instance Method Summary collapse

Constructor Details

#initialize(path, options) ⇒ Template

Returns a new instance of Template.



3
4
5
6
# File 'lib/this_town/template.rb', line 3

def initialize(path, options)
  @erb = ERB.new(File.read(path), nil, "-", "@output")
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/this_town/template.rb', line 35

def method_missing(name, *args, &block)
  if @options.has_key?(name)
    @options[name]
  else
    super
  end
end

Instance Method Details

#codeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/this_town/template.rb', line 12

def code
  constant_array.each_with_index do |constant, i|
    @output << (('  ' * i) + "module #{constant}\n")
  end

  # Pull a switcheroo to get the content
  old_output = @output
  @output = ""
  yield
  content = @output
  @output = old_output

  # Indent each line of content
  indent_num = constant_array.size - 1
  content.each_line do |line|
    @output << (('  ' * indent_num) + line)
  end

  (constant_array.size - 1).downto(0) do |i|
    @output << (('  ' * i) + "end\n")
  end
end

#resultObject



8
9
10
# File 'lib/this_town/template.rb', line 8

def result
  @erb.result(binding)
end