Plugins can define action hooks via action_hook

This commit is contained in:
Mitchell Hashimoto 2013-02-06 15:27:14 -08:00
parent 83bba789a4
commit aa7193471f
3 changed files with 11 additions and 10 deletions

View File

@ -6,12 +6,19 @@ module Vagrant
# components, and the actual container of those components. This # components, and the actual container of those components. This
# removes a bit of state overhead from the plugin class itself. # removes a bit of state overhead from the plugin class itself.
class Components class Components
# This contains all the action hooks.
#
# @return [Array<Proc>]
attr_reader :action_hooks
# This contains all the configuration plugins by scope. # This contains all the configuration plugins by scope.
# #
# @return [Hash<Symbol, Registry>] # @return [Hash<Symbol, Registry>]
attr_reader :configs attr_reader :configs
def initialize def initialize
@action_hooks = []
# Create the configs hash which defaults to a registry # Create the configs hash which defaults to a registry
@configs = Hash.new { |h, k| h[k] = Registry.new } @configs = Hash.new { |h, k| h[k] = Registry.new }
end end

View File

@ -65,18 +65,12 @@ module Vagrant
# is run. This allows plugin authors to hook into things like VM # is run. This allows plugin authors to hook into things like VM
# bootup, VM provisioning, etc. # bootup, VM provisioning, etc.
# #
# @param [Symbol] name Name of the action. # @param [String] name Name of the action.
# @return [Array] List of the hooks for the given action. # @return [Array] List of the hooks for the given action.
def self.action_hook(name, &block) def self.action_hook(name, &block)
# Get the list of hooks for the given hook name # The name is currently not used but we want it for the future.
data[:action_hooks] ||= {}
hooks = data[:action_hooks][name.to_sym] ||= []
# Return the list if we don't have a block components.action_hooks << block
return hooks if !block_given?
# Otherwise add the block to the list of hooks for this action.
hooks << block
end end
# Defines additional command line commands available by key. The key # Defines additional command line commands available by key. The key

View File

@ -29,7 +29,7 @@ describe Vagrant::Plugin::V2::Plugin do
action_hook("foo") { "bar" } action_hook("foo") { "bar" }
end end
hooks = plugin.action_hook("foo") hooks = plugin.components.action_hooks
hooks.length.should == 1 hooks.length.should == 1
hooks[0].call.should == "bar" hooks[0].call.should == "bar"
end end