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_around_hook(name, *args, &block)
Runs around hooks in a way that ensure the scenario block is executed only once.
-
- (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.
55 56 57 |
# File 'lib/spinach/hookable.rb', line 55 def hooks @hooks ||= {} end |
Instance Method Details
- (Object) add_hook(name, &block)
Adds a hook to the queue
119 120 121 122 |
# File 'lib/spinach/hookable.rb', line 119 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
108 109 110 |
# File 'lib/spinach/hookable.rb', line 108 def hooks_for(name) hooks[name.to_sym] || [] end |
- (Object) reset
Resets all this class' hooks to a pristine state
60 61 62 |
# File 'lib/spinach/hookable.rb', line 60 def reset self.hooks = {} end |
- (Object) run_around_hook(name, *args, &block)
Runs around hooks in a way that ensure the scenario block is executed only once
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/spinach/hookable.rb', line 75 def run_around_hook(name, *args, &block) raise ArgumentError.new("block is mandatory") unless block if callbacks = hooks[name.to_sym] callbacks.reverse.inject(block) do |blk, callback| proc do callback.call *args do blk.call end end end.call else yield end end |
- (Object) run_hook(name, *args, &block)
Runs a particular hook given a set of arguments
95 96 97 98 99 100 101 |
# File 'lib/spinach/hookable.rb', line 95 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 |