Class: Trenni::Template

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

Direct Known Subclasses

MarkupTemplate

Defined Under Namespace

Classes: Assembler

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer) ⇒ Template

Returns a new instance of Template.



97
98
99
# File 'lib/trenni/template.rb', line 97

def initialize(buffer)
	@buffer = buffer
end

Class Method Details

.buffer(binding) ⇒ Object

Returns the buffer used for capturing output.



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

def self.buffer(binding)
	binding.local_variable_get(OUT)
end

.capture(*args, &block) ⇒ Object

Returns the output produced by calling the given block.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/trenni/template.rb', line 44

def self.capture(*args, &block)
	scope = block.binding
	output_buffer = scope.local_variable_get(OUT)
	
	capture_buffer = String.new.force_encoding(output_buffer.encoding)
	scope.local_variable_set(OUT, capture_buffer)
	
	begin
		block.call(*args)
	ensure
		scope.local_variable_set(OUT, output_buffer)
	end
	
	return capture_buffer
end

.load_file(path, *args) ⇒ Object



93
94
95
# File 'lib/trenni/template.rb', line 93

def self.load_file(path, *args)
	self.new(FileBuffer.new(path), *args).freeze
end

Instance Method Details

#freezeObject



101
102
103
104
105
106
107
# File 'lib/trenni/template.rb', line 101

def freeze
	return self if frozen?
	
	to_proc
	
	super
end

#to_buffer(scope) ⇒ Object



117
118
119
# File 'lib/trenni/template.rb', line 117

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

#to_proc(scope = nil) ⇒ Object



121
122
123
# File 'lib/trenni/template.rb', line 121

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

#to_string(scope = Object.new, output = nil) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/trenni/template.rb', line 109

def to_string(scope = Object.new, output = nil)
	output ||= output_buffer
	
	scope.instance_exec(output, &to_proc)
	
	return output
end