Class: Trenni::Template

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

Defined Under Namespace

Classes: Assembler, Scanner

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer) ⇒ Template

Returns a new instance of Template.



170
171
172
# File 'lib/trenni/template.rb', line 170

def initialize(buffer)
	@buffer = buffer
end

Class Method Details

.buffer(binding) ⇒ Object

Returns the buffer used for capturing output.



39
40
41
# File 'lib/trenni/template.rb', line 39

def self.buffer(binding)
	eval(OUT, binding)
end

.capture(*args, &block) ⇒ Object

Returns the output produced by calling the given block.



29
30
31
32
33
34
35
36
# File 'lib/trenni/template.rb', line 29

def self.capture(*args, &block)
	out = eval(OUT, block.binding)
	top = out.size
	
	block.call(*args)
	
	return out.pop(out.size - top).join
end

.load_file(path) ⇒ Object



166
167
168
# File 'lib/trenni/template.rb', line 166

def self.load_file(path)
	self.new(FileBuffer.new(path))
end

Instance Method Details

#to_array(scope) ⇒ Object



186
187
188
189
190
191
192
193
194
# File 'lib/trenni/template.rb', line 186

def to_array(scope)
	if Binding === scope
		# Slow code path, evaluate the code string in the given binding (scope).
		eval(code, scope, @buffer.path)
	else
		# Faster code path, use instance_eval on a compiled Proc.
		scope.instance_eval(&to_proc)
	end
end

#to_buffer(scope) ⇒ Object



178
179
180
# File 'lib/trenni/template.rb', line 178

def to_buffer(scope)
	Buffer.new(to_array(scope).join, path: @buffer.path)
end

#to_procObject



196
197
198
# File 'lib/trenni/template.rb', line 196

def to_proc
	@compiled_proc ||= eval("proc{;#{code};}", binding, @buffer.path)
end

#to_string(scope = Object.new) ⇒ Object Also known as: evaluate, result



174
175
176
# File 'lib/trenni/template.rb', line 174

def to_string(scope = Object.new)
	to_array(scope).join
end