Class: RakeDependencies::Template
- Inherits:
-
Object
- Object
- RakeDependencies::Template
- Defined in:
- lib/rake_dependencies/template.rb
Instance Method Summary collapse
-
#initialize(template, parameters = {}) ⇒ Template
constructor
A new instance of Template.
- #render ⇒ Object
- #with_parameter(key, value) ⇒ Object
- #with_parameters(pairs) ⇒ Object
Constructor Details
#initialize(template, parameters = {}) ⇒ Template
Returns a new instance of Template.
8 9 10 11 12 13 14 |
# File 'lib/rake_dependencies/template.rb', line 8 def initialize( template, parameters = {} ) @template = template @parameters = Hamster::Hash.new(parameters) end |
Instance Method Details
#render ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/rake_dependencies/template.rb', line 26 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 |
#with_parameter(key, value) ⇒ Object
16 17 18 |
# File 'lib/rake_dependencies/template.rb', line 16 def with_parameter(key, value) Template.new(@template, @parameters.put(key, value)) end |
#with_parameters(pairs) ⇒ Object
20 21 22 23 24 |
# File 'lib/rake_dependencies/template.rb', line 20 def with_parameters(pairs) pairs.to_a.reduce(self) do |memo, parameter| memo.with_parameter(*parameter) end end |