Class: Bade::Renderer

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

Defined Under Namespace

Classes: LoadError

Constant Summary collapse

TEMPLATE_FILE_NAME =
'(__template__)'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#file_pathString

Returns:



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

def file_path
  @file_path
end

#lambda_bindingBinding

Returns:

  • (Binding)


48
49
50
# File 'lib/bade/renderer.rb', line 48

def lambda_binding
  @lambda_binding
end

#localsHash

Returns:

  • (Hash)


44
45
46
# File 'lib/bade/renderer.rb', line 44

def locals
  @locals
end

#precompiledPrecompiled

Returns:



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_bindingRenderBinding

Returns:

  • (RenderBinding)


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

def render_binding
  @render_binding
end

#source_textString

Returns:



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.

Parameters:

  • file (String, File)

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

Returns:

  • (Renderer)

    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

Parameters:

Returns:

  • (Renderer)

    preconfigured instance of this class



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.

Parameters:

  • source (String)

    source string that should be parsed

Returns:

  • (Renderer)

    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_instanceProc

Returns:

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

Returns:



147
148
149
# File 'lib/bade/renderer.rb', line 147

def lambda_string
  precompiled.code_string
end

#parsed_documentsHash<String, Document>

Returns absolute path => document.

Returns:

  • (Hash<String, Document>)

    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.

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



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

Returns:



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

Parameters:

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

Returns:

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