Sha256: 8e3daab5dbeeaa92e2bfb71e22d03e519046464c5546a1b6dc132ac592b0970d

Contents?: true

Size: 738 Bytes

Versions: 62

Compression:

Stored size: 738 Bytes

Contents

require 'hamster'
require 'erb'

module RakeDependencies
  class Template
    def initialize(
        template,
        parameters = {})
      @template = template
      @parameters = Hamster::Hash.new(parameters)
    end

    def with_parameter key, value
      Template.new(@template, @parameters.put(key, value))
    end

    def with_parameters **pairs
      pairs.to_a.reduce(self) do |memo, parameter|
        memo.with_parameter(*parameter)
      end
    end

    def render
      context = Object.new
      @parameters.each do |key, value|
        context.instance_variable_set("@#{key}", value)
      end
      context_binding = context.instance_eval { binding }
      ERB.new(@template).result(context_binding)
    end
  end
end

Version data entries

62 entries across 62 versions & 2 rubygems

Version Path
rake_dependencies-1.12.0 lib/rake_dependencies/template.rb
rake_dependencies-1.11.0.pre.1 lib/rake_dependencies/template.rb
rake_dependencies-1.10.0 lib/rake_dependencies/template.rb
rake_dependencies-1.9.0.pre.1 lib/rake_dependencies/template.rb
rake_dependencies-1.8.0 lib/rake_dependencies/template.rb
rake_dependencies-1.7.0.pre.1 lib/rake_dependencies/template.rb
rake_dependencies-1.6.0 lib/rake_dependencies/template.rb
rake_dependencies-1.5.0.pre.2 lib/rake_dependencies/template.rb
rake_dependencies-1.5.0.pre.1 lib/rake_dependencies/template.rb
rake_dependencies-1.4.0 lib/rake_dependencies/template.rb
rake_dependencies-1.3.0.pre.1 lib/rake_dependencies/template.rb
rake_dependencies-1.2.0 lib/rake_dependencies/template.rb
rake_dependencies-1.1.0.pre.2 lib/rake_dependencies/template.rb
rake_dependencies-1.1.0.pre.1 lib/rake_dependencies/template.rb
rake_dependencies-0.23.0 lib/rake_dependencies/template.rb
rake_dependencies-0.22.0.pre.pre.2 lib/rake_dependencies/template.rb
rake_dependencies-0.22.0.pre.pre.1 lib/rake_dependencies/template.rb
rake_dependencies-0.21.0 lib/rake_dependencies/template.rb
rake_dependencies-0.20.0.pre.pre.2 lib/rake_dependencies/template.rb
rake_dependencies-0.20.0.pre.pre.1 lib/rake_dependencies/template.rb