Class: Makanai::TemplateEngine::Erb

Inherits:
Base
  • Object
show all
Defined in:
lib/makanai/template_engine/erb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text:, locals: {}) ⇒ Erb

Returns a new instance of Erb.



10
11
12
13
14
# File 'lib/makanai/template_engine/erb.rb', line 10

def initialize(text:, locals: {})
  super()
  @text = text
  @locals = locals
end

Instance Attribute Details

#localsObject (readonly)

Returns the value of attribute locals.



16
17
18
# File 'lib/makanai/template_engine/erb.rb', line 16

def locals
  @locals
end

#textObject (readonly)

Returns the value of attribute text.



16
17
18
# File 'lib/makanai/template_engine/erb.rb', line 16

def text
  @text
end

Instance Method Details

#resultObject



18
19
20
21
22
23
# File 'lib/makanai/template_engine/erb.rb', line 18

def result
  # NOTE: ERB is need to pass the binding.
  # So, pass the binding of the object that defined the instance variable.
  # ref: https://docs.ruby-lang.org/en/2.7.0/ERB.html#method-i-result
  ERB.new(text).result(Locals.new(locals).self_binding)
end