Added class level method to render string for the TemplateRenderer

This commit is contained in:
Mitchell Hashimoto 2010-03-17 19:09:17 -07:00
parent 37de04eb9b
commit 88cfaf8f27
4 changed files with 72 additions and 34 deletions

View File

@ -23,7 +23,7 @@ error
# Copy over the rootfile template into this directory
default_box ||= "base"
File.open(rootfile_path, 'w+') do |f|
f.write(TemplateRenderer.render!(Env::ROOTFILE_NAME, :default_box => default_box))
f.write(TemplateRenderer.render(Env::ROOTFILE_NAME, :default_box => default_box))
end
end

View File

@ -11,12 +11,27 @@ module Vagrant
# takes a block which will be passed the renderer prior to rendering, which
# allows the caller to set any view variables within the renderer itself.
#
# @param [String] template Name of the template file, without the extension
# @return [String] Rendered template
def render!(template, data={})
def render(*args)
render_with(:render, *args)
end
# Render a given string and return the result. This method optionally
# takes a block which will be passed the renderer prior to rendering, which
# allows the caller to set any view variables within the renderer itself.
#
# @param [String] template The template data string.
# @return [String] Rendered template
def render_string(*args)
render_with(:render_string, *args)
end
# Method used internally to DRY out the other renderers. This method
# creates and sets up the renderer before calling a specified method on it.
def render_with(method, template, data={})
renderer = new(template, data)
yield renderer if block_given?
renderer.render
renderer.send(method.to_sym)
end
end

View File

@ -17,7 +17,7 @@ class CommandsTest < Test::Unit::TestCase
File.stubs(:open).yields(@file)
@rootfile_path = File.join(Dir.pwd, Vagrant::Env::ROOTFILE_NAME)
Vagrant::Util::TemplateRenderer.stubs(:render!)
Vagrant::Util::TemplateRenderer.stubs(:render)
end
should "error and exit if a rootfile already exists" do
@ -28,7 +28,7 @@ class CommandsTest < Test::Unit::TestCase
should "write to the rootfile path using the template renderer" do
result = "foo"
Vagrant::Util::TemplateRenderer.expects(:render!).returns(result).once
Vagrant::Util::TemplateRenderer.expects(:render).returns(result).once
@file.expects(:write).with(result).once
File.expects(:open).with(@rootfile_path, 'w+').yields(@file)
@ -37,12 +37,12 @@ class CommandsTest < Test::Unit::TestCase
should "use the given base box if given" do
box = "zooo"
Vagrant::Util::TemplateRenderer.expects(:render!).with(Vagrant::Env::ROOTFILE_NAME, :default_box => box)
Vagrant::Util::TemplateRenderer.expects(:render).with(Vagrant::Env::ROOTFILE_NAME, :default_box => box)
Vagrant::Commands.init(box)
end
should "use the default `base` if no box is given" do
Vagrant::Util::TemplateRenderer.expects(:render!).with(Vagrant::Env::ROOTFILE_NAME, :default_box => "base")
Vagrant::Util::TemplateRenderer.expects(:render).with(Vagrant::Env::ROOTFILE_NAME, :default_box => "base")
Vagrant::Commands.init
end
end

View File

@ -77,39 +77,62 @@ class TemplateRendererUtilTest < Test::Unit::TestCase
end
end
context "class-level render! method" do
setup do
@template = "foo"
@r = Vagrant::Util::TemplateRenderer.new(@template)
@r.stubs(:render)
context "class methods" do
context "render_with method" do
setup do
@template = "foo"
@r = Vagrant::Util::TemplateRenderer.new(@template)
@r.stubs(:render)
Vagrant::Util::TemplateRenderer.stubs(:new).with(@template, {}).returns(@r)
end
@method = :rawr
should "use the first argument as the template" do
template = "foo"
Vagrant::Util::TemplateRenderer.expects(:new).with(template, {}).returns(@r)
Vagrant::Util::TemplateRenderer.render!(template)
end
Vagrant::Util::TemplateRenderer.stubs(:new).with(@template, {}).returns(@r)
end
should "send in additional argument to the renderer" do
template = "foo"
data = {:hey => :foo}
Vagrant::Util::TemplateRenderer.expects(:new).with(template, data).returns(@r)
Vagrant::Util::TemplateRenderer.render!(template, data)
end
should "use the second argument as the template" do
Vagrant::Util::TemplateRenderer.expects(:new).with(@template, {}).returns(@r)
Vagrant::Util::TemplateRenderer.render_with(@method, @template)
end
should "yield a block if given with the renderer as the argument" do
@r.expects(:yielded=).with(true).once
Vagrant::Util::TemplateRenderer.render!(@template) do |r|
r.yielded = true
should "send in additional argument to the renderer" do
data = {:hey => :foo}
Vagrant::Util::TemplateRenderer.expects(:new).with(@template, data).returns(@r)
Vagrant::Util::TemplateRenderer.render_with(@method, @template, data)
end
should "yield a block if given with the renderer as the argument" do
@r.expects(:yielded=).with(true).once
Vagrant::Util::TemplateRenderer.render_with(@method, @template) do |r|
r.yielded = true
end
end
should "render the result using the given method" do
result = mock('result')
@r.expects(@method).returns(result)
assert_equal result, Vagrant::Util::TemplateRenderer.render_with(@method, @template)
end
should "convert the given method to a sym prior to calling" do
@r.expects(@method.to_sym).returns(nil)
Vagrant::Util::TemplateRenderer.render_with(@method.to_s, @template)
end
end
should "render the result" do
result = mock('result')
@r.expects(:render).returns(result)
assert_equal result, Vagrant::Util::TemplateRenderer.render!(@template)
context "render method" do
should "call render_with the render! method" do
args = ["foo", "bar", "baz"]
Vagrant::Util::TemplateRenderer.expects(:render_with).with(:render, *args)
Vagrant::Util::TemplateRenderer.render(*args)
end
end
context "render_string method" do
should "call render_with the render! method" do
args = ["foo", "bar", "baz"]
Vagrant::Util::TemplateRenderer.expects(:render_with).with(:render_string, *args)
Vagrant::Util::TemplateRenderer.render_string(*args)
end
end
end
end