Class: ConfCtl::ErbTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/confctl/erb_template.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, vars) ⇒ ErbTemplate

Returns a new instance of ErbTemplate.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/confctl/erb_template.rb', line 15

def initialize(name, vars)
  @_tpl = ERB.new(
    File.read(
      File.join(ConfCtl.root, 'template', "#{name}.erb")
    ), trim_mode: '-'
  )

  vars.each do |k, v|
    if v.is_a?(Proc)
      define_singleton_method(k, &v)
    elsif v.is_a?(Method)
      define_singleton_method(k) { |*args| v.call(*args) }
    else
      define_singleton_method(k) { v }
    end
  end
end

Class Method Details

.render(name, vars) ⇒ Object



5
6
7
8
# File 'lib/confctl/erb_template.rb', line 5

def self.render(name, vars)
  t = new(name, vars)
  t.render
end

.render_to(name, vars, path) ⇒ Object



10
11
12
13
# File 'lib/confctl/erb_template.rb', line 10

def self.render_to(name, vars, path)
  File.write("#{path}.new", render(name, vars))
  File.rename("#{path}.new", path)
end

Instance Method Details

#renderObject



33
34
35
# File 'lib/confctl/erb_template.rb', line 33

def render
  @_tpl.result(binding)
end