Class: Fortitude::MethodTemplates::SimpleCompiledTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/fortitude/method_templates/simple_compiled_template.rb

Defined Under Namespace

Classes: EvaluationObject

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_file) ⇒ SimpleCompiledTemplate

Returns a new instance of SimpleCompiledTemplate.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fortitude/method_templates/simple_compiled_template.rb', line 14

def initialize(source_file)
  @source_file = source_file
  method_text_lines = [
    "def result",
    "  output = ''"
  ]
  pending_fixed_strings = [ ]

  File.read(source_file).split(/\r\n|\r|\n/).each do |line|
    needs_end = false
    line = line.chomp

    if line =~ /^(.*)\#\s*\:if\s*(.*?)\s*$/i
      if pending_fixed_strings.length > 0
        method_text_lines << "    output << <<EOS"
        method_text_lines += pending_fixed_strings
        method_text_lines << "EOS"
        pending_fixed_strings = [ ]
      end

      line = $1
      condition = $2
      method_text_lines << "  if #{condition}"
      method_text_lines << "    output << <<EOS"
      method_text_lines << line
      method_text_lines << "EOS"
      method_text_lines << "  end"
    else
      pending_fixed_strings << line
    end
  end

  if pending_fixed_strings.length > 0
    method_text_lines << "    output << <<EOS"
    method_text_lines += pending_fixed_strings
    method_text_lines << "EOS"
    pending_fixed_strings = [ ]
  end

  method_text_lines << "  output"
  method_text_lines << "end"

  @method_text = method_text_lines.join("\n")
  @evaluation_object = EvaluationObject.new
  metaclass = (class << @evaluation_object; self; end)
  metaclass.class_eval(method_text_lines.join("\n"))
end

Class Method Details

.template(name) ⇒ Object



8
9
10
11
# File 'lib/fortitude/method_templates/simple_compiled_template.rb', line 8

def template(name)
  @templates ||= { }
  @templates[name] ||= new(File.join(File.dirname(__FILE__), "#{name}.rb.smpl"))
end

Instance Method Details

#result(bindings) ⇒ Object



87
88
89
90
91
# File 'lib/fortitude/method_templates/simple_compiled_template.rb', line 87

def result(bindings)
  @evaluation_object.hash = bindings
  out = @evaluation_object.result
  out
end