I’d like to have const_missing return a class that would do the
following:
- Have the same class name as the class it should have loaded but
without loading the class. Let’s call this “proxy”.
That part is easy, as I’ve done similar in my constance gem. Here is
where it starts to get more difficult:
- Implement method missing such that when “proxy” is called it in turn
calls another class that unloads the proxy class and then loads the
originally requested file.
Implementing method missing is easy, and removing a constant not hard
either, but how can an unloaded class return something from method
missing when it is gone? That wouldn’t work.
So then you might say “of course not, just use method missing and return
from the proxy”, but if possible I don’t want to have the “proxy” class
act as a wrapper or delegate all calls because that is additional
overhead. Maybe it would be ok and transparent and I could mess with the
caller stack to make it appear as if the proxy were not even there, but
I think eventually the magic go-between wrapper would be a problem.
This actually isn’t an academic question- I was trying to think of a
solution to the problem of people using names of classes rather than the
constants in class bodies where associations, etc. are defined in Rails
(note: this is not a Rails question, but yes that is where I’d like to
use it). By returning a flyweight proxy class that loads and calls the
real class as needed, they wouldn’t need to use strings instead of
classnames because const_missing would return something that didn’t get
into some sort of massive autoloading disaster that the community is
afraid of.
Sorry if this just sounds idiotic.