Skip to content

Commit 8f15c31

Browse files
committed
Allow specifying a custom binding for template evaluation
1 parent 0bb59c1 commit 8f15c31

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/thor/actions/file_manipulation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def template(source, *args, &block)
110110
destination = args.first || source.sub(/#{TEMPLATE_EXTNAME}$/, "")
111111

112112
source = File.expand_path(find_in_source_paths(source.to_s))
113-
context = instance_eval("binding")
113+
context = config.delete(:context) || instance_eval("binding")
114114

115115
create_file destination, nil, config do
116116
content = ERB.new(::File.binread(source), nil, "-", "@output_buffer").result(context)

spec/actions/file_manipulation_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,16 @@ def file
205205
expect(File.read(File.join(destination_root, "doc/config.rb"))).to match(/^OMG/)
206206
end
207207

208+
it "accepts a context to use as the binding" do
209+
begin
210+
@klass = 'FooBar'
211+
action :template, "doc/config.rb", :context => eval("binding")
212+
expect(File.read(File.join(destination_root, "doc/config.rb"))).to eq("class FooBar; end\n")
213+
ensure
214+
remove_instance_variable(:@klass)
215+
end
216+
end
217+
208218
it "guesses the destination name when given only a source" do
209219
action :template, "doc/config.yaml.tt"
210220

0 commit comments

Comments
 (0)