Class: Vagrant::Config::V1::Root

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/config/v1/root.rb

Overview

This is the root configuration class. An instance of this is what is passed into version 1 Vagrant configuration blocks.

Instance Method Summary collapse

Constructor Details

#initialize(config_map, keys = nil) ⇒ Root

Initializes a root object that maps the given keys to specific configuration classes.

Parameters:

  • config_map (Hash)

    Map of key to config class.



13
14
15
16
17
# File 'lib/vagrant/config/v1/root.rb', line 13

def initialize(config_map, keys=nil)
  @keys              = keys || {}
  @config_map        = config_map
  @missing_key_calls = Set.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

We use method_missing as a way to get the configuration that is used for Vagrant and load the proper configuration classes for each.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vagrant/config/v1/root.rb', line 22

def method_missing(name, *args)
  return @keys[name] if @keys.has_key?(name)

  config_klass = @config_map[name.to_sym]
  if config_klass
    # Instantiate the class and return the instance
    @keys[name] = config_klass.new
    return @keys[name]
  else
    # Record access to a missing key as an error
    @missing_key_calls.add(name.to_s)
    return DummyConfig.new
  end
end

Instance Method Details

#__internal_stateObject

Returns the internal state of the root object. This is used by outside classes when merging, and shouldn’t be called directly. Note the strange method name is to attempt to avoid any name clashes with potential configuration keys.



50
51
52
53
54
55
56
# File 'lib/vagrant/config/v1/root.rb', line 50

def __internal_state
  {
    "config_map"        => @config_map,
    "keys"              => @keys,
    "missing_key_calls" => @missing_key_calls
  }
end

#finalize!Object

Called to finalize this object just prior to it being used by the Vagrant system. The “!” signifies that this is expected to mutate itself.



40
41
42
43
44
# File 'lib/vagrant/config/v1/root.rb', line 40

def finalize!
  @keys.each do |_key, instance|
    instance.finalize!
  end
end