Util::Errors class to parse errors from YML and render them via ERB
This commit is contained in:
parent
88cfaf8f27
commit
ac6c3a4892
|
@ -0,0 +1,33 @@
|
|||
require 'yaml'
|
||||
|
||||
module Vagrant
|
||||
module Util
|
||||
# This class is responsible for outputting errors. It retrieves the errors,
|
||||
# based on their key, from the error file, and then outputs it.
|
||||
class Errors
|
||||
@@errors = nil
|
||||
|
||||
class <<self
|
||||
def reset!
|
||||
@@errors = nil
|
||||
end
|
||||
|
||||
# Returns the hash of errors from the error YML files. This only loads once,
|
||||
# then returns a cached value until {reset!} is called.
|
||||
#
|
||||
# @return [Hash]
|
||||
def errors
|
||||
@@errors ||= YAML.load_file(File.join(PROJECT_ROOT, "templates", "errors.yml"))
|
||||
end
|
||||
|
||||
# Renders the error with the given key and data parameters and returns
|
||||
# the rendered result.
|
||||
#
|
||||
# @return [String]
|
||||
def error_string(key, data = {})
|
||||
TemplateRenderer.render_string(errors[key], data)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1 @@
|
|||
:foo: bar
|
|
@ -0,0 +1,52 @@
|
|||
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
|
||||
|
||||
class ErrorsUtilTest < Test::Unit::TestCase
|
||||
include Vagrant::Util
|
||||
|
||||
context "loading the errors from the YML" do
|
||||
setup do
|
||||
YAML.stubs(:load_file)
|
||||
Errors.reset!
|
||||
end
|
||||
|
||||
should "load the file initially, then never again unless reset" do
|
||||
YAML.expects(:load_file).with(File.join(PROJECT_ROOT, "templates", "errors.yml")).once
|
||||
Errors.errors
|
||||
Errors.errors
|
||||
Errors.errors
|
||||
Errors.errors
|
||||
end
|
||||
|
||||
should "reload if reset! is called" do
|
||||
YAML.expects(:load_file).with(File.join(PROJECT_ROOT, "templates", "errors.yml")).twice
|
||||
Errors.errors
|
||||
Errors.reset!
|
||||
Errors.errors
|
||||
end
|
||||
end
|
||||
|
||||
context "getting the error string" do
|
||||
setup do
|
||||
@errors = {}
|
||||
@errors[:foo] = "foo bar baz"
|
||||
Errors.stubs(:errors).returns(@errors)
|
||||
end
|
||||
|
||||
should "render the error string" do
|
||||
TemplateRenderer.expects(:render_string).with(@errors[:foo], anything).once
|
||||
Errors.error_string(:foo)
|
||||
end
|
||||
|
||||
should "pass in any data entries" do
|
||||
data = mock("data")
|
||||
TemplateRenderer.expects(:render_string).with(@errors[:foo], data).once
|
||||
Errors.error_string(:foo, data)
|
||||
end
|
||||
|
||||
should "return the result of the render" do
|
||||
result = mock("result")
|
||||
TemplateRenderer.expects(:render_string).returns(result)
|
||||
assert_equal result, Errors.error_string(:foo)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue