Module: Guard::Hook

Included in:
Guard
Defined in:
lib/guard/hook.rb

Overview

Guard has a hook mechanism that allows you to insert callbacks for individual Guards. By default, each of the Guard 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).

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Class Method Details

.add_callback(listener, guard_class, events) ⇒ Object

Add a callback.

Parameters:

  • listener (Block)

    the listener to notify

  • guard_class (Guard::Guard)

    the Guard class to add the callback

  • events (Array<Symbol>)

    the events to register



80
81
82
83
84
85
# File 'lib/guard/hook.rb', line 80

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

.callbacksObject

Get all callbacks.



70
71
72
# File 'lib/guard/hook.rb', line 70

def callbacks
  @callbacks ||= Hash.new { |hash, key| hash[key] = [] }
end

.has_callback?(listener, guard_class, event) ⇒ Boolean

Checks if a callback has been registered.

Parameters:

  • listener (Block)

    the listener to notify

  • guard_class (Guard::Guard)

    the Guard class to add the callback

  • event (Symbol)

    the event to look for

Returns:

  • (Boolean)


93
94
95
# File 'lib/guard/hook.rb', line 93

def has_callback?(listener, guard_class, event)
  callbacks[[guard_class, event]].include?(listener)
end

.included(base) ⇒ Object

The Hook module gets included.

Parameters:

  • base (Class)

    the class that includes the module



16
17
18
# File 'lib/guard/hook.rb', line 16

def self.included(base)
  base.send :include, InstanceMethods
end

.notify(guard_class, event, *args) ⇒ Object

Notify a callback.

Parameters:

  • guard_class (Guard::Guard)

    the Guard class to add the callback

  • event (Symbol)

    the event to trigger

  • args (Array)

    the arguments for the listener



103
104
105
106
107
# File 'lib/guard/hook.rb', line 103

def notify(guard_class, event, *args)
  callbacks[[guard_class, event]].each do |listener|
    listener.call(guard_class, event, *args)
  end
end

.reset_callbacks!Object

Reset all callbacks.



111
112
113
# File 'lib/guard/hook.rb', line 111

def reset_callbacks!
  @callbacks = nil
end