Sha256: 0781f229b520646ea88ca01de3c057cc710e50f810a3bc2c55e2963867ea3f2a

Contents?: true

Size: 771 Bytes

Versions: 17

Compression:

Stored size: 771 Bytes

Contents

# frozen_string_literal: true

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

17 entries across 17 versions & 1 rubygems

Version Path
rake_dependencies-3.10.0.pre.2 lib/rake_dependencies/template.rb
rake_dependencies-3.10.0.pre.1 lib/rake_dependencies/template.rb
rake_dependencies-3.9.0 lib/rake_dependencies/template.rb
rake_dependencies-3.8.0.pre.1 lib/rake_dependencies/template.rb
rake_dependencies-3.7.0 lib/rake_dependencies/template.rb
rake_dependencies-3.6.0.pre.8 lib/rake_dependencies/template.rb
rake_dependencies-3.6.0.pre.4 lib/rake_dependencies/template.rb
rake_dependencies-3.6.0.pre.3 lib/rake_dependencies/template.rb
rake_dependencies-3.6.0.pre.2 lib/rake_dependencies/template.rb
rake_dependencies-3.6.0.pre.1 lib/rake_dependencies/template.rb
rake_dependencies-3.5.0 lib/rake_dependencies/template.rb
rake_dependencies-3.4.0.pre.1 lib/rake_dependencies/template.rb
rake_dependencies-3.3.0 lib/rake_dependencies/template.rb
rake_dependencies-3.2.0.pre.7 lib/rake_dependencies/template.rb
rake_dependencies-3.2.0.pre.6 lib/rake_dependencies/template.rb
rake_dependencies-3.2.0.pre.5 lib/rake_dependencies/template.rb
rake_dependencies-3.2.0.pre.4 lib/rake_dependencies/template.rb