Forum: Ruby on Rails Changes to class caching between Rails 0.14.3 and 1.0?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
rusty geldmacher (Guest)
on 2006-03-10 05:44
Hi all,

We're having a rather unique problem with class caching. We are
implementing the strategy pattern in the following manner:

1. Set an accessor for the class, let's call it :strategy

  class Widget
    cattr_accessor :strategy
  end

2. 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

3. In the regular flow of the application, that strategy gets accessed
so it can affect some behavior:

  ...
  Widget.strategy.do(:foo)
  ...

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,
rusty
Rick O. (Guest)
on 2006-03-10 05:59
(Received via mailing list)
> 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,
> rusty

Try this on edge rails...

class Widget < AR::Base
  def self.reloadable?() false; end
end

--
Rick O.
http://techno-weenie.net
Rick O. (Guest)
on 2006-03-10 05:59
(Received via mailing list)
> Try this on edge rails...
>
> class Widget < AR::Base
>   def self.reloadable?() false; end
> end
>

Doh, sorry, I read that as 1.1.
rusty geldmacher (Guest)
on 2006-03-10 16:58
Rick O. wrote:
>> Try this on edge rails...
>>
>> class Widget < AR::Base
>>   def self.reloadable?() false; end
>> end
>>
>
> Doh, sorry, I read that as 1.1.

Hi Rick,

Thanks for the reply! Are you saying that this problem can be addressed
if I use Rails 1.1?

Thanks,
rusty
This topic is locked and can not be replied to.