module Gogetit::ExecutionHooks

Public Instance Methods

before_hook(method_name) click to toggle source

this is the DSL method that classes use to add before hooks

# File lib/executionhooks.rb, line 13
def before_hook(method_name)
  hooks << method_name
end
hooks() click to toggle source

keeps track of all before hooks

# File lib/executionhooks.rb, line 18
def hooks
  @hooks ||= []
end
method_added(method_name) click to toggle source

this method is invoked whenever a new instance method is added to a class

# File lib/executionhooks.rb, line 5
def method_added(method_name)
  # do nothing if the method that was added was an actual hook method, or
  # if it already had hooks added to it
  return if hooks.include?(method_name) || hooked_methods.include?(method_name)
  add_hooks_to(method_name)
end

Private Instance Methods

add_hooks_to(method_name) click to toggle source
# File lib/executionhooks.rb, line 29
def add_hooks_to(method_name)
  # add this method to known hook mappings to avoid infinite
  # recursion when we redefine the method below
  hooked_methods << method_name

  # grab the original method definition
  original_method = instance_method(method_name)

  # re-define the method, but notice how we reference the original
  # method definition
  define_method(method_name) do |*args, &block|
    # invoke the hook methods
    self.class.hooks.each do |hook|
      method(hook).call
    end

    # now invoke the original method
    original_method.bind(self).call(*args, &block)
  end
end
hooked_methods() click to toggle source

keeps track of all currently hooked methods

# File lib/executionhooks.rb, line 25
def hooked_methods
  @hooked_methods ||= []
end