I am trying to write a general purpose module to apply the method_missing template to create a dynamic method to some of my Rails models. These models have both class methods and instance methods. Although I can write a module quite simply for any class:
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
def respond_to_missing?(method_name, include_private = false)
Async::async?(method_name) || super
end
end
end
or 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
def respond_to_missing?(method_name, include_private = false)
Async::async?(method_name) || super
end
end
... It seems that I do not support both cases in the same class. Is there a better way to override method_missing so that both cases are supported? I'm on Rails 3.2 ....
source
share