Class: Trenni::Template::Assembler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter: String, encoding: Encoding::UTF_8) ⇒ Assembler



51
52
53
54
# File 'lib/trenni/template.rb', line 51

def initialize(filter: String, encoding: Encoding::UTF_8)
  @code = String.new.force_encoding(encoding)
  @filter = filter
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



56
57
58
# File 'lib/trenni/template.rb', line 56

def code
  @code
end

Instance Method Details

#expression(text) ⇒ Object

Output a string interpolation.



73
74
75
76
# File 'lib/trenni/template.rb', line 73

def expression(text)
  # Double brackets are required here to handle expressions like #{foo rescue "bar"}.
  @code << "#{OUT}<<#{@filter}((#{text}));"
end

#instruction(text, postfix = nil) ⇒ Object

Output a ruby expression (or part of).



68
69
70
# File 'lib/trenni/template.rb', line 68

def instruction(text, postfix = nil)
  @code << text << (postfix || ';')
end

#text(text) ⇒ Object

Output raw text to the template.



59
60
61
62
63
64
65
# File 'lib/trenni/template.rb', line 59

def text(text)
  text = text.gsub("'", "\\\\'")
  @code << "#{OUT}<<'#{text}';"
  
  # This is an interesting approach, but it doens't preserve newlines or tabs as raw characters, so template line numbers don't match up.
  # @parts << "#{OUT}<<#{text.dump};"
end