module Doing::Hooks
Hook manager
Constants
- DEFAULT_PRIORITY
Public Class Methods
Source
# 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
Source
# 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
Ensure the priority is a Fixnum
Source
# 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 hook(s) to be called later, public API
Source
# 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
register a single hook to be called later, internal API
Source
# 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