Class: Bade::Renderer

Inherits:
Object show all
Defined in:
lib/bade/renderer.rb

Defined Under Namespace

Classes: LoadError

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clear_constants: Bade::Renderer.clear_constants) ⇒ Renderer

Returns a new instance of Renderer.

Parameters:

  • clear_constants (Boolean) (defaults to: Bade::Renderer.clear_constants)

    When set to true it will remove all constants that template created. Off by default.



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_constantsBoolean

When set to true it will remove all constants that template created. Off by default.

Returns:

  • (Boolean)


40
41
42
# File 'lib/bade/renderer.rb', line 40

def clear_constants
  @clear_constants
end

Instance Attribute Details

#clear_constantsBoolean

Returns When set to true it will remove all constants that template created. Off by default.

Returns:

  • (Boolean)

    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

#file_pathString

Returns:



56
57
58
# File 'lib/bade/renderer.rb', line 56

def file_path
  @file_path
end

#lambda_bindingBinding

Returns:

  • (Binding)


64
65
66
# File 'lib/bade/renderer.rb', line 64

def lambda_binding
  @lambda_binding
end

#localsHash

Returns:

  • (Hash)


60
61
62
# File 'lib/bade/renderer.rb', line 60

def locals
  @locals
end

#optimizeBool

Returns:

  • (Bool)


72
73
74
# File 'lib/bade/renderer.rb', line 72

def optimize
  @optimize
end

#precompiledPrecompiled

Returns:



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_bindingRenderBinding

rubocop:disable Lint/DuplicateMethods

Returns:

  • (RenderBinding)


68
69
70
# File 'lib/bade/renderer.rb', line 68

def render_binding
  @render_binding
end

#source_textString

Returns:



52
53
54
# File 'lib/bade/renderer.rb', line 52

def source_text
  @source_text
end

Class Method Details

._globals_trackerObject



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.

Parameters:

  • file (String, File)

    file path or file instance, file that should be loaded and parsed

Returns:

  • (Renderer)

    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

Parameters:

Returns:

  • (Renderer)

    preconfigured instance of this class



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.

Parameters:

  • source (String)

    source string that should be parsed

Returns:

  • (Renderer)

    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_instanceProc

Returns:

  • (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_stringString

Returns:



186
187
188
# File 'lib/bade/renderer.rb', line 186

def lambda_string
  precompiled.code_string
end

#optimizedObject



158
159
160
161
# File 'lib/bade/renderer.rb', line 158

def optimized
  self.optimize = true
  self
end

#parsed_documentsHash<String, Document>

Returns absolute path => document.

Returns:

  • (Hash<String, Document>)

    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.

Parameters:

  • binding (Binding) (defaults to: nil)

    custom binding for evaluating the template, but it is not recommended to use, use :locals and #with_locals instead

  • new_line (String) (defaults to: nil)

    newline string, default is n

  • indent (String) (defaults to: nil)

    indent string, default is two spaces

Returns:

  • (String)

    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_documentBade::AST::Node

Returns:



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

Returns:

  • (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

Parameters:

  • locals (Hash) (defaults to: {})

Returns:

  • (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

Parameters:

  • binding (RenderBinding)

Returns:

  • (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