Module: Redmine::Hook

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

Defined Under Namespace

Modules: Helper Classes: Listener, ViewListener

Constant Summary

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

Class Method Summary (collapse)

Class Method Details

+ (Object) add_listener(klass)

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



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

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

+ (Object) call_hook(hook, context = {})

Calls a hook. Returns the listeners response.



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

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

+ (Object) clear_listeners

Clears all the listeners.



46
47
48
49
# File 'lib/redmine/hook.rb', line 46

def clear_listeners
  @@listener_classes = []
  clear_listeners_instances
end

+ (Object) clear_listeners_instances

Clears all the listeners instances.



52
53
54
55
# File 'lib/redmine/hook.rb', line 52

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

+ (Object) hook_listeners(hook)

Returns the listeners instances for the given hook.



41
42
43
# File 'lib/redmine/hook.rb', line 41

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

+ (Object) listeners

Returns all the listerners instances.



36
37
38
# File 'lib/redmine/hook.rb', line 36

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