Config can now be deserialized as well as serialized to JSON

This commit is contained in:
Mitchell Hashimoto 2010-09-01 21:01:15 -07:00
parent d2d2404961
commit 43cdcb7808
5 changed files with 28 additions and 15 deletions

View File

@ -6,12 +6,24 @@ module Vagrant
class Base
attr_accessor :env
def [](key)
send(key)
# Loads configuration values from JSON back into the proper
# configuration classes. By default, this is done by simply
# iterating over all values in the JSON hash and assigning them
# to instance variables on the class.
def self.json_create(data)
data.inject(new) do |result, data|
key, value = data
result.instance_variable_set("@#{key}".to_sym, value) if key != "json_class"
result
end
end
def to_json(*a)
{ 'json_class' => self.class.name }.merge(instance_variables_hash).to_json(*a)
opts = a.first if a.first.is_a?(Hash)
opts ||= {}
result = {}
result.merge!('json_class' => self.class.name) if opts[:loadable]
result.merge(instance_variables_hash).to_json(*a)
end
def instance_variables_hash

View File

@ -58,7 +58,7 @@ module Vagrant
opts[:forward_agent] = true if env.config.ssh.forward_agent
Net::SSH.start(env.config.ssh.host,
env.config[:ssh][:username],
env.config.ssh.username,
opts.merge( :port => port,
:keys => [env.config.ssh.private_key_path],
:user_known_hosts_file => [],

View File

@ -5,11 +5,6 @@ class ConfigBaseTest < Test::Unit::TestCase
@base = Vagrant::Config::Base.new
end
should "forward [] access to methods" do
@base.expects(:foo).once
@base[:foo]
end
should "return a hash of instance variables" do
data = { "foo" => "bar", "bar" => "baz" }
@ -26,11 +21,17 @@ class ConfigBaseTest < Test::Unit::TestCase
end
context "converting to JSON" do
should "include magic `json_class` if loadable is set to true" do
@iv_hash = { "foo" => "bar" }
@base.expects(:instance_variables_hash).returns(@iv_hash)
@json = { 'json_class' => @base.class.name }.merge(@iv_hash).to_json
assert_equal @json, @base.to_json(:loadable => true)
end
should "convert instance variable hash to json" do
@iv_hash = { "foo" => "bar" }
@base.expects(:instance_variables_hash).returns(@iv_hash)
@json = { "json_class" => @base.class.name }.merge(@iv_hash).to_json
assert_equal @json, @base.to_json
assert_equal @iv_hash.to_json, @base.to_json
end
should "not include env in the JSON hash" do

View File

@ -7,7 +7,7 @@ class ConfigSSHTest < Test::Unit::TestCase
end
should "expand any path when requesting the value" do
result = File.expand_path(@env.config.ssh[:private_key_path], @env.root_path)
result = File.expand_path(@env.config.ssh.private_key_path, @env.root_path)
assert_equal result, @env.config.ssh.private_key_path
end
end

View File

@ -25,8 +25,8 @@ class ChefProvisionerTest < Test::Unit::TestCase
end
should "not include the 'json' key in the config dump" do
result = JSON.parse(@config.to_json)
assert !result.has_key?("json")
result = @config.to_json
assert result !~ /"json":/
end
should "provide accessors to the run list" do
@ -141,7 +141,7 @@ class ChefProvisionerTest < Test::Unit::TestCase
context "generating and uploading json" do
def assert_json
@vm.ssh.expects(:upload!).with do |json, path|
data = JSON.parse(json.read)
data = JSON.parse(json.read, :object_class => Hash)
yield data
true
end