module Doing::Hooks

Hook manager

Constants

DEFAULT_PRIORITY

Public Class Methods

insert_hook(event, priority, &block) click to toggle 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
priority_value(priority) click to toggle source

Ensure the priority is a Fixnum

# 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) click to toggle source

register hook(s) to be called later, public API

# 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) click to toggle source

register a single hook to be called later, internal API

# 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) click to toggle 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