Module: Spinach::Hookable::InstanceMethods

Defined in:
lib/spinach/hookable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hooksHash

Returns hash in which the key is the hook name and the value an array of any defined callbacks, or nil.

Returns:

  • (Hash)

    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

Parameters:

  • name (String)

    the hook’s identifier

  • block (Proc)

    an action to perform once that hook is executed



75
76
77
78
# File 'lib/spinach/hookable.rb', line 75

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.

Parameters:

  • name (String)

    the hook’s identifier

Returns:

  • (Array)

    array of hooks for that particular identifier



64
65
66
# File 'lib/spinach/hookable.rb', line 64

def hooks_for(name)
  hooks[name.to_sym] || []
end

#resetObject

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) ⇒ Object

Runs a particular hook given a set of arguments

Parameters:

  • name (String)

    the hook’s name



53
54
55
56
57
# File 'lib/spinach/hookable.rb', line 53

def run_hook(name, *args)
  if callbacks = hooks[name.to_sym]
    callbacks.each{ |c| c.call(*args) }
  end
end