Class: HaveAPI::GoClient::ErbTemplate

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/haveapi/go_client/erb_template.rb

Constant Summary

Constants included from Utils

Utils::GO_KEYWORDS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#camelize, #go_comment_text, #go_json_tag, #go_module_path, #go_package_name, #go_query_key, #go_string_literal, #safe_file_component

Constructor Details

#initialize(name, vars) ⇒ ErbTemplate

Returns a new instance of ErbTemplate.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/haveapi/go_client/erb_template.rb', line 31

def initialize(name, vars)
  @_tpl = ERB.new(File.new(HaveAPI::GoClient.tpl(name)).read, 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



9
10
11
12
# File 'lib/haveapi/go_client/erb_template.rb', line 9

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

.render_to(name, vars, path) ⇒ Object



14
15
16
17
# File 'lib/haveapi/go_client/erb_template.rb', line 14

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

.render_to_if_changed(name, vars, path) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/haveapi/go_client/erb_template.rb', line 19

def self.render_to_if_changed(name, vars, path)
  tmp_path = "#{path}.new"
  File.write(tmp_path, render(name, vars))

  if !File.exist?(path) || !FileUtils.identical?(path, tmp_path)
    File.rename(tmp_path, path)

  else
    File.unlink(tmp_path)
  end
end

Instance Method Details

#renderObject



45
46
47
# File 'lib/haveapi/go_client/erb_template.rb', line 45

def render
  @_tpl.result(binding)
end