Module: Redmine::Hook

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.


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

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.


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

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.


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

def clear_listeners
  @@listener_classes = []
  clear_listeners_instances
end

.clear_listeners_instancesObject

Clears all the listeners instances.


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

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

.hook_listeners(hook) ⇒ Object

Returns the listener instances for the given hook.


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

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

.listenersObject

Returns all the listener instances.


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

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