Helper module including specific functions to handle dataset hooks.
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 object's hooks
# File lib/miga/common/hooks.rb, line 37 def default_hooks {} end
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
Get the stack of hooks
# File lib/miga/common/hooks.rb, line 31 def hooks @_hooks ||= default_hooks end
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