module ExecutionHooks::ClassMethods

Public Instance Methods

before_hook(method_name, opts={}) click to toggle source

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

# File lib/capybara/widgets/core/execution_hooks.rb, line 16
def before_hook(method_name, opts={})
  hooks << method_name
  ignored_methods.concat Array(opts[:ignore])
end
hooks() click to toggle source

keeps track of all before hooks

# File lib/capybara/widgets/core/execution_hooks.rb, line 22
def hooks
  @hooks ||= []
end
ignored_methods() click to toggle source

keeps track of all before hooks

# File lib/capybara/widgets/core/execution_hooks.rb, line 27
def ignored_methods
  @ignored_methods ||= []
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/capybara/widgets/core/execution_hooks.rb, line 8
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) || ignored_methods.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/capybara/widgets/core/execution_hooks.rb, line 38
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
    hooks_to_run = []
    klass = self.class
    while klass != BasicObject do
      hooks_to_run.concat(klass.hooks) if klass.respond_to? :hooks
      klass = klass.superclass
    end
    hooks_to_run.each do |h|
      # LOGGER.debug "calling hook #{h} for method: #{method_name}"
      method(h).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/capybara/widgets/core/execution_hooks.rb, line 34
def hooked_methods
  @hooked_methods ||= []
end