Class: XRB::Template::Assembler

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

Overview

Assembles parser events into executable Ruby source code for a template.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encoding: Encoding::UTF_8) ⇒ Assembler

Initialize an empty template assembler.



58
59
60
# File 'lib/xrb/template.rb', line 58

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

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



62
63
64
# File 'lib/xrb/template.rb', line 62

def code
  @code
end

Instance Method Details

#expression(code) ⇒ Object

Output a string interpolation.



78
79
80
# File 'lib/xrb/template.rb', line 78

def expression(code)
	@code << "#{OUT}<<(#{code});"
end

#instruction(text, postfix = nil) ⇒ Object

Output a ruby expression (or part of).



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

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

#text(text) ⇒ Object

Output raw text to the template. Raw text should be escaped, e.g. it can't contain tags or other special characters.



66
67
68
69
70
# File 'lib/xrb/template.rb', line 66

def text(text)
	# We have to use an approach which preserves newlines and tabs as raw characters.
	text = text.gsub("'", "\\\\'")
	@code << "#{OUT}.raw('#{text}');"
end