module Spinach::Hookable::InstanceMethods

Attributes

hooks[W]

Public Instance Methods

add_hook(name, &block) click to toggle source

Adds a hook to the queue

@param [String] name

the hook's identifier

@param [Proc] block

an action to perform once that hook is executed
# File lib/spinach/hookable.rb, line 119
def add_hook(name, &block)
  hooks[name.to_sym] ||= []
  hooks[name.to_sym] << block
end
hooks() click to toggle source

@return [Hash]

hash in which the key is the hook name and the value an array of any
defined callbacks, or nil.
# File lib/spinach/hookable.rb, line 55
def hooks
  @hooks ||= {}
end
hooks_for(name) click to toggle source

@param [String] name

the hook's identifier

@return [Array]

array of hooks for that particular identifier
# File lib/spinach/hookable.rb, line 108
def hooks_for(name)
  hooks[name.to_sym] || []
end
reset() click to toggle source

Resets all this class' hooks to a pristine state

# File lib/spinach/hookable.rb, line 60
def reset
  self.hooks = {}
end
run_around_hook(name, *args) { || ... } click to toggle source

Runs around hooks in a way that ensure the scenario block is executed only once

@param [String] name

the around hook's name

@param [] args

the list of arguments to pass to other around filters

@param [Proc] block

the block containing the scenario action to be executed
# 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
run_hook(name, *args) { || ... } click to toggle source

Runs a particular hook given a set of arguments

@param [String] name

the hook's name
# 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