module Bunto::Hooks
Constants
- DEFAULT_PRIORITY
- NotAvailable
- PRIORITY_MAP
compatibility layer for octopress-hooks users
- Uncallable
Public Class Methods
insert_hook(owner, event, priority, &block)
click to toggle source
# File lib/bunto/hooks.rb, line 82 def self.insert_hook(owner, event, priority, &block) @hook_priority[block] = [-priority, @hook_priority.size] @registry[owner][event] << block end
priority_value(priority)
click to toggle source
Ensure the priority is a Fixnum
# File lib/bunto/hooks.rb, line 56 def self.priority_value(priority) return priority if priority.is_a?(Integer) PRIORITY_MAP[priority] || DEFAULT_PRIORITY end
register(owners, event, priority: DEFAULT_PRIORITY, &block)
click to toggle source
register hook(s) to be called later, public API
# File lib/bunto/hooks.rb, line 49 def self.register(owners, event, priority: DEFAULT_PRIORITY, &block) Array(owners).each do |owner| register_one(owner, event, priority_value(priority), &block) end end
register_one(owner, event, priority, &block)
click to toggle source
register a single hook to be called later, internal API
# File lib/bunto/hooks.rb, line 62 def self.register_one(owner, event, priority, &block) @registry[owner] ||={ :post_init => [], :pre_render => [], :post_render => [], :post_write => [], } unless @registry[owner][event] raise NotAvailable, "Invalid hook. #{owner} supports only the " \ "following hooks #{@registry[owner].keys.inspect}" end unless block.respond_to? :call raise Uncallable, "Hooks must respond to :call" end insert_hook owner, event, priority, &block end
trigger(owner, event, *args)
click to toggle source
interface for Bunto
core components to trigger hooks
# File lib/bunto/hooks.rb, line 88 def self.trigger(owner, event, *args) # proceed only if there are hooks to call return unless @registry[owner] return unless @registry[owner][event] # hooks to call for this owner and event hooks = @registry[owner][event] # 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