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
-
.add_callback(listener, guard_class, events) ⇒ Object
Add a callback.
-
.callbacks ⇒ Object
Get all callbacks.
-
.has_callback?(listener, guard_class, event) ⇒ Boolean
Checks if a callback has been registered.
-
.included(base) ⇒ Object
The Hook module gets included.
-
.notify(guard_class, event, *args) ⇒ Object
Notify a callback.
-
.reset_callbacks! ⇒ Object
Reset all callbacks.
Class Method Details
.add_callback(listener, guard_class, events) ⇒ Object
Add a callback.
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 |
.callbacks ⇒ Object
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.
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.
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.
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 |