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.

Constant Summary collapse

@@configures =
{}

Instance Attribute Summary collapse

Attributes inherited from Base

#top

Class Method 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(env = nil) ⇒ Top

Returns a new instance of Top.



30
31
32
33
34
35
36
37
38
# File 'lib/vagrant/config/top.rb', line 30

def initialize(env=nil)
  self.class.configures_list.each do |key, klass|
    config = klass.new
    config.top = self
    instance_variable_set("@#{key}".to_sym, config)
  end

  @env = env
end

Instance Attribute Details

#envObject (readonly)

The environment that this configuration is for.



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

def env
  @env
end

Class Method Details

.configures(key, klass) ⇒ Object

Registers a configuration class with the given key. This method shouldn't be called. Instead, inherit from Base and call Base.configures.



24
25
26
27
# File 'lib/vagrant/config/top.rb', line 24

def configures(key, klass)
  configures_list[key] = klass
  attr_reader key.to_sym
end

.configures_listObject

The list of registered configuration classes as well as the key they're registered under.



18
19
20
# File 'lib/vagrant/config/top.rb', line 18

def configures_list
  @@configures ||= {}
end

Instance Method Details

#validate!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.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vagrant/config/top.rb', line 45

def validate!
  # Validate each of the configured classes and store the results into
  # a hash.
  errors = self.class.configures_list.inject({}) do |container, data|
    key, _ = data
    recorder = ErrorRecorder.new
    send(key.to_sym).validate(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