Module: Olelo::Hooks
- Included in:
- Application, Page, Plugin
- Defined in:
- lib/olelo/hooks.rb
Overview
Include this module to add hook support to your class. The class will be extended with ClassMethods which provides the methods to register hooks.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#invoke_hook(name, *args) ⇒ Array
Invoke hooks registered for this class.
-
#with_hooks(name, *args) ⇒ Array
Execute block surrounded with hooks.
Class Method Details
.included(base) ⇒ Object
34 35 36 |
# File 'lib/olelo/hooks.rb', line 34 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#invoke_hook(name, *args) ⇒ Array
Invoke hooks registered for this class
The hooks can be registered using Olelo::Hooks::ClassMethods#hook.
66 67 68 69 70 |
# 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) ⇒ Array
Execute block surrounded with hooks
It calls the hooks that were registered by Olelo::Hooks::ClassMethods#before and Olelo::Hooks::ClassMethods#after and returns an array consisting of the before hook results, the block result and the after hook results.
49 50 51 52 53 54 55 |
# 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 |