Class: Trenni::Template::Assembler

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

Constant Summary collapse

CODE_PREFIX =
"#{OUT}=[];".freeze
CODE_POSTFIX =
"#{OUT}".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAssembler

Returns a new instance of Assembler.



44
45
46
# File 'lib/trenni/template.rb', line 44

def initialize
	@parts = []
end

Instance Attribute Details

#partsObject (readonly)

Returns the value of attribute parts.



48
49
50
# File 'lib/trenni/template.rb', line 48

def parts
  @parts
end

Instance Method Details

#codeObject



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

def code
	return [CODE_PREFIX, *@parts, CODE_POSTFIX].join
end

#expression(text) ⇒ Object

Output a ruby expression (or part of).



60
61
62
# File 'lib/trenni/template.rb', line 60

def expression(text)
	@parts << "#{text};"
end

#interpolation(text) ⇒ Object

Output a string interpolation.



65
66
67
# File 'lib/trenni/template.rb', line 65

def interpolation(text)
	@parts << "#{OUT}<<(#{text});"
end

#text(text) ⇒ Object

Output raw text to the template.



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

def text(text)
	text = text.gsub("'", "\\\\'")
	@parts << "#{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