简体   繁体   English

覆盖类和实例方法的method_missing?

[英]Override method_missing for class and instance methods?

I'm trying to write a general purpose module to apply the method_missing pattern for dynamic method creation to some of my Rails models. 我正在尝试编写一个通用模块,将dynamic_missing模式应用于我的一些Rails模型中。 These models have both class methods and instance methods. 这些模型具有类方法和实例方法。 While I can write a module fairly straightforwardly for either the class case: 虽然我可以相当直接地为类案例编写一个模块:

  module ClassVersion
    extend ActiveSupport::Concern

    module ClassMethods
      def method_missing(meth, *args, &block)
        if meth.to_s =~ /^(.+)_async$/
          Async::handle_async self, $1, *args, &block
        else
          super meth, *args, &block
        end
      end

      # Logic for this method MUST match that of the detection in method_missing
      def respond_to_missing?(method_name, include_private = false)
        Async::async?(method_name) || super
      end
    end
  end

or the instance case: 或实例案例:

  module InstanceVersion
    extend ActiveSupport::Concern

    def method_missing(meth, *args, &block)
      if meth.to_s =~ /^(.+)_async$/
        Async::handle_async self, $1, *args, &block
      else
        super meth, *args, &block
      end
    end

    # Logic for this method MUST match that of the detection in method_missing
    def respond_to_missing?(method_name, include_private = false)
      Async::async?(method_name) || super
    end
  end

... I can't seem to support both cases in the same class. ......我似乎无法在同一个班级支持这两种情况。 Is there a better way to override method_missing such that both cases are supported? 有没有更好的方法来覆盖method_missing,以便支持这两种情况? I'm on Rails 3.2.... 我在Rails 3.2上....

What you are trying to achieve is pretty straightforward, and unusual at the same time. 你想要达到的目标非常简单,同时又不同寻常。 ActiveSupport::Concern is giving you possibility of defining nested ClassMethods module that is extending base class on included module hook. ActiveSupport::Concern让您可以定义嵌套的ClassMethods模块,该模块在included模块挂钩上扩展基类。 Usually this is handy, because you don't want to augment both class and its instances with the same methods. 通常这很方便,因为您不希望使用相同的方法扩充类及其实例。

What you need to do is stop using ActiveSupport::Concern and write included hook to meet your particular needs: 您需要做的是停止使用ActiveSupport::Concern并编写included钩子以满足您的特定需求:

module AsyncModule
  def method_missing(meth, *args, &block)
    if meth.to_s =~ /^(.+)_async$/
      Async::handle_async self, $1, *args, &block
    else
      super meth, *args, &block
    end
  end

  # Logic for this method MUST match that of the detection in method_missing
  def respond_to_missing?(method_name, include_private = false)
    Async::async?(method_name) || super
  end

  private

  def self.included(base)
    base.extend self
  end
end

have you tried the following 你试过以下吗?

  module ClassAndInstanceVersion
    extend ActiveSupport::Concern

    def method_missing(meth, *args, &block)
      self.class.method_missing(meth, *args, &block)
    end

    def respond_to_missing?(method_name, include_private = false)
      self.class.respond_to_missing?(method_name, include_private)
    end

    module ClassMethods
      def method_missing(meth, *args, &block)
        if meth.to_s =~ /^(.+)_async$/
          Async::handle_async self, $1, *args, &block
        else
          super meth, *args, &block
        end
      end

      # Logic for this method MUST match that of the detection in method_missing
      def respond_to_missing?(method_name, include_private = false)
        Async::async?(method_name) || super
      end
    end
  end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM