module Guard::Hook
def self.included(base)
-
base
(Class
) -- the class that includes the module
def self.included(base) base.send :include, InstanceMethods end
def add_callback(listener, guard_class, events)
-
events
(Array
) -- the events to register -
guard_class
(Guard::Guard
) -- the Guard class to add the callback -
listener
(Block
) -- the listener to notify
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
def callbacks
Get all callbacks.
def callbacks @callbacks ||= Hash.new { |hash, key| hash[key] = [] } end
def has_callback?(listener, guard_class, event)
-
event
(Symbol
) -- the event to look for -
guard_class
(Guard::Guard
) -- the Guard class to add the callback -
listener
(Block
) -- the listener to notify
def has_callback?(listener, guard_class, event) callbacks[[guard_class, event]].include?(listener) end
def notify(guard_class, event, *args)
-
args
(Array
) -- the arguments for the listener -
event
(Symbol
) -- the event to trigger -
guard_class
(Guard::Guard
) -- the Guard class to add the callback
def notify(guard_class, event, *args) callbacks[[guard_class, event]].each do |listener| listener.call(guard_class, event, *args) end end
def reset_callbacks!
Reset all callbacks.
def reset_callbacks! @callbacks = nil end