From aee49a61abc1ac4f54b14cbe292ff72a01efe040 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 17 Jul 2010 00:02:13 -0700 Subject: [PATCH] Add Config::Top#deep_clone method to deep clone Vagrant configuration. --- lib/vagrant/config.rb | 6 ++++++ test/vagrant/config_test.rb | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/vagrant/config.rb b/lib/vagrant/config.rb index 34d26c0bb..05385362c 100644 --- a/lib/vagrant/config.rb +++ b/lib/vagrant/config.rb @@ -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 diff --git a/test/vagrant/config_test.rb b/test/vagrant/config_test.rb index a834bb566..87dd88475 100644 --- a/test/vagrant/config_test.rb +++ b/test/vagrant/config_test.rb @@ -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