module Olelo::Hooks::ClassMethods
Extends class with hook functionality
Public Instance Methods
Register before hook
The hook will be invoked by {#with_hooks}.
@param [Symbol, String] name of hook @param [Integer] priority @yield Hook block with arguments matching the hook invocation @return [void] @api public
# File lib/olelo/hooks.rb, line 137 def after(name, priority = 99, &block) hook("AFTER #{name}", priority, &block) end
Register before hook
The hook will be invoked by {#with_hooks}.
@param [Symbol, String] name of hook @param [Integer] priority @yield Hook block with arguments matching the hook invocation @return [void] @api public
# File lib/olelo/hooks.rb, line 123 def before(name, priority = 99, &block) hook("BEFORE #{name}", priority, &block) end
# File lib/olelo/hooks.rb, line 81 def has_around_hooks(*names) names.each do |name| has_hooks "BEFORE #{name}", "AFTER #{name}" end end
# File lib/olelo/hooks.rb, line 87 def has_hooks(*names) names.map(&:to_sym).each do |name| raise "#{self} already has hook '#{name}'" if hooks.include?(name) hooks[name] = [] end end
Register hook for class
The hook will be invoked by {#invoke_hook}. Hooks
with lower priority are called first.
@param [Symbol, String] name of hook @param [Integer] priority @yield Hook block with arguments matching the hook invocation @return [void] @api public
# File lib/olelo/hooks.rb, line 104 def hook(name, priority = 99, &block) list = hooks[name.to_sym] raise "#{self} has no hook '#{name}'" if !list method = "HOOK #{name} #{list.size}" define_method(method, &block) list << [priority, instance_method(method)] list.sort_by!(&:first) end
Hash
of registered hooks @api private @return [Hash] of hooks
# File lib/olelo/hooks.rb, line 77 def hooks @hooks ||= {} end