Implement `template' task keyword
This commit is contained in:
parent
ff0287b545
commit
6cd294a0b8
27
features/task_template.feature
Normal file
27
features/task_template.feature
Normal file
@ -0,0 +1,27 @@
|
||||
Feature: `template' task keyword
|
||||
|
||||
Background:
|
||||
Given a file named "basic.erb" with:
|
||||
"""
|
||||
basic template
|
||||
"""
|
||||
Given a file named "variables.erb" with:
|
||||
"""
|
||||
<%= @foo %>
|
||||
"""
|
||||
|
||||
Scenario: renders an ERB template file
|
||||
Given a recipe with:
|
||||
"""
|
||||
task(:echo_template) { echo template 'basic' }
|
||||
"""
|
||||
When I execute the recipe
|
||||
Then the output must contain "basic template"
|
||||
|
||||
Scenario: renders ERB with given attributes as member data
|
||||
Given a recipe with:
|
||||
"""
|
||||
task(:echo_template) { echo template('variables', foo: 'bar') }
|
||||
"""
|
||||
When I execute the recipe
|
||||
Then the output must contain "bar"
|
@ -1,3 +1,4 @@
|
||||
require 'erb'
|
||||
require 'etc'
|
||||
require 'forwardable'
|
||||
require 'optparse'
|
||||
|
@ -57,6 +57,25 @@ module Producer
|
||||
def get(key)
|
||||
@env[key]
|
||||
end
|
||||
|
||||
def template(path, variables = {})
|
||||
path = "#{path}.erb"
|
||||
tpl = ERB.new(File.read(path), nil, '-')
|
||||
tpl.filename = path
|
||||
tpl.result build_erb_binding variables
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def build_erb_binding(variables)
|
||||
Object.new.instance_eval do |o|
|
||||
variables.each do |k, v|
|
||||
o.instance_variable_set "@#{k}", v
|
||||
end
|
||||
binding
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user