Permalink
Browse files

Allow using block helpers in templates

  • Loading branch information...
1 parent fff07f9 commit 8b9279f0a9805adc06f172d5a00e3f427b1ee3d5 @drogus drogus committed with josevalim Sep 24, 2010
View
19 lib/thor/actions/file_manipulation.rb
@@ -86,7 +86,7 @@ def template(source, *args, &block)
context = instance_eval('binding')
create_file destination, nil, config do
- content = ERB.new(::File.binread(source), nil, '-').result(context)
+ content = ERB.new(::File.binread(source), nil, '-', '@output_buffer').result(context)
content = block.call(content) if block
content
end
@@ -225,5 +225,22 @@ def remove_file(path, config={})
end
alias :remove_dir :remove_file
+ private
+ attr_accessor :output_buffer
+ def concat(string)
+ @output_buffer.concat(string)
+ end
+
+ def capture(*args, &block)
+ with_output_buffer { block.call(*args) }
+ end
+
+ def with_output_buffer(buf = '') #:nodoc:
+ self.output_buffer, old_buffer = buf, output_buffer
+ yield
+ output_buffer
+ ensure
+ self.output_buffer = old_buffer
+ end
end
end
View
7 spec/actions/file_manipulation_spec.rb
@@ -117,6 +117,13 @@ def file
end
describe "#template" do
+ it "allows using block helpers in the template" do
+ action :template, "doc/block_helper.rb"
+
+ file = File.join(destination_root, "doc/block_helper.rb")
+ File.read(file).must == "Hello world!"
+ end
+
it "evaluates the template given as source" do
runner.instance_variable_set("@klass", "Config")
action :template, "doc/config.rb"
View
3 spec/fixtures/doc/block_helper.rb
@@ -0,0 +1,3 @@
+<% world do -%>
+Hello
+<% end -%>
View
9 spec/fixtures/group.thor
@@ -37,6 +37,13 @@ FOO
super
base.source_paths.unshift(File.expand_path(File.join(File.dirname(__FILE__), "doc")))
end
+
+ no_tasks do
+ def world(&block)
+ result = capture(&block)
+ concat(result.strip + " world!")
+ end
+ end
end
class ClearCounter < MyCounter
@@ -85,4 +92,4 @@ class TaskConflict < Thor::Group
def group
puts "group"
end
-end
+end

0 comments on commit 8b9279f

Please sign in to comment.