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
# File 'lib/aruba/hooks.rb', line 23

def append(label, block)
  unless store.key?(label.to_sym) && store[label.to_sym].respond_to?(:<<)
    store[label.to_sym] = []
  end
  store[label.to_sym] << block
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



40
41
42
43
44
# File 'lib/aruba/hooks.rb', line 40

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)


50
51
52
# File 'lib/aruba/hooks.rb', line 50

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