TemplateRenderer can now render strings
This commit is contained in:
parent
c6e39ddbf0
commit
37de04eb9b
|
@ -35,13 +35,25 @@ module Vagrant
|
||||||
#
|
#
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def render
|
def render
|
||||||
|
# TODO: Seems like a pretty dirty way to do this. Perhaps refactor this
|
||||||
|
old_template = template
|
||||||
result = nil
|
result = nil
|
||||||
File.open(full_template_path, 'r') do |f|
|
File.open(full_template_path, 'r') do |f|
|
||||||
erb = ERB.new(f.read)
|
self.template = f.read
|
||||||
result = erb.result(binding)
|
result = render_string
|
||||||
end
|
end
|
||||||
|
|
||||||
result
|
result
|
||||||
|
ensure
|
||||||
|
self.template = old_template
|
||||||
|
end
|
||||||
|
|
||||||
|
# Renders a template, handling the template as a string, but otherwise
|
||||||
|
# acting the same way as {#render}.
|
||||||
|
#
|
||||||
|
# @return [String]
|
||||||
|
def render_string
|
||||||
|
ERB.new(template).result(binding)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the full path to the template, taking into accoun the gem directory
|
# Returns the full path to the template, taking into accoun the gem directory
|
||||||
|
|
|
@ -24,9 +24,6 @@ class TemplateRendererUtilTest < Test::Unit::TestCase
|
||||||
@file = mock("file")
|
@file = mock("file")
|
||||||
@file.stubs(:read).returns(@contents)
|
@file.stubs(:read).returns(@contents)
|
||||||
File.stubs(:open).yields(@file)
|
File.stubs(:open).yields(@file)
|
||||||
|
|
||||||
@erb = mock("erb")
|
|
||||||
@erb.stubs(:result)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
should "open the template file for reading" do
|
should "open the template file for reading" do
|
||||||
|
@ -34,15 +31,13 @@ class TemplateRendererUtilTest < Test::Unit::TestCase
|
||||||
@r.render
|
@r.render
|
||||||
end
|
end
|
||||||
|
|
||||||
should "create an ERB object with the file contents" do
|
should "set the template to the file contents, render, then set it back" do
|
||||||
ERB.expects(:new).with(@file.read).returns(@erb)
|
result = "bar"
|
||||||
@r.render
|
|
||||||
end
|
|
||||||
|
|
||||||
should "render the ERB file and return the results" do
|
template_seq = sequence("template_seq")
|
||||||
result = mock("result")
|
@r.expects(:template=).with(@file.read).in_sequence(template_seq)
|
||||||
ERB.expects(:new).returns(@erb)
|
@r.expects(:render_string).returns(result).in_sequence(template_seq)
|
||||||
@erb.expects(:result).with(anything).once.returns(result)
|
@r.expects(:template=).with(@template).in_sequence(template_seq)
|
||||||
assert_equal result, @r.render
|
assert_equal result, @r.render
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -55,6 +50,21 @@ class TemplateRendererUtilTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "rendering as string" do
|
||||||
|
setup do
|
||||||
|
@result = "foo"
|
||||||
|
@erb = mock("erb")
|
||||||
|
@erb.stubs(:result).returns(@result)
|
||||||
|
|
||||||
|
@r = Vagrant::Util::TemplateRenderer.new("foo")
|
||||||
|
end
|
||||||
|
|
||||||
|
should "simply render the template as a string" do
|
||||||
|
ERB.expects(:new).with(@r.template).returns(@erb)
|
||||||
|
assert_equal @result, @r.render_string
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "the full template path" do
|
context "the full template path" do
|
||||||
setup do
|
setup do
|
||||||
@template = "foo"
|
@template = "foo"
|
||||||
|
|
Loading…
Reference in New Issue