Module: Spinach::Hookable::InstanceMethods
- Defined in:
- lib/spinach/hookable.rb
Instance Attribute Summary collapse
-
#hooks ⇒ Hash
Hash in which the key is the hook name and the value an array of any defined callbacks, or nil.
Instance Method Summary collapse
-
#add_hook(name, &block) ⇒ Object
Adds a hook to the queue.
-
#hooks_for(name) ⇒ Array
Array of hooks for that particular identifier.
-
#reset ⇒ Object
Resets all this class’ hooks to a pristine state.
-
#run_hook(name, *args, &block) ⇒ Object
Runs a particular hook given a set of arguments.
Instance Attribute Details
#hooks ⇒ Hash
Returns hash in which the key is the hook name and the value an array of any defined callbacks, or nil.
39 40 41 |
# File 'lib/spinach/hookable.rb', line 39 def hooks @hooks ||= {} end |
Instance Method Details
#add_hook(name, &block) ⇒ Object
Adds a hook to the queue
77 78 79 80 |
# File 'lib/spinach/hookable.rb', line 77 def add_hook(name, &block) hooks[name.to_sym] ||= [] hooks[name.to_sym] << block end |
#hooks_for(name) ⇒ Array
Returns array of hooks for that particular identifier.
66 67 68 |
# File 'lib/spinach/hookable.rb', line 66 def hooks_for(name) hooks[name.to_sym] || [] end |
#reset ⇒ Object
Resets all this class’ hooks to a pristine state
44 45 46 |
# File 'lib/spinach/hookable.rb', line 44 def reset self.hooks = {} end |
#run_hook(name, *args, &block) ⇒ Object
Runs a particular hook given a set of arguments
53 54 55 56 57 58 59 |
# File 'lib/spinach/hookable.rb', line 53 def run_hook(name, *args, &block) if callbacks = hooks[name.to_sym] callbacks.each{ |c| c.call(*args, &block) } else yield if block end end |