module Olelo::Hooks

Include this module to add hook support to your class. The class will be extended with {ClassMethods} which provides the methods to register hooks.

Public Class Methods

included(base) click to toggle source
# File lib/olelo/hooks.rb, line 34
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

invoke_hook(name, *args) click to toggle source

Invoke hooks registered for this class

The hooks can be registered using {ClassMethods#hook}.

@param [Symbol] name of hook to call @param *args Hook arguments @return [Array] [Hook results] @api public

# File lib/olelo/hooks.rb, line 66
def invoke_hook(name, *args)
  hooks = self.class.hooks[name.to_sym]
  raise "#{self.class} has no hook '#{name}'" if !hooks
  hooks.map {|prio,method| method.bind(self).(*args) }
end
with_hooks(name, *args) { || ... } click to toggle source

Execute block surrounded with hooks

It calls the hooks that were registered by {ClassMethods#before} and {ClassMethods#after} and returns an array consisting of the before hook results, the block result and the after hook results.

@param [Symbol] name of hook to call @param *args Hook arguments @return [Array] [*Before hook results, Block result, *After hook results] @api public

# File lib/olelo/hooks.rb, line 49
def with_hooks(name, *args)
  result = []
  result.push(*invoke_hook("BEFORE #{name}", *args))
  result << yield
ensure
  result.push(*invoke_hook("AFTER #{name}", *args))
end