Class: Vagrant::Config::Loader
- Inherits:
-
Object
- Object
- Vagrant::Config::Loader
- Defined in:
- lib/vagrant/config/loader.rb
Overview
This class is responsible for loading Vagrant configuration, usually in the form of Vagrantfiles.
Loading works by specifying the sources for the configuration as well as the order the sources should be loaded. Configuration set later always overrides those set earlier; this is how configuration "scoping" is implemented.
Instance Attribute Summary collapse
-
#load_order ⇒ Object
This is an array of symbols specifying the order in which configuration is loaded.
Instance Method Summary collapse
-
#initialize ⇒ Loader
constructor
A new instance of Loader.
-
#load ⇒ Object
This loads the configured sources in the configured order and returns an actual configuration object that is ready to be used.
-
#set(name, sources) ⇒ Object
Set the configuration data for the given name.
Constructor Details
#initialize ⇒ Loader
Returns a new instance of Loader.
19 20 21 22 23 24 |
# File 'lib/vagrant/config/loader.rb', line 19 def initialize @logger = Log4r::Logger.new("vagrant::config::loader") @sources = {} @proc_cache = {} @config_cache = {} end |
Instance Attribute Details
#load_order ⇒ Object
This is an array of symbols specifying the order in which configuration is loaded. For examples, see the class documentation.
17 18 19 |
# File 'lib/vagrant/config/loader.rb', line 17 def load_order @load_order end |
Instance Method Details
#load ⇒ Object
This loads the configured sources in the configured order and returns an actual configuration object that is ready to be used.
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 93 94 95 96 |
# File 'lib/vagrant/config/loader.rb', line 64 def load @logger.debug("Loading configuration in order: #{@load_order.inspect}") unknown_sources = @sources.keys - @load_order if !unknown_sources.empty? # TODO: Raise exception here perhaps. @logger.error("Unknown config sources: #{unknown_sources.inspect}") end # Create the top-level configuration which will hold all the config. result = Top.new @load_order.each do |key| next if !@sources.has_key?(key) @sources[key].each do |proc| if !@config_cache.has_key?(proc) @logger.debug("Loading from: #{key} (evaluating)") current = Top.new proc.call(current) @config_cache[proc] = current else @logger.debug("Loading from: #{key} (cache)") end # Merge in the results of this proc's configuration result = result.merge(@config_cache[proc]) end end @logger.debug("Configuration loaded successfully") result end |
#set(name, sources) ⇒ Object
Set the configuration data for the given name.
The name
should be a symbol and must uniquely identify the data
being given.
data
can either be a path to a Ruby Vagrantfile or a Proc
directly.
data
can also be an array of such values.
At this point, no configuration is actually loaded. Note that calling
set
multiple times with the same name will override any previously
set values. In this way, the last set data for a given name wins.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/vagrant/config/loader.rb', line 37 def set(name, sources) @logger.debug("Set #{name.inspect} = #{sources.inspect}") # Sources should be an array sources = [sources] if !sources.kind_of?(Array) # Gather the procs for every source, since that is what we care about. procs = [] sources.each do |source| if !@proc_cache.has_key?(source) # Load the procs for this source and cache them. This caching # avoids the issue where a file may have side effects when loading # and loading it multiple times causes unexpected behavior. @logger.debug("Populating proc cache for #{source.inspect}") @proc_cache[source] = procs_for_source(source) end # Add on to the array of procs we're going to use procs.concat(@proc_cache[source]) end # Set this source by name. @sources[name] = procs end |