Sha256: f69689135fe8d7f6f581e2d45e20cf3f06dc4d6a3f55200e3a0626c0f4a36d0e

Contents?: true

Size: 1.05 KB

Versions: 10

Compression:

Stored size: 1.05 KB

Contents

# encoding: utf-8

module Nake
  module TaskHelpers
    def template(source, target, context = Hash.new)
      template = Template.new(source)
      File.open(target, "w") do |file|
        file.puts(template.render(context))
      end
    end

    def erb(source, target, context = Hash.new)
      template = ErbTemplate.new(source)
      File.open(target, "w") do |file|
        file.puts(template.render(context))
      end
    end
  end

  class Template
    def initialize(path)
      @path = path
    end

    def render(context = Hash.new)
      File.read(@path) % context
    end
  end

  class ErbTemplate
    def initialize(path)
      require "erb"
      @path = path
    end

    def set_locals(context)
      context.inject("") do |source, pair|
        source += "#{pair.first} = context[:#{pair.first}]\n"
      end
    end

    def source(context)
      ["<% #{self.set_locals(context)} %>", File.read(@path)].join("")
    end

    def render(context = Hash.new)
      template = ERB.new(self.source(context))
      template.result(binding)
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
nake-0.1.1 lib/nake/template.rb
nake-0.1 lib/nake/template.rb
nake-0.0.9.5 lib/nake/template.rb
nake-0.0.9.4 lib/nake/template.rb
nake-0.0.9.3 lib/nake/template.rb
nake-0.0.9.2 lib/nake/template.rb
nake-0.0.9.pre lib/nake/template.rb
do_riak-0.10.1.pre gems/gems/nake-0.0.8/lib/nake/template.rb
nake-0.0.8.pre lib/nake/template.rb
nake-0.0.8 lib/nake/template.rb