Module: Doing::Hooks
- Defined in:
- lib/doing/hooks.rb
Overview
Hook manager
Constant Summary collapse
- DEFAULT_PRIORITY =
20
Class Method Summary collapse
- .insert_hook(event, priority, &block) ⇒ Object
-
.priority_value(priority) ⇒ Object
Ensure the priority is a Fixnum.
-
.register(event, priority: DEFAULT_PRIORITY, &block) ⇒ Object
register hook(s) to be called later, public API.
-
.register_one(event, priority, &block) ⇒ Object
register a single hook to be called later, internal API.
- .trigger(event, *args) ⇒ Object
Class Method Details
.insert_hook(event, priority, &block) ⇒ Object
53 54 55 56 |
# File 'lib/doing/hooks.rb', line 53 def self.insert_hook(event, priority, &block) @hook_priority[block] = [-priority, @hook_priority.size] @registry[event] << block end |
.priority_value(priority) ⇒ Object
Ensure the priority is a Fixnum
34 35 36 37 38 |
# File 'lib/doing/hooks.rb', line 34 def self.priority_value(priority) return priority if priority.is_a?(Integer) PRIORITY_MAP[priority] || DEFAULT_PRIORITY end |
.register(event, priority: DEFAULT_PRIORITY, &block) ⇒ Object
register hook(s) to be called later, public API
25 26 27 28 29 30 31 |
# File 'lib/doing/hooks.rb', line 25 def self.register(event, priority: DEFAULT_PRIORITY, &block) if event.is_a?(Array) event.each { |ev| register_one(ev, priority_value(priority), &block) } else register_one(event, priority_value(priority), &block) end end |
.register_one(event, priority, &block) ⇒ Object
register a single hook to be called later, internal API
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/doing/hooks.rb', line 41 def self.register_one(event, priority, &block) unless @registry[event] raise Doing::Errors::HookUnavailable.new("Invalid hook. Doing only supports #{@registry.keys.inspect}", 'hook', event) end raise Doing::Errors::PluginUncallable.new('Hooks must respond to :call', 'hook', event) unless block.respond_to? :call Doing.logger.debug('Hook Manager:', "Registered #{event} hook") if ENV['DOING_PLUGIN_DEBUG'] insert_hook event, priority, &block end |
.trigger(event, *args) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/doing/hooks.rb', line 58 def self.trigger(event, *args) hooks = @registry[event] return unless hooks.good? # sort and call hooks according to priority and load order hooks.sort_by { |h| @hook_priority[h] }.each do |hook| hook.call(*args) end end |