0

有人可以帮我修改为拦截实例方法调用提供的答案,以便它适用于类方法调用或类和实例方法调用吗?从我对使用 Ruby 进行元编程的有限知识来看,我想这与在某个地方使用class << self.出来。但是,您能否为我提供正确方向的推动,而不是直接回答?除非我完全超出我的深度,否则我非常喜欢为自己解决问题。谢谢!

4

1 回答 1

2

这是我根据您提供的链接中的答案修改的解决方案。我将钩子逻辑从超类移动到一个单独的模块,这样当一个类需要钩子时,它只包含或扩展该模块并调用钩子方法。

  • before_each_method type, &block-type可以是:classor :instance,块是每个方法之前要执行的代码。块在一定的环境下会被求值,即实例方法,self块中就是实例;对于类方法,self块中是类。
  • before_class_method &block- 别名before_each_method :class, &block
  • before_instance_method &block- 别名before_each_method :instance, &block
module MethodHooker
  def self.included(base)
    base.extend(ClassMethods)
  end
  def self.extended(base)
    base.extend(ClassMethods)
  end
  module ClassMethods
    def before_each_method type, &block
      singleton = class << self; self; end
      case type
      when :instance
        this = self
        singleton.instance_eval do
          define_method :method_added do |name|
            last = instance_variable_get(:@__last_methods_added)
            return if last and last.include?(name)
            with = :"#{name}_with_before_each_method"
            without = :"#{name}_without_before_each_method"
            instance_variable_set(:@__last_methods_added, [name, with, without])
            this.class_eval do
              define_method with do |*args, &blk|
                instance_exec(name, args, blk, &block)
                send without, *args, &blk
              end
              alias_method without, name
              alias_method name, with
            end
            instance_variable_set(:@__last_methods_added, nil)
          end
        end
      when :class
        this = self
        singleton.instance_eval do
          define_method :singleton_method_added do |name|
            return if name == :singleton_method_added
            last = instance_variable_get(:@__last_singleton_methods_added)
            return if last and last.include?(name)
            with = :"#{name}_with_before_each_method"
            without = :"#{name}_without_before_each_method"
            instance_variable_set(:@__last_singleton_methods_added, [name, with, without])
            singleton.class_eval do
              define_method with do |*args, &blk|
                instance_exec(name, args, blk, &block)
                send without, *args, &blk
              end
              alias_method without, name
              alias_method name, with
            end
            instance_variable_set(:@__last_singleton_methods_added, nil)
          end
        end
      end
    end
    def before_class_method &block
      before_each_method :class, &block
    end
    def before_instance_method &block
      before_each_method :instance, &block
    end
  end
end

class Test
  extend MethodHooker
  before_each_method :instance do |method, args, block|
    p [method, args, block]
    puts "before instance method(#{method}) #{@var}"
  end
  before_class_method do |method, args, block|
    puts "before class method(#{method}) #{@class_instance_var}"
  end
  @class_instance_var = 'stackoverflow'
  def initialize
    @var = 1
  end
  def test(a, b, c)
    puts "instance method test"
  end
  def self.test1
    puts "class method test"
  end
end

Test.new.test(1, "arg2", [3]) {|t| t}
Test.test1

输出将类似于:

[:initialize, [], nil]
before instance method(initialize)
[:test, [1, "arg2", [3]], #<Proc:0x00000001017d5eb8@/Users/test/before_method.rb:88>]
before instance method(test) 1
instance method test
before class method(test1) stackoverflow
class method test
于 2013-06-27T07:06:26.830 回答