We’re having a rather unique problem with class caching. We are
implementing the strategy pattern in the following manner:
- Set an accessor for the class, let’s call it :strategy
- In environment.rb, we instantiate an object that implements the
strategy and use the accessor to set it for the class
strat = MyStrategy.new
Widget.strategy = strat
- In the regular flow of the application, that strategy gets accessed
so it can affect some behavior:
OK, now in development mode, class caching is off. In Rails 0.14.3 this
didn’t seem to matter – the class variable stayed set. But now
mysteriously in Rails 1.0 I will get an exception when trying to call
.do() on nil… ie, Widget.strategy becomes nil at some point. I’m
assuming the class gets unloaded and then reloaded without :strategy
ever getting set again. That’s likely because the environment never got
to set up the class variable again.
So, in 0.14.3, did the environment get evaluated again? Or did the class
never really get unloaded?
Does anyone know a way I can avoid this problem? Is there a way to
control class caching on a per-class basis? Looking at “module
Dependencies” makes me believe there is not.
Thanks for any help,