Class: Trenni::Template
- Inherits:
-
Object
- Object
- Trenni::Template
- Defined in:
- lib/trenni/template.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Assembler
Class Method Summary collapse
-
.buffer(binding) ⇒ Object
Returns the buffer used for capturing output.
-
.capture(*args, &block) ⇒ Object
Returns the output produced by calling the given block.
- .load_file(path, **options) ⇒ Object
Instance Method Summary collapse
-
#initialize(buffer, filter: String) ⇒ Template
constructor
A new instance of Template.
- #to_buffer(scope) ⇒ Object
- #to_proc(scope = nil) ⇒ Object
- #to_string(scope = Object.new, output = output_buffer) ⇒ Object
Constructor Details
#initialize(buffer, filter: String) ⇒ Template
83 84 85 86 |
# File 'lib/trenni/template.rb', line 83 def initialize(buffer, filter: String) @buffer = buffer @filter = filter end |
Class Method Details
.buffer(binding) ⇒ Object
Returns the buffer used for capturing output.
46 47 48 |
# File 'lib/trenni/template.rb', line 46 def self.buffer(binding) binding.local_variable_get(OUT) end |
.capture(*args, &block) ⇒ Object
Returns the output produced by calling the given block.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/trenni/template.rb', line 29 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, **options) ⇒ Object
79 80 81 |
# File 'lib/trenni/template.rb', line 79 def self.load_file(path, **) self.new(FileBuffer.new(path), **) end |
Instance Method Details
#to_buffer(scope) ⇒ Object
94 95 96 |
# File 'lib/trenni/template.rb', line 94 def to_buffer(scope) Buffer.new(to_string(scope), path: @buffer.path) end |
#to_proc(scope = nil) ⇒ Object
98 99 100 |
# File 'lib/trenni/template.rb', line 98 def to_proc(scope = nil) @compiled_proc ||= eval("proc{|#{OUT}|;#{code};#{OUT}}", scope, @buffer.path) end |
#to_string(scope = Object.new, output = output_buffer) ⇒ Object
88 89 90 91 92 |
# File 'lib/trenni/template.rb', line 88 def to_string(scope = Object.new, output = output_buffer) output ||= output_buffer scope.instance_exec(output, &to_proc) end |