Sha256: f5b5762f241870acfc729f419920d3ed9656f670cc71633a54427b3622b08879

Contents?: true

Size: 1.43 KB

Versions: 50

Compression:

Stored size: 1.43 KB

Contents

# https://gist.github.com/ryanlecompte/1283413
module Gogetit
  module ExecutionHooks
    # this method is invoked whenever a new instance method is added to a class
    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

    # this is the DSL method that classes use to add before hooks
    def before_hook(method_name)
      hooks << method_name
    end

    # keeps track of all before hooks
    def hooks
      @hooks ||= []
    end

    private

    # keeps track of all currently hooked methods
    def hooked_methods
      @hooked_methods ||= []
    end

    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
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
gogetit-0.23.1 lib/executionhooks.rb
gogetit-0.22.10 lib/executionhooks.rb
gogetit-0.22.7 lib/executionhooks.rb
gogetit-0.22.6 lib/executionhooks.rb
gogetit-0.22.1 lib/executionhooks.rb
gogetit-0.22.0 lib/executionhooks.rb
gogetit-0.21.1 lib/executionhooks.rb
gogetit-0.21.0 lib/executionhooks.rb
gogetit-0.20.2 lib/executionhooks.rb
gogetit-0.20.1 lib/executionhooks.rb
gogetit-0.20.0 lib/executionhooks.rb
gogetit-0.19.0 lib/executionhooks.rb
gogetit-0.18.2 lib/executionhooks.rb
gogetit-0.18.1 lib/executionhooks.rb
gogetit-0.18.0 lib/executionhooks.rb
gogetit-0.17.0 lib/executionhooks.rb
gogetit-0.16.0 lib/executionhooks.rb
gogetit-0.15.2 lib/executionhooks.rb
gogetit-0.15.1 lib/executionhooks.rb
gogetit-0.15.0 lib/executionhooks.rb