Module: Redmine::Hook

Includes:
ActionController::UrlWriter
Defined in:
lib/redmine/hook.rb

Defined Under Namespace

Modules: Helper Classes: Listener, ViewListener

Constant Summary collapse

@@listener_classes =
[]
@@listeners =
nil
@@hook_listeners =
{}

Class Method Summary collapse

Class Method Details

.add_listener(klass) ⇒ Object

Adds a listener class. Automatically called when a class inherits from Redmine::Hook::Listener.


26
27
28
29
30
# File 'lib/redmine/hook.rb', line 26

def add_listener(klass)
  raise "Hooks must include Singleton module." unless klass.included_modules.include?(Singleton)
  @@listener_classes << klass
  clear_listeners_instances
end

.call_hook(hook, context = {}) ⇒ Object

Calls a hook. Returns the listeners response.


56
57
58
59
60
61
62
63
# File 'lib/redmine/hook.rb', line 56

def call_hook(hook, context={})
  [].tap do |response|
    hls = hook_listeners(hook)
    if hls.any?
      hls.each {|listener| response << listener.send(hook, context)}
    end
  end
end

.clear_listenersObject

Clears all the listeners.


43
44
45
46
# File 'lib/redmine/hook.rb', line 43

def clear_listeners
  @@listener_classes = []
  clear_listeners_instances
end

.clear_listeners_instancesObject

Clears all the listeners instances.


49
50
51
52
# File 'lib/redmine/hook.rb', line 49

def clear_listeners_instances
  @@listeners = nil
  @@hook_listeners = {}
end

.hook_listeners(hook) ⇒ Object

Returns the listeners instances for the given hook.


38
39
40
# File 'lib/redmine/hook.rb', line 38

def hook_listeners(hook)
  @@hook_listeners[hook] ||= listeners.select {|listener| listener.respond_to?(hook)}
end

.listenersObject

Returns all the listerners instances.


33
34
35
# File 'lib/redmine/hook.rb', line 33

def listeners
  @@listeners ||= @@listener_classes.collect {|listener| listener.instance}
end