Sha256: 076ab7644b77bf771d37ff3488e69d008bb73d563a25b48aa879253b5066df31

Contents?: true

Size: 775 Bytes

Versions: 2

Compression:

Stored size: 775 Bytes

Contents

require 'erb'

module ConfCtl
  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 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

    def render
      @_tpl.result(binding)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
confctl-2.0.0 lib/confctl/erb_template.rb
confctl-1.0.0 lib/confctl/erb_template.rb