Class: Fixtury::Hooks
- Inherits:
-
Object
- Object
- Fixtury::Hooks
- Defined in:
- lib/fixtury/hooks.rb
Overview
Provides a mechanism for observing Fixtury lifecycle events.
Instance Attribute Summary collapse
-
#hooks ⇒ Object
readonly
Returns the value of attribute hooks.
Instance Method Summary collapse
-
#after(trigger_type, &hook) ⇒ Object
Register a hook to be called after the execution of a trigger.
-
#around(trigger_type, &hook) ⇒ Object
Register a hook to be called around the execution of a trigger.
-
#before(trigger_type, &hook) ⇒ Object
Register a hook to be called before the execution of a trigger.
-
#call(trigger_type, *args, &block) ⇒ Object
Trigger the hooks registered for a specific trigger type.
-
#initialize ⇒ Hooks
constructor
A new instance of Hooks.
-
#on(trigger_type, &hook) ⇒ Object
Similar to after, but the return value is not injected.
Constructor Details
#initialize ⇒ Hooks
Returns a new instance of Hooks.
9 10 11 |
# File 'lib/fixtury/hooks.rb', line 9 def initialize @hooks = Hash.new { |h, k| h[k] = { before: [], after: [], around: [], on: [] } } end |
Instance Attribute Details
#hooks ⇒ Object (readonly)
Returns the value of attribute hooks.
7 8 9 |
# File 'lib/fixtury/hooks.rb', line 7 def hooks @hooks end |
Instance Method Details
#after(trigger_type, &hook) ⇒ Object
Register a hook to be called after the execution of a trigger. The return value will be provided as the first argument to the hook. (see #register_hook)
32 33 34 |
# File 'lib/fixtury/hooks.rb', line 32 def after(trigger_type, &hook) register_hook(trigger_type, :after, hook) end |
#around(trigger_type, &hook) ⇒ Object
Register a hook to be called around the execution of a trigger. The around hook should ensure the return value is preserved. This also means that the hook itself could modify the return value.
19 20 21 |
# File 'lib/fixtury/hooks.rb', line 19 def around(trigger_type, &hook) register_hook(trigger_type, :around, hook) end |
#before(trigger_type, &hook) ⇒ Object
Register a hook to be called before the execution of a trigger. (see #register_hook)
25 26 27 |
# File 'lib/fixtury/hooks.rb', line 25 def before(trigger_type, &hook) register_hook(trigger_type, :before, hook) end |
#call(trigger_type, *args, &block) ⇒ Object
Trigger the hooks registered for a specific trigger type. :before hooks will be triggered first, followed by :around hooks, :on hooks, and finally :after hooks.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fixtury/hooks.rb', line 50 def call(trigger_type, *args, &block) hook_lists = hooks[trigger_type.to_sym] call_inline_hooks(hook_lists[:before], *args) return_value = call_around_hooks(hook_lists[:around], 0, block, *args) call_inline_hooks(hook_lists[:on], *args) call_inline_hooks(hook_lists[:after], return_value, *args) return_value end |
#on(trigger_type, &hook) ⇒ Object
Similar to after, but the return value is not injected. (see #register_hook)
38 39 40 |
# File 'lib/fixtury/hooks.rb', line 38 def on(trigger_type, &hook) register_hook(trigger_type, :on, hook) end |