Class: Bade::Renderer
- Inherits:
-
Object
- Object
- Bade::Renderer
- Defined in:
- lib/bade/renderer.rb
Defined Under Namespace
Classes: LoadError
Constant Summary collapse
- TEMPLATE_FILE_NAME =
'(__template__)'.freeze
Instance Attribute Summary collapse
- #file_path ⇒ String
- #lambda_binding ⇒ Binding
- #locals ⇒ Hash
- #optimize ⇒ Bool
- #precompiled ⇒ Precompiled
- #render_binding ⇒ RenderBinding
- #source_text ⇒ String
Class Method Summary collapse
-
.from_file(file) ⇒ Renderer
Preconfigured instance of this class.
-
.from_precompiled(precompiled) ⇒ Renderer
Method to create Renderer from Precompiled object, for example when you want to reuse precompiled object from disk.
-
.from_source(source, file_path = nil) ⇒ Renderer
Preconfigured instance of this class.
Instance Method Summary collapse
-
#initialize ⇒ Renderer
constructor
A new instance of Renderer.
- #lambda_instance ⇒ Proc
- #lambda_string ⇒ String
- #optimized ⇒ Object
-
#parsed_documents ⇒ Hash<String, Document>
Absolute path => document.
-
#render(binding: nil, new_line: nil, indent: nil) ⇒ String
Rendered content of template.
- #root_document ⇒ Bade::AST::Node
- #with_binding(binding) ⇒ Object
- #with_locals(locals = {}) ⇒ self
Constructor Details
#initialize ⇒ Renderer
Returns a new instance of Renderer.
32 33 34 |
# File 'lib/bade/renderer.rb', line 32 def initialize @optimize = false end |
Instance Attribute Details
#lambda_binding ⇒ Binding
52 53 54 |
# File 'lib/bade/renderer.rb', line 52 def lambda_binding @lambda_binding end |
#locals ⇒ Hash
48 49 50 |
# File 'lib/bade/renderer.rb', line 48 def locals @locals end |
#optimize ⇒ Bool
60 61 62 |
# File 'lib/bade/renderer.rb', line 60 def optimize @optimize end |
#precompiled ⇒ Precompiled
154 155 156 157 |
# File 'lib/bade/renderer.rb', line 154 def precompiled @precompiled ||= Precompiled.new(Generator.document_to_lambda_string(root_document, optimize: @optimize), file_path) end |
#render_binding ⇒ RenderBinding
56 57 58 |
# File 'lib/bade/renderer.rb', line 56 def render_binding @render_binding end |
#source_text ⇒ String
40 41 42 |
# File 'lib/bade/renderer.rb', line 40 def source_text @source_text end |
Class Method Details
.from_file(file) ⇒ Renderer
Returns preconfigured instance of this class.
91 92 93 94 95 96 97 98 99 |
# File 'lib/bade/renderer.rb', line 91 def self.from_file(file) path = if file.is_a?(File) file.path else file end from_source(nil, path) end |
.from_precompiled(precompiled) ⇒ Renderer
Method to create Renderer from Precompiled object, for example when you want to reuse precompiled object from disk
107 108 109 110 111 |
# File 'lib/bade/renderer.rb', line 107 def self.from_precompiled(precompiled) inst = new inst.precompiled = precompiled inst end |
.from_source(source, file_path = nil) ⇒ Renderer
Returns preconfigured instance of this class.
80 81 82 83 84 85 |
# File 'lib/bade/renderer.rb', line 80 def self.from_source(source, file_path = nil) inst = new inst.source_text = source inst.file_path = file_path inst end |
Instance Method Details
#lambda_instance ⇒ Proc
173 174 175 176 177 178 179 |
# File 'lib/bade/renderer.rb', line 173 def lambda_instance if lambda_binding lambda_binding.eval(lambda_string, file_path || TEMPLATE_FILE_NAME) else render_binding.instance_eval(lambda_string, file_path || TEMPLATE_FILE_NAME) end end |
#lambda_string ⇒ String
161 162 163 |
# File 'lib/bade/renderer.rb', line 161 def lambda_string precompiled.code_string end |
#optimized ⇒ Object
133 134 135 136 |
# File 'lib/bade/renderer.rb', line 133 def optimized self.optimize = true self end |
#parsed_documents ⇒ Hash<String, Document>
Returns absolute path => document.
68 69 70 |
# File 'lib/bade/renderer.rb', line 68 def parsed_documents @parsed_documents ||= {} end |
#render(binding: nil, new_line: nil, indent: nil) ⇒ String
Returns rendered content of template.
191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/bade/renderer.rb', line 191 def render(binding: nil, new_line: nil, indent: nil) self.lambda_binding = binding unless binding.nil? # backward compatibility run_vars = { Generator::NEW_LINE_NAME.to_sym => new_line, Generator::BASE_INDENT_NAME.to_sym => indent, } run_vars.reject! { |_key, value| value.nil? } # remove nil values lambda_instance.call(**run_vars) end |
#root_document ⇒ Bade::AST::Node
144 145 146 |
# File 'lib/bade/renderer.rb', line 144 def root_document @root_document ||= _parsed_document(source_text, file_path) end |
#with_binding(binding) ⇒ Object
128 129 130 131 |
# File 'lib/bade/renderer.rb', line 128 def with_binding(binding) self.lambda_binding = binding self end |
#with_locals(locals = {}) ⇒ self
121 122 123 124 125 126 |
# File 'lib/bade/renderer.rb', line 121 def with_locals(locals = {}) self.render_binding = nil self.locals = locals self end |