module MiGA::Common::Hooks

Helper module including specific functions to handle dataset hooks.

Public Instance Methods

add_hook(event, action, *args) click to toggle source

Whenever event occurs, launch action with parameters args.

# File lib/miga/common/hooks.rb, line 25
def add_hook(event, action, *args)
  (hooks[event] ||= []) << [action, *args]
end
default_hooks() click to toggle source

Default object's hooks

# File lib/miga/common/hooks.rb, line 37
def default_hooks
  {}
end
hook_run_lambda(hook_args, event_args) click to toggle source

Run the function defined in the first hook argument

# File lib/miga/common/hooks.rb, line 43
def hook_run_lambda(hook_args, event_args)
  hook_args.first[*event_args]
end
hooks() click to toggle source

Get the stack of hooks

# File lib/miga/common/hooks.rb, line 31
def hooks
  @_hooks ||= default_hooks
end
pull_hook(event, *event_args) click to toggle source

Call the hook with symbol event and any parameters event_args

# File lib/miga/common/hooks.rb, line 6
def pull_hook(event, *event_args)
  event = event.to_sym
  event_queue = (hooks[event] || [])
  event_queue += (metadata[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