Sha256: 77fd47d68af4fbeb493b678f85ff6e34e5f52f99dc9af0bf77dc867270895e67

Contents?: true

Size: 1.01 KB

Versions: 21

Compression:

Stored size: 1.01 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, 0, '-')

      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

21 entries across 21 versions & 1 rubygems

Version Path
haveapi-go-client-0.19.3 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.19.2 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.19.1 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.19.0 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.18.2 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.18.1 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.18.0 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.17.0 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.16.3 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.16.2 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.16.1 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.16.0 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.15.1 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.15.0 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.14.2 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.14.1 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.14.0 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.13.3 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.13.2 lib/haveapi/go_client/erb_template.rb
haveapi-go-client-0.13.1 lib/haveapi/go_client/erb_template.rb