Class: Bade::Renderer
Defined Under Namespace
Classes: LoadError
Class Attribute Summary collapse
-
.clear_constants ⇒ Boolean
When set to true it will remove all constants that template created.
Instance Attribute Summary collapse
-
#clear_constants ⇒ Boolean
When set to true it will remove all constants that template created.
- #file_path ⇒ String
- #lambda_binding ⇒ Binding
- #locals ⇒ Hash
- #optimize ⇒ Bool
- #precompiled ⇒ Precompiled
-
#render_binding ⇒ RenderBinding
rubocop:disable Lint/DuplicateMethods.
- #source_text ⇒ String
Class Method Summary collapse
- ._globals_tracker ⇒ Object
-
.from_file(file, clear_constants: Bade::Renderer.clear_constants) ⇒ Renderer
Preconfigured instance of this class.
-
.from_precompiled(precompiled, clear_constants: Bade::Renderer.clear_constants) ⇒ 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, clear_constants: Bade::Renderer.clear_constants) ⇒ Renderer
Preconfigured instance of this class.
Instance Method Summary collapse
-
#initialize(clear_constants: Bade::Renderer.clear_constants) ⇒ 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) ⇒ self
- #with_locals(locals = {}) ⇒ self
- #with_render_binding(binding) ⇒ self
Constructor Details
#initialize(clear_constants: Bade::Renderer.clear_constants) ⇒ Renderer
Returns a new instance of Renderer.
45 46 47 48 |
# File 'lib/bade/renderer.rb', line 45 def initialize(clear_constants: Bade::Renderer.clear_constants) @optimize = false @clear_constants = clear_constants end |
Class Attribute Details
.clear_constants ⇒ Boolean
When set to true it will remove all constants that template created. Off by default.
40 41 42 |
# File 'lib/bade/renderer.rb', line 40 def clear_constants @clear_constants end |
Instance Attribute Details
#clear_constants ⇒ Boolean
Returns When set to true it will remove all constants that template created. Off by default.
76 77 78 |
# File 'lib/bade/renderer.rb', line 76 def clear_constants @clear_constants end |
#lambda_binding ⇒ Binding
64 65 66 |
# File 'lib/bade/renderer.rb', line 64 def lambda_binding @lambda_binding end |
#locals ⇒ Hash
60 61 62 |
# File 'lib/bade/renderer.rb', line 60 def locals @locals end |
#optimize ⇒ Bool
72 73 74 |
# File 'lib/bade/renderer.rb', line 72 def optimize @optimize end |
#precompiled ⇒ Precompiled
179 180 181 182 |
# File 'lib/bade/renderer.rb', line 179 def precompiled @precompiled ||= Precompiled.new(Generator.document_to_lambda_string(root_document, optimize: @optimize), file_path) end |
#render_binding ⇒ RenderBinding
rubocop:disable Lint/DuplicateMethods
68 69 70 |
# File 'lib/bade/renderer.rb', line 68 def render_binding @render_binding end |
#source_text ⇒ String
52 53 54 |
# File 'lib/bade/renderer.rb', line 52 def source_text @source_text end |
Class Method Details
._globals_tracker ⇒ Object
33 34 35 |
# File 'lib/bade/renderer.rb', line 33 def _globals_tracker @globals_tracker ||= Bade::Runtime::GlobalsTracker.new end |
.from_file(file, clear_constants: Bade::Renderer.clear_constants) ⇒ Renderer
Returns preconfigured instance of this class.
107 108 109 110 111 112 113 114 115 |
# File 'lib/bade/renderer.rb', line 107 def self.from_file(file, clear_constants: Bade::Renderer.clear_constants) path = if file.is_a?(File) file.path else file end from_source(nil, path, clear_constants: clear_constants) end |
.from_precompiled(precompiled, clear_constants: Bade::Renderer.clear_constants) ⇒ Renderer
Method to create Renderer from Precompiled object, for example when you want to reuse precompiled object from disk
123 124 125 126 127 |
# File 'lib/bade/renderer.rb', line 123 def self.from_precompiled(precompiled, clear_constants: Bade::Renderer.clear_constants) inst = new(clear_constants: clear_constants) inst.precompiled = precompiled inst end |
.from_source(source, file_path = nil, clear_constants: Bade::Renderer.clear_constants) ⇒ Renderer
Returns preconfigured instance of this class.
96 97 98 99 100 101 |
# File 'lib/bade/renderer.rb', line 96 def self.from_source(source, file_path = nil, clear_constants: Bade::Renderer.clear_constants) inst = new(clear_constants: clear_constants) inst.source_text = source inst.file_path = file_path inst end |
Instance Method Details
#lambda_instance ⇒ Proc
200 201 202 203 204 205 206 207 208 |
# File 'lib/bade/renderer.rb', line 200 def lambda_instance _catching_globals do if lambda_binding lambda_binding.eval(lambda_string, file_path || Bade::Runtime::TEMPLATE_FILE_NAME) else render_binding.instance_eval(lambda_string, file_path || Bade::Runtime::TEMPLATE_FILE_NAME) end end end |
#lambda_string ⇒ String
186 187 188 |
# File 'lib/bade/renderer.rb', line 186 def lambda_string precompiled.code_string end |
#optimized ⇒ Object
158 159 160 161 |
# File 'lib/bade/renderer.rb', line 158 def optimized self.optimize = true self end |
#parsed_documents ⇒ Hash<String, Document>
Returns absolute path => document.
84 85 86 |
# File 'lib/bade/renderer.rb', line 84 def parsed_documents @parsed_documents ||= {} end |
#render(binding: nil, new_line: nil, indent: nil) ⇒ String
Returns rendered content of template.
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/bade/renderer.rb', line 220 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.compact! # remove nil values begin return _catching_globals do lambda_instance.call(**run_vars) end rescue Bade::Runtime::RuntimeError => e raise e rescue Exception => e # rubocop:disable Lint/RescueException msg = "Exception raised during execution of template: #{e}" raise Bade::Runtime::RuntimeError.wrap_existing_error(msg, e, render_binding.__location_stack) ensure self.class._globals_tracker.clear_constants if @clear_constants end end |
#root_document ⇒ Bade::AST::Node
169 170 171 |
# File 'lib/bade/renderer.rb', line 169 def root_document @root_document ||= _parsed_document(source_text, file_path) end |
#with_binding(binding) ⇒ self
145 146 147 148 |
# File 'lib/bade/renderer.rb', line 145 def with_binding(binding) self.lambda_binding = binding self end |
#with_locals(locals = {}) ⇒ self
137 138 139 140 141 142 |
# File 'lib/bade/renderer.rb', line 137 def with_locals(locals = {}) self.render_binding = nil self.locals = locals self end |
#with_render_binding(binding) ⇒ self
152 153 154 155 156 |
# File 'lib/bade/renderer.rb', line 152 def with_render_binding(binding) self.lambda_binding = nil self.render_binding = binding self end |