Moved template renderer until the Util namespace
This commit is contained in:
parent
4229b65a81
commit
b1872c4e31
|
@ -3,7 +3,7 @@ PROJECT_ROOT = File.join(libdir, '..') unless defined?(PROJECT_ROOT)
|
|||
|
||||
# The libs which must be loaded prior to the rest
|
||||
%w{tempfile open-uri json pathname logger uri net/http virtualbox net/ssh archive/tar/minitar
|
||||
net/scp fileutils ostruct erb}.each do |lib|
|
||||
net/scp fileutils}.each do |lib|
|
||||
require lib
|
||||
end
|
||||
|
||||
|
|
|
@ -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(Util::TemplateRenderer.render!(Env::ROOTFILE_NAME, :default_box => default_box))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
module Vagrant
|
||||
# This class is used to render the ERB templates in the
|
||||
# `GEM_ROOT/templates` directory.
|
||||
class TemplateRenderer < OpenStruct
|
||||
class <<self
|
||||
# Render a given template 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 Name of the template file, without the extension
|
||||
# @return [String] Rendered template
|
||||
def render!(template, data={})
|
||||
renderer = new(template, data)
|
||||
yield renderer if block_given?
|
||||
renderer.render
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(template, data = {})
|
||||
super()
|
||||
|
||||
data[:template] = template
|
||||
data.each do |key, value|
|
||||
send("#{key}=", value)
|
||||
end
|
||||
end
|
||||
|
||||
# Renders the template using the class intance as the binding. Because the
|
||||
# renderer inherits from `OpenStruct`, additional view variables can be
|
||||
# added like normal accessors.
|
||||
#
|
||||
# @return [String]
|
||||
def render
|
||||
result = nil
|
||||
File.open(full_template_path, 'r') do |f|
|
||||
erb = ERB.new(f.read)
|
||||
result = erb.result(binding)
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
# Returns the full path to the template, taking into accoun the gem directory
|
||||
# and adding the `.erb` extension to the end.
|
||||
#
|
||||
# @return [String]
|
||||
def full_template_path
|
||||
File.join(PROJECT_ROOT, 'templates', "#{template}.erb")
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,56 @@
|
|||
require 'ostruct'
|
||||
require 'erb'
|
||||
|
||||
module Vagrant
|
||||
module Util
|
||||
# This class is used to render the ERB templates in the
|
||||
# `GEM_ROOT/templates` directory.
|
||||
class TemplateRenderer < OpenStruct
|
||||
class <<self
|
||||
# Render a given template 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 Name of the template file, without the extension
|
||||
# @return [String] Rendered template
|
||||
def render!(template, data={})
|
||||
renderer = new(template, data)
|
||||
yield renderer if block_given?
|
||||
renderer.render
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(template, data = {})
|
||||
super()
|
||||
|
||||
data[:template] = template
|
||||
data.each do |key, value|
|
||||
send("#{key}=", value)
|
||||
end
|
||||
end
|
||||
|
||||
# Renders the template using the class intance as the binding. Because the
|
||||
# renderer inherits from `OpenStruct`, additional view variables can be
|
||||
# added like normal accessors.
|
||||
#
|
||||
# @return [String]
|
||||
def render
|
||||
result = nil
|
||||
File.open(full_template_path, 'r') do |f|
|
||||
erb = ERB.new(f.read)
|
||||
result = erb.result(binding)
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
# Returns the full path to the template, taking into accoun the gem directory
|
||||
# and adding the `.erb` extension to the end.
|
||||
#
|
||||
# @return [String]
|
||||
def full_template_path
|
||||
File.join(PROJECT_ROOT, 'templates', "#{template}.erb")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -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::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::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::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::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
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
require File.join(File.dirname(__FILE__), '..', 'test_helper')
|
||||
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
|
||||
|
||||
class TemplateRendererTest < Test::Unit::TestCase
|
||||
class TemplateRendererUtilTest < Test::Unit::TestCase
|
||||
context "initializing" do
|
||||
should "set the template to the given argument" do
|
||||
r = Vagrant::TemplateRenderer.new("foo")
|
||||
r = Vagrant::Util::TemplateRenderer.new("foo")
|
||||
assert_equal "foo", r.template
|
||||
end
|
||||
|
||||
should "set any additional variables" do
|
||||
r = Vagrant::TemplateRenderer.new("foo", {:bar => :baz})
|
||||
r = Vagrant::Util::TemplateRenderer.new("foo", {:bar => :baz})
|
||||
assert_equal :baz, r.bar
|
||||
end
|
||||
end
|
||||
|
@ -16,7 +16,7 @@ class TemplateRendererTest < Test::Unit::TestCase
|
|||
context "rendering" do
|
||||
setup do
|
||||
@template = "foo"
|
||||
@r = Vagrant::TemplateRenderer.new(@template)
|
||||
@r = Vagrant::Util::TemplateRenderer.new(@template)
|
||||
@r.stubs(:full_template_path).returns(@template + "!")
|
||||
|
||||
@contents = "bar"
|
||||
|
@ -58,7 +58,7 @@ class TemplateRendererTest < Test::Unit::TestCase
|
|||
context "the full template path" do
|
||||
setup do
|
||||
@template = "foo"
|
||||
@r = Vagrant::TemplateRenderer.new(@template)
|
||||
@r = Vagrant::Util::TemplateRenderer.new(@template)
|
||||
end
|
||||
|
||||
should "be the ERB file in the templates directory" do
|
||||
|
@ -70,28 +70,28 @@ class TemplateRendererTest < Test::Unit::TestCase
|
|||
context "class-level render! method" do
|
||||
setup do
|
||||
@template = "foo"
|
||||
@r = Vagrant::TemplateRenderer.new(@template)
|
||||
@r = Vagrant::Util::TemplateRenderer.new(@template)
|
||||
@r.stubs(:render)
|
||||
|
||||
Vagrant::TemplateRenderer.stubs(:new).with(@template, {}).returns(@r)
|
||||
Vagrant::Util::TemplateRenderer.stubs(:new).with(@template, {}).returns(@r)
|
||||
end
|
||||
|
||||
should "use the first argument as the template" do
|
||||
template = "foo"
|
||||
Vagrant::TemplateRenderer.expects(:new).with(template, {}).returns(@r)
|
||||
Vagrant::TemplateRenderer.render!(template)
|
||||
Vagrant::Util::TemplateRenderer.expects(:new).with(template, {}).returns(@r)
|
||||
Vagrant::Util::TemplateRenderer.render!(template)
|
||||
end
|
||||
|
||||
should "send in additional argument to the renderer" do
|
||||
template = "foo"
|
||||
data = {:hey => :foo}
|
||||
Vagrant::TemplateRenderer.expects(:new).with(template, data).returns(@r)
|
||||
Vagrant::TemplateRenderer.render!(template, data)
|
||||
Vagrant::Util::TemplateRenderer.expects(:new).with(template, data).returns(@r)
|
||||
Vagrant::Util::TemplateRenderer.render!(template, data)
|
||||
end
|
||||
|
||||
should "yield a block if given with the renderer as the argument" do
|
||||
@r.expects(:yielded=).with(true).once
|
||||
Vagrant::TemplateRenderer.render!(@template) do |r|
|
||||
Vagrant::Util::TemplateRenderer.render!(@template) do |r|
|
||||
r.yielded = true
|
||||
end
|
||||
end
|
||||
|
@ -99,7 +99,7 @@ class TemplateRendererTest < Test::Unit::TestCase
|
|||
should "render the result" do
|
||||
result = mock('result')
|
||||
@r.expects(:render).returns(result)
|
||||
assert_equal result, Vagrant::TemplateRenderer.render!(@template)
|
||||
assert_equal result, Vagrant::Util::TemplateRenderer.render!(@template)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue