我正在嘗試編寫一個通用模塊來將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'不會生效。 –