Added class level method to render string for the TemplateRenderer
This commit is contained in:
parent
37de04eb9b
commit
88cfaf8f27
|
@ -23,7 +23,7 @@ error
|
||||||
# Copy over the rootfile template into this directory
|
# Copy over the rootfile template into this directory
|
||||||
default_box ||= "base"
|
default_box ||= "base"
|
||||||
File.open(rootfile_path, 'w+') do |f|
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -11,12 +11,27 @@ module Vagrant
|
||||||
# takes a block which will be passed the renderer prior to rendering, which
|
# 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.
|
# 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
|
# @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)
|
renderer = new(template, data)
|
||||||
yield renderer if block_given?
|
yield renderer if block_given?
|
||||||
renderer.render
|
renderer.send(method.to_sym)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ class CommandsTest < Test::Unit::TestCase
|
||||||
File.stubs(:open).yields(@file)
|
File.stubs(:open).yields(@file)
|
||||||
@rootfile_path = File.join(Dir.pwd, Vagrant::Env::ROOTFILE_NAME)
|
@rootfile_path = File.join(Dir.pwd, Vagrant::Env::ROOTFILE_NAME)
|
||||||
|
|
||||||
Vagrant::Util::TemplateRenderer.stubs(:render!)
|
Vagrant::Util::TemplateRenderer.stubs(:render)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "error and exit if a rootfile already exists" do
|
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
|
should "write to the rootfile path using the template renderer" do
|
||||||
result = "foo"
|
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(:write).with(result).once
|
||||||
File.expects(:open).with(@rootfile_path, 'w+').yields(@file)
|
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
|
should "use the given base box if given" do
|
||||||
box = "zooo"
|
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)
|
Vagrant::Commands.init(box)
|
||||||
end
|
end
|
||||||
|
|
||||||
should "use the default `base` if no box is given" do
|
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
|
Vagrant::Commands.init
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -77,39 +77,62 @@ class TemplateRendererUtilTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "class-level render! method" do
|
context "class methods" do
|
||||||
setup do
|
context "render_with method" do
|
||||||
@template = "foo"
|
setup do
|
||||||
@r = Vagrant::Util::TemplateRenderer.new(@template)
|
@template = "foo"
|
||||||
@r.stubs(:render)
|
@r = Vagrant::Util::TemplateRenderer.new(@template)
|
||||||
|
@r.stubs(:render)
|
||||||
|
|
||||||
Vagrant::Util::TemplateRenderer.stubs(:new).with(@template, {}).returns(@r)
|
@method = :rawr
|
||||||
end
|
|
||||||
|
|
||||||
should "use the first argument as the template" do
|
Vagrant::Util::TemplateRenderer.stubs(:new).with(@template, {}).returns(@r)
|
||||||
template = "foo"
|
end
|
||||||
Vagrant::Util::TemplateRenderer.expects(:new).with(template, {}).returns(@r)
|
|
||||||
Vagrant::Util::TemplateRenderer.render!(template)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "send in additional argument to the renderer" do
|
should "use the second argument as the template" do
|
||||||
template = "foo"
|
Vagrant::Util::TemplateRenderer.expects(:new).with(@template, {}).returns(@r)
|
||||||
data = {:hey => :foo}
|
Vagrant::Util::TemplateRenderer.render_with(@method, @template)
|
||||||
Vagrant::Util::TemplateRenderer.expects(:new).with(template, data).returns(@r)
|
end
|
||||||
Vagrant::Util::TemplateRenderer.render!(template, data)
|
|
||||||
end
|
|
||||||
|
|
||||||
should "yield a block if given with the renderer as the argument" do
|
should "send in additional argument to the renderer" do
|
||||||
@r.expects(:yielded=).with(true).once
|
data = {:hey => :foo}
|
||||||
Vagrant::Util::TemplateRenderer.render!(@template) do |r|
|
Vagrant::Util::TemplateRenderer.expects(:new).with(@template, data).returns(@r)
|
||||||
r.yielded = true
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
should "render the result" do
|
context "render method" do
|
||||||
result = mock('result')
|
should "call render_with the render! method" do
|
||||||
@r.expects(:render).returns(result)
|
args = ["foo", "bar", "baz"]
|
||||||
assert_equal result, Vagrant::Util::TemplateRenderer.render!(@template)
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue