Skip to content

Commit

Permalink
Configuration versions can finalize config after loading
Browse files Browse the repository at this point in the history
This is useful so that it can take a look at the final loaded
configuration object and possibly make some tweaks to the configuration
object. The use case this was built for was so that config V1 can verify
that there is always at least a single VM defined as a sub-VM, the
"default" VM.
  • Loading branch information
mitchellh committed Jun 23, 2012
1 parent 7e19d68 commit 70fb804
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/vagrant/config/loader.rb
Expand Up @@ -103,8 +103,8 @@ def load
end
end

@logger.debug("Configuration loaded successfully")
result
@logger.debug("Configuration loaded successfully, finalizing and returning")
current_config_klass.finalize(result)
end

protected
Expand Down
13 changes: 13 additions & 0 deletions lib/vagrant/config/version_base.rb
Expand Up @@ -16,6 +16,19 @@ def self.init
raise NotImplementedError
end

# This is called just before configuration loading is complete of
# a potentially completely-merged value to perform final touch-ups
# to the configuration, if required.
#
# This is an optional method to implement. The default implementation
# will simply return the same object.
#
# @param [Object] obj Final configuration object.
# @param [Object] Finalized configuration object.
def self.finalize(obj)
obj
end

# Loads the configuration for the given proc and returns a configuration
# object. The return value is treated as an opaque object, so it can be
# anything you'd like. The return value is the object that is passed
Expand Down
22 changes: 21 additions & 1 deletion test/unit/vagrant/config/loader_test.rb
Expand Up @@ -11,7 +11,7 @@
# This is just a dummy implementation of a configuraiton loader which
# simply acts on hashes.
let(:test_loader) do
Class.new do
Class.new(Vagrant::Config::VersionBase) do
def self.init
{}
end
Expand Down Expand Up @@ -55,6 +55,26 @@ def self.merge(old, new)
config[:foo].should == "yep"
end

it "should finalize the configuration" do
# Create the finalize method on our loader
def test_loader.finalize(obj)
obj[:finalized] = true
obj
end

# Basic configuration proc
proc = lambda do |config|
config[:foo] = "yep"
end

# Run the actual configuration and assert that we get the proper result
instance.load_order = [:proc]
instance.set(:proc, [[current_version, proc]])
config = instance.load
config[:foo].should == "yep"
config[:finalized].should == true
end

it "should only run the same proc once" do
count = 0
proc = Proc.new do |config|
Expand Down

0 comments on commit 70fb804

Please sign in to comment.