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
- #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
- #lambda_instance ⇒ Proc
- #lambda_string ⇒ String
-
#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
Instance Attribute Details
#lambda_binding ⇒ Binding
48 49 50 |
# File 'lib/bade/renderer.rb', line 48 def lambda_binding @lambda_binding end |
#locals ⇒ Hash
44 45 46 |
# File 'lib/bade/renderer.rb', line 44 def locals @locals end |
#precompiled ⇒ Precompiled
141 142 143 |
# File 'lib/bade/renderer.rb', line 141 def precompiled @precompiled ||= Precompiled.new(Generator.document_to_lambda_string(root_document), file_path) end |
#render_binding ⇒ RenderBinding
52 53 54 |
# File 'lib/bade/renderer.rb', line 52 def render_binding @render_binding end |
#source_text ⇒ String
36 37 38 |
# File 'lib/bade/renderer.rb', line 36 def source_text @source_text end |
Class Method Details
.from_file(file) ⇒ Renderer
Returns preconfigured instance of this class.
83 84 85 86 87 88 89 90 91 |
# File 'lib/bade/renderer.rb', line 83 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
99 100 101 102 103 |
# File 'lib/bade/renderer.rb', line 99 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.
72 73 74 75 76 77 |
# File 'lib/bade/renderer.rb', line 72 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
159 160 161 162 163 164 165 |
# File 'lib/bade/renderer.rb', line 159 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
147 148 149 |
# File 'lib/bade/renderer.rb', line 147 def lambda_string precompiled.code_string end |
#parsed_documents ⇒ Hash<String, Document>
Returns absolute path => document.
60 61 62 |
# File 'lib/bade/renderer.rb', line 60 def parsed_documents @parsed_documents ||= {} end |
#render(binding: nil, new_line: nil, indent: nil) ⇒ String
Returns rendered content of template.
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/bade/renderer.rb', line 177 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
131 132 133 |
# File 'lib/bade/renderer.rb', line 131 def root_document @root_document ||= _parsed_document(source_text, file_path) end |
#with_binding(binding) ⇒ Object
120 121 122 123 |
# File 'lib/bade/renderer.rb', line 120 def with_binding(binding) self.lambda_binding = binding self end |
#with_locals(locals = {}) ⇒ self
113 114 115 116 117 118 |
# File 'lib/bade/renderer.rb', line 113 def with_locals(locals = {}) self.render_binding = nil self.locals = locals self end |