Guard has a hook mechanism that allows you to insert callbacks for individual Guard plugins. By default, each of the Guard plugin instance methods has a "_begin" and an "_end" hook. For example, the Guard::Guard#start method has a :start_begin hook that is runs immediately before Guard::Guard#start, and a :start_end hook that runs immediately after Guard::Guard#start.
Read more about [hooks and callbacks on the wiki](github.com/guard/guard/wiki/Hooks-and-callbacks).
Add a callback.
@param [Block] listener the listener to notify @param [Guard::Guard] guard_class the Guard class to add the callback @param [Array<Symbol>] events the events to register
# File lib/guard/hook.rb, line 82 def add_callback(listener, guard_class, events) _events = events.is_a?(Array) ? events : [events] _events.each do |event| callbacks[[guard_class, event]] << listener end end
Get all callbacks.
# File lib/guard/hook.rb, line 72 def callbacks @callbacks ||= Hash.new { |hash, key| hash[key] = [] } end
Checks if a callback has been registered.
@param [Block] listener the listener to notify @param [Guard::Guard] guard_class the Guard class to add the callback @param [Symbol] event the event to look for
# File lib/guard/hook.rb, line 95 def has_callback?(listener, guard_class, event) callbacks[[guard_class, event]].include?(listener) end
The Hook module gets included.
@param [Class] base the class that includes the module
# File lib/guard/hook.rb, line 18 def self.included(base) base.send :include, InstanceMethods end
Notify a callback.
@param [Guard::Guard] guard_class the Guard class to add the callback @param [Symbol] event the event to trigger @param [Array] args the arguments for the listener
# File lib/guard/hook.rb, line 105 def notify(guard_class, event, *args) callbacks[[guard_class, event]].each do |listener| listener.call(guard_class, event, *args) end end
Generated with the Darkfish Rdoc Generator 2.