Sha256: 9845f5b6f9de1a2a3e16d3a6eb2d9a15baab7b783225875da758ac1752a9b709

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 KB

Contents

require "method_callbacks/finder"
require "method_callbacks/version"

module MethodCallbacks
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def after_method(method_name, *callback_names)
      define(method_name, :after, callback_names)
      define_with_block(method_name, :after, &Proc.new) if block_given?
    end

    def around_method(method_name, *callback_names)
      define(method_name, :around, callback_names)
    end

    def before_method(method_name, *callback_names)
      define(method_name, :before, callback_names)
      define_with_block(method_name, :before, &Proc.new) if block_given?
    end

    private

    def define(method_name, type, callback_names)
      find_or_new(method_name).define(type, callback_names)
    end

    def define_with_block(method_name, type)
      find_or_new(method_name).define_with_block(type, &Proc.new)
    end

    def find(method_name)
      finder(method_name).find
    end

    def finder(method_name)
      @_finder ||= {}
      @_finder[method_name] ||= MethodCallbacks::Finder.new(method_name)
    end

    def find_or_new(method_name)
      method = finder(method_name).find_or_new
      redefine_method(method)
      method
    end

    def method_added(method_name)
      redefine_method(find(method_name))

      super
    end

    def redefine_method(method)
      return if !method || method.locked? || !method_defined?(method.name) || method_defined?(method.alias)

      method.lock! && alias_method(method.alias, method.name)

      define_method(method.name) do
        method.execute(:before, self)
        return_value = method.execute(:around, self) do
          send(method.alias)
        end
        method.execute(:after, self)
        return_value
      end

      method.unlock!
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
method_callbacks-1.1.1 lib/method_callbacks.rb
method_callbacks-1.1.0 lib/method_callbacks.rb