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, *args) ⇒ Object
Instance Method Summary collapse
- #freeze ⇒ Object
-
#initialize(buffer) ⇒ Template
constructor
A new instance of Template.
- #to_buffer(scope) ⇒ Object
- #to_proc(scope = nil) ⇒ Object
- #to_string(scope = Object.new, output = nil) ⇒ Object
Constructor Details
#initialize(buffer) ⇒ Template
Returns a new instance of Template.
93 94 95 |
# File 'lib/trenni/template.rb', line 93 def initialize(buffer) @buffer = buffer end |
Class Method Details
.buffer(binding) ⇒ Object
Returns the buffer used for capturing output.
57 58 59 |
# File 'lib/trenni/template.rb', line 57 def self.buffer(binding) binding.local_variable_get(OUT) end |
.capture(*args, &block) ⇒ Object
Returns the output produced by calling the given block.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/trenni/template.rb', line 40 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
89 90 91 |
# File 'lib/trenni/template.rb', line 89 def self.load_file(path, *args) self.new(FileBuffer.new(path), *args).freeze end |
Instance Method Details
#freeze ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/trenni/template.rb', line 97 def freeze return self if frozen? to_proc super end |
#to_buffer(scope) ⇒ Object
111 112 113 |
# File 'lib/trenni/template.rb', line 111 def to_buffer(scope) Buffer.new(to_string(scope), path: @buffer.path) end |
#to_proc(scope = nil) ⇒ Object
115 116 117 |
# File 'lib/trenni/template.rb', line 115 def to_proc(scope = nil) @compiled_proc ||= eval("proc{|#{OUT}|;#{code};#{OUT}}", scope, @buffer.path).freeze end |
#to_string(scope = Object.new, output = nil) ⇒ Object
105 106 107 108 109 |
# File 'lib/trenni/template.rb', line 105 def to_string(scope = Object.new, output = nil) output ||= output_buffer scope.instance_exec(output, &to_proc) end |