Module: Spinach::Hookable::InstanceMethods
- Defined in:
- lib/spinach/hookable.rb
Instance Attribute Summary (collapse)
-
- (Hash) hooks
Hash in which the key is the hook name and the value an array of any defined callbacks, or nil.
Instance Method Summary (collapse)
-
- (Object) add_hook(name, &block)
Adds a hook to the queue.
-
- (Array) hooks_for(name)
Array of hooks for that particular identifier.
-
- (Object) reset
Resets all this class' hooks to a pristine state.
-
- (Object) run_hook(name, *args, &block)
Runs a particular hook given a set of arguments.
Instance Attribute Details
- (Hash) hooks
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
- (Object) add_hook(name, &block)
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 |
- (Array) hooks_for(name)
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 |
- (Object) reset
Resets all this class' hooks to a pristine state
44 45 46 |
# File 'lib/spinach/hookable.rb', line 44 def reset self.hooks = {} end |
- (Object) run_hook(name, *args, &block)
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 |