Sha256: bfc4c9f545b35f3acb3330790575afc1c762830b5552c666ca4502109af8a48d
Contents?: true
Size: 1.02 KB
Versions: 16
Compression:
Stored size: 1.02 KB
Contents
require 'erb' require 'fileutils' module HaveAPI::GoClient class ErbTemplate def self.render(name, vars) t = new(name, vars) t.render end def self.render_to(name, vars, path) File.write("#{path}.new", render(name, vars)) File.rename("#{path}.new", path) end 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 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 def render @_tpl.result(binding) end end end
Version data entries
16 entries across 16 versions & 1 rubygems