我正在尝试编写一个通用模块来将method_missing模式应用于一些Rails模型的动态方法创建。这些模型有类方法和实例方法。虽然我可以写一个模块相当直截了当的任一类情况:覆盖类和实例方法的method_missing?
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
或实例情况:
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
...我似乎无法支持这两种情况下在同一个班。有没有更好的方法来覆盖method_missing,这样两种情况都被支持?我on Rails的3.2 ....
古朴典雅。谢谢! –
似乎关键是:如果通过模块向类中添加'method_missing',则还必须为模块提供'respond_to_missing?',否则'method_missing'不会生效。 –