Add Config::Top#deep_clone method to deep clone Vagrant configuration.

This commit is contained in:
Mitchell Hashimoto 2010-07-17 00:02:13 -07:00
parent 302bc348d3
commit aee49a61ab
2 changed files with 26 additions and 0 deletions

View File

@ -245,6 +245,12 @@ module Vagrant
def loaded!
@loaded = true
end
# Deep clones the entire configuration tree using the marshalling
# trick. All subclasses must be able to marshal properly.
def deep_clone
Marshal.load(Marshal.dump(self))
end
end
end
end

View File

@ -199,6 +199,26 @@ class ConfigTest < Test::Unit::TestCase
assert @top.loaded?
end
end
context "deep cloning" do
class DeepCloneConfig < Vagrant::Config::Base
attr_accessor :attribute
end
setup do
Vagrant::Config::Top.configures :deep, DeepCloneConfig
@top = Vagrant::Config::Top.new
@top.deep.attribute = [1,2,3]
end
should "deep clone the object" do
copy = @top.deep_clone
copy.deep.attribute << 4
assert_not_equal @top.deep.attribute, copy.deep.attribute
assert_equal 3, @top.deep.attribute.length
assert_equal 4, copy.deep.attribute.length
end
end
end
context "vagrant configuration" do