Method: Vagrant::Config::V2::Loader.merge

Defined in:
lib/vagrant/config/v2/loader.rb

.merge(old, new) ⇒ V2::Root

Merges two configuration objects.

Parameters:

  • old (V2::Root)

    The older root config.

  • new (V2::Root)

    The newer root config.

Returns:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vagrant/config/v2/loader.rb', line 49

def self.merge(old, new)
  # Grab the internal states, we use these heavily throughout the process
  old_state = old.__internal_state
  new_state = new.__internal_state

  # The config map for the new object is the old one merged with the
  # new one.
  config_map = old_state["config_map"].merge(new_state["config_map"])

  # Merge the keys.
  old_keys = old_state["keys"]
  new_keys = new_state["keys"]
  keys     = {}
  old_keys.each do |key, old_value|
    if new_keys.has_key?(key)
      # We need to do a merge, which we expect to be available
      # on the config class itself.
      keys[key] = old_value.merge(new_keys[key])
    else
      # We just take the old value, but dup it so that we can modify.
      keys[key] = old_value.dup
    end
  end

  new_keys.each do |key, new_value|
    # Add in the keys that the new class has that we haven't merged.
    if !keys.has_key?(key)
      keys[key] = new_value.dup
    end
  end

  # Merge the missing keys
  new_missing_key_calls =
    old_state["missing_key_calls"] + new_state["missing_key_calls"]

  # Return the final root object
  V2::Root.new(config_map).tap do |result|
    result.__set_internal_state({
      "config_map"        => config_map,
      "keys"              => keys,
      "missing_key_calls" => new_missing_key_calls
    })
  end
end