Files

Guard::Plugin::Hooker

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::Plugin#start method has a :start_begin hook that is runs immediately before Guard::Plugin#start, and a :start_end hook that runs immediately after Guard::Plugin#start.

Read more about [hooks and callbacks on the wiki](github.com/guard/guard/wiki/Hooks-and-callbacks).

Public Class Methods

add_callback(listener, guard_plugin, events) click to toggle source

Add a callback.

@param [Block] listener the listener to notify @param [Guard::Plugin] guard_plugin the Guard plugin to add the callback @param [Array<Symbol>] events the events to register

# File lib/guard/plugin/hooker.rb, line 33
def self.add_callback(listener, guard_plugin, events)
  _events = events.is_a?(Array) ? events : [events]
  _events.each do |event|
    callbacks[[guard_plugin, event]] << listener
  end
end
callbacks() click to toggle source

Get all callbacks registered for all Guard plugins present in the Guardfile.

# File lib/guard/plugin/hooker.rb, line 23
def self.callbacks
  @callbacks ||= Hash.new { |hash, key| hash[key] = [] }
end
has_callback?(listener, guard_plugin, event) click to toggle source

Checks if a callback has been registered.

@param [Block] listener the listener to notify @param [Guard::Plugin] guard_plugin the Guard plugin to add the callback @param [Symbol] event the event to look for

# File lib/guard/plugin/hooker.rb, line 46
def self.has_callback?(listener, guard_plugin, event)
  callbacks[[guard_plugin, event]].include?(listener)
end
notify(guard_plugin, event, *args) click to toggle source

Notify a callback.

@param [Guard::Plugin] guard_plugin the Guard plugin to add the callback @param [Symbol] event the event to trigger @param [Array] args the arguments for the listener

# File lib/guard/plugin/hooker.rb, line 56
def self.notify(guard_plugin, event, *args)
  callbacks[[guard_plugin, event]].each do |listener|
    listener.call(guard_plugin, event, *args)
  end
end
reset_callbacks!() click to toggle source

Reset all callbacks.

# File lib/guard/plugin/hooker.rb, line 64
def self.reset_callbacks!
  @callbacks = nil
end

Public Instance Methods

hook(event, *args) click to toggle source

When event is a Symbol, {hook} will generate a hook name by concatenating the method name from where {hook} is called with the given Symbol.

@example Add a hook with a Symbol

def run_all
  hook :foo
end

Here, when {Guard::Plugin::Base#run_all} is called, {hook} will notify callbacks registered for the "run_all_foo" event.

When event is a String, {hook} will directly turn the String into a Symbol.

@example Add a hook with a String

def run_all
  hook "foo_bar"
end

When {Guard::Plugin::Base#run_all} is called, {hook} will notify callbacks registered for the "foo_bar" event.

@param [Symbol, String] event the name of the Guard event @param [Array] args the parameters are passed as is to the callbacks

registered for the given event.
# File lib/guard/plugin/hooker.rb, line 97
def hook(event, *args)
  hook_name = if event.is_a? Symbol
                calling_method = caller[0][/`([^']*)'/, 1]
                "#{ calling_method }_#{ event }"
              else
                event
              end

  ::Guard::UI.debug "Hook :#{ hook_name } executed for #{ self }"

  Hooker.notify(self, hook_name.to_sym, *args)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.