Plugins can define action hooks via action_hook
This commit is contained in:
parent
83bba789a4
commit
aa7193471f
|
@ -6,12 +6,19 @@ module Vagrant
|
|||
# components, and the actual container of those components. This
|
||||
# removes a bit of state overhead from the plugin class itself.
|
||||
class Components
|
||||
# This contains all the action hooks.
|
||||
#
|
||||
# @return [Array<Proc>]
|
||||
attr_reader :action_hooks
|
||||
|
||||
# This contains all the configuration plugins by scope.
|
||||
#
|
||||
# @return [Hash<Symbol, Registry>]
|
||||
attr_reader :configs
|
||||
|
||||
def initialize
|
||||
@action_hooks = []
|
||||
|
||||
# Create the configs hash which defaults to a registry
|
||||
@configs = Hash.new { |h, k| h[k] = Registry.new }
|
||||
end
|
||||
|
|
|
@ -65,18 +65,12 @@ module Vagrant
|
|||
# is run. This allows plugin authors to hook into things like VM
|
||||
# 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.
|
||||
def self.action_hook(name, &block)
|
||||
# Get the list of hooks for the given hook name
|
||||
data[:action_hooks] ||= {}
|
||||
hooks = data[:action_hooks][name.to_sym] ||= []
|
||||
# The name is currently not used but we want it for the future.
|
||||
|
||||
# Return the list if we don't have a block
|
||||
return hooks if !block_given?
|
||||
|
||||
# Otherwise add the block to the list of hooks for this action.
|
||||
hooks << block
|
||||
components.action_hooks << block
|
||||
end
|
||||
|
||||
# Defines additional command line commands available by key. The key
|
||||
|
|
|
@ -29,7 +29,7 @@ describe Vagrant::Plugin::V2::Plugin do
|
|||
action_hook("foo") { "bar" }
|
||||
end
|
||||
|
||||
hooks = plugin.action_hook("foo")
|
||||
hooks = plugin.components.action_hooks
|
||||
hooks.length.should == 1
|
||||
hooks[0].call.should == "bar"
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue