Module: Redmine::Hook

Defined in:
lib/redmine/hook.rb,
lib/redmine/hook/listener.rb,
lib/redmine/hook/view_listener.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.



29
30
31
32
33
34
35
36
# File 'lib/redmine/hook.rb', line 29

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

  @@listener_classes << klass
  clear_listeners_instances
end

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

Calls a hook. Returns the listeners response.



62
63
64
65
66
67
68
69
# File 'lib/redmine/hook.rb', line 62

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.



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

def clear_listeners
  @@listener_classes = []
  clear_listeners_instances
end

.clear_listeners_instancesObject

Clears all the listeners instances.



55
56
57
58
# File 'lib/redmine/hook.rb', line 55

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

.hook_listeners(hook) ⇒ Object

Returns the listener instances for the given hook.



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

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

.listenersObject

Returns all the listener instances.



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

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