Class: Vagrant::Config::Top

Inherits:
Base
  • Object
show all
Defined in:
lib/vagrant/config/top.rb

Overview

This class is the "top" configure class, which handles registering other configuration classes as well as validation of all configured classes. This is the object which is returned by Environment#config and has accessors to all other configuration classes.

If you're looking to create your own configuration class, see Base.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#instance_variables_hash, json_create, #set_options, #to_hash, #to_json, #validate

Constructor Details

#initialize(registry = nil) ⇒ Top

Returns a new instance of Top.



12
13
14
15
# File 'lib/vagrant/config/top.rb', line 12

def initialize(registry=nil)
  @keys = {}
  @registry = registry || Vagrant.config_keys
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.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vagrant/config/top.rb', line 19

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

  config_klass = @registry.get(name.to_sym)
  if config_klass
    # Instantiate the class and return the instance
    @keys[name] = config_klass.new
    return @keys[name]
  else
    # Super it up to probably raise a NoMethodError
    super
  end
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



10
11
12
# File 'lib/vagrant/config/top.rb', line 10

def keys
  @keys
end

Instance Method Details

#merge(other) ⇒ Object

Custom implementation to merge each key separately.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vagrant/config/top.rb', line 34

def merge(other)
  result = self.class.new
  @keys.each do |key, value|
    result.keys[key] = value.merge(other.send(key))
  end

  other.keys.each do |key, value|
    if !@keys.has_key?(key)
      # This is a key that the other configuration class has
      # that we don't, so just copy it in.
      result.keys[key] = value.dup
    end
  end

  result
end

#validate!(env) ⇒ Object

Validates the configuration classes of this instance and raises an exception if they are invalid. If you are implementing a custom configuration class, the method you want to implement is Base#validate. This is the method that checks all the validation, not one which defines validation rules.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/vagrant/config/top.rb', line 56

def validate!(env)
  # Validate each of the configured classes and store the results into
  # a hash.
  errors = @keys.inject({}) do |container, data|
    key, instance = data
    recorder = ErrorRecorder.new
    instance.validate(env, recorder)
    container[key.to_sym] = recorder if !recorder.errors.empty?
    container
  end

  return if errors.empty?
  raise Errors::ConfigValidationFailed, :messages => Util::TemplateRenderer.render("config/validation_failed", :errors => errors)
end