Class: Aruba::Hooks

Inherits:
Object
  • Object
show all
Defined in:
lib/aruba/hooks.rb

Overview

Aruba Hooks

Instance Method Summary collapse

Constructor Details

#initializeHooks

Create store


12
13
14
# File 'lib/aruba/hooks.rb', line 12

def initialize
  @store = {}
end

Instance Method Details

#append(label, block) ⇒ Object

Add new hook

Parameters:

  • label (String, Symbol)

    The name of the hook

  • block (Proc)

    The block which should be run for the hook


23
24
25
26
27
28
29
30
# File 'lib/aruba/hooks.rb', line 23

def append(label, block)
  if store.key?(label.to_sym) && store[label.to_sym].respond_to?(:<<)
    store[label.to_sym] << block
  else
    store[label.to_sym] = []
    store[label.to_sym] << block
  end
end

#execute(label, context, *args) ⇒ Object

Run hook

Parameters:

  • label (String, Symbol)

    The name of the hook

  • context (Object)

    The context in which the hook is run

  • args (Array)

    Other arguments


42
43
44
45
46
# File 'lib/aruba/hooks.rb', line 42

def execute(label, context, *args)
  Array(store[label.to_sym]).each do |block|
    context.instance_exec(*args, &block)
  end
end

#exist?(label) ⇒ Boolean

Check if hook exist

Parameters:

  • label (String, Symbol)

    The name of the hook

Returns:

  • (Boolean)

52
53
54
# File 'lib/aruba/hooks.rb', line 52

def exist?(label)
  store.key? label.to_sym
end