2
2
require 'vagrant/config/error_recorder'
3
3
4
4
module Vagrant
5
- # The config class is responsible for loading Vagrant configurations
6
- # (usually through Vagrantfiles).
5
+ # The config class is responsible for loading Vagrant configurations, which
6
+ # are usually found in Vagrantfiles but may also be procs. The loading is done
7
+ # by specifying a queue of files or procs that are for configuration, and then
8
+ # executing them. The config loader will run each item in the queue, so that
9
+ # configuration from later items overwrite that from earlier items. This is how
10
+ # Vagrant "scoping" of Vagranfiles is implemented.
11
+ #
12
+ # If you're looking to create your own configuration classes, see {Base}.
13
+ #
14
+ # # Loading Configuration Files
15
+ #
16
+ # If you are in fact looking to load configuration files, then this is the
17
+ # class you are looking for. Loading configuration is quite easy. The following
18
+ # example assumes `env` is already a loaded instance of {Environment}:
19
+ #
20
+ # config = Vagrant::Config.new(env)
21
+ # config.queue << "/path/to/some/Vagrantfile"
22
+ # result = config.load!
23
+ #
24
+ # p "Your box is: #{result.vm.box}"
25
+ #
7
26
class Config
8
27
extend Util ::StackedProcRunner
9
28
@@ -12,6 +31,12 @@ class Config
12
31
attr_reader :queue
13
32
14
33
class << self
34
+ # Resets the current loaded config object to the specified environment.
35
+ # This clears the proc stack and initializes a new {Top} for loading.
36
+ # This method shouldn't be called directly, instead use an instance of this
37
+ # class for config loading.
38
+ #
39
+ # @param [Environment] env
15
40
def reset! ( env = nil )
16
41
@@config = nil
17
42
proc_stack . clear
@@ -20,14 +45,29 @@ def reset!(env=nil)
20
45
config ( env )
21
46
end
22
47
48
+ # Returns the current {Top} configuration object. While this is still
49
+ # here for implementation purposes, it shouldn't be called directly. Instead,
50
+ # use an instance of this class.
23
51
def config ( env = nil )
24
52
@@config ||= Config ::Top . new ( env )
25
53
end
26
54
55
+ # Adds the given proc/block to the stack of config procs which are all
56
+ # run later on a single config object. This is the main way to configure
57
+ # Vagrant, and is how all Vagrantfiles are formatted:
58
+ #
59
+ # Vagrant::Config.run do |config|
60
+ # # ...
61
+ # end
62
+ #
27
63
def run ( &block )
28
64
push_proc ( &block )
29
65
end
30
66
67
+ # Executes all the config procs onto the currently loaded {Top} object,
68
+ # and returns the final configured object. This also validates the
69
+ # configuration by calling {Top#validate!} on every configuration
70
+ # class.
31
71
def execute! ( config_object = nil )
32
72
config_object ||= config
33
73
@@ -37,6 +77,10 @@ def execute!(config_object=nil)
37
77
end
38
78
end
39
79
80
+ # Initialize a {Config} object for the given {Environment}.
81
+ #
82
+ # @param [Environment] env Environment which config object will be part
83
+ # of.
40
84
def initialize ( env )
41
85
@env = env
42
86
@queue = [ ]
@@ -65,14 +109,24 @@ def load!
65
109
end
66
110
67
111
class Config
112
+ # This class is the "top" configure class, which handles registering
113
+ # other configuration classes as well as validation of all configured
114
+ # classes. This is the object which is returned by {Environment#config}
115
+ # and has accessors to all other configuration classes.
116
+ #
117
+ # If you're looking to create your own configuration class, see {Base}.
68
118
class Top < Base
69
119
@@configures = [ ]
70
120
71
121
class << self
122
+ # The list of registered configuration classes as well as the key
123
+ # they're registered under.
72
124
def configures_list
73
125
@@configures ||= [ ]
74
126
end
75
127
128
+ # Registers a configuration class with the given key. This method shouldn't
129
+ # be called. Instead, inherit from {Base} and call {Base.configures}.
76
130
def configures ( key , klass )
77
131
configures_list << [ key , klass ]
78
132
attr_reader key . to_sym
@@ -90,7 +144,10 @@ def initialize(env=nil)
90
144
end
91
145
92
146
# Validates the configuration classes of this instance and raises an
93
- # exception if they are invalid.
147
+ # exception if they are invalid. If you are implementing a custom configuration
148
+ # class, the method you want to implement is {Base#validate}. This is
149
+ # the method that checks all the validation, not one which defines
150
+ # validation rules.
94
151
def validate!
95
152
# Validate each of the configured classes and store the results into
96
153
# a hash.
0 commit comments