Module: MiGA::Common::Hooks

Included in:
Dataset::Hooks, Project::Hooks
Defined in:
lib/miga/common/hooks.rb

Overview

Helper module including specific functions to handle dataset hooks.

Instance Method Summary collapse

Instance Method Details

#add_hook(event, action, *args) ⇒ Object

Whenever event occurs, launch action with parameters args.



25
26
27
# File 'lib/miga/common/hooks.rb', line 25

def add_hook(event, action, *args)
  (hooks[event] ||= []) << [action, *args]
end

#default_hooksObject

Default object’s hooks



37
38
39
# File 'lib/miga/common/hooks.rb', line 37

def default_hooks
  {}
end

#hook_run_lambda(hook_args, event_args) ⇒ Object

Run the function defined in the first hook argument



43
44
45
# File 'lib/miga/common/hooks.rb', line 43

def hook_run_lambda(hook_args, event_args)
  hook_args.first[*event_args]
end

#hooksObject

Get the stack of hooks



31
32
33
# File 'lib/miga/common/hooks.rb', line 31

def hooks
  @_hooks ||= default_hooks
end

#pull_hook(event, *event_args) ⇒ Object

Call the hook with symbol event and any parameters event_args



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/miga/common/hooks.rb', line 6

def pull_hook(event, *event_args)
  event = event.to_sym
  event_queue = (hooks[event] || [])
  event_queue += ([event] || []) if respond_to? :metadata
  event_queue.each do |i|
    action = i.first
    hook_name = :"hook_#{action}"
    hook_args = i[1..-1]
    if respond_to? hook_name
      MiGA::MiGA.DEBUG "Hook: #{self.class}(#{event} > #{action})"
      self.send(hook_name, hook_args, event_args)
    else
      raise "Cannot find action #{action} elicited by #{self.class}(#{event})"
    end
  end
end