The module receives a reload of each request, so the lost source data is lost

I store the value in a class variable inside the module, for example:

module TranslationEnhancer def self.install! klass @dictionaries ||= [] << klass end ... end 

I call this from the initializer in config / initializers:

 require Rails.root + "lib" + "translation_enhancer.rb" TranslationEnhancer::install! TranslationDictionary 

Now, if I start the server in the development environment, everything is fine during the first request. However, after this request, @ dictionaries are suddenly gone. I commented on all the other code in TranslationEnhancer, so I am absolutely sure that the whole module should be reloaded every time I make a request.

I tried to move the module outside of the lib directory (moved it to lib_unloadable), and then tried:

 ActiveSupport::Dependencies.explicitly_unloadable_constants << "TranslationEnhancer" 

but again failed. I do not know how to solve this, please help.

Got Ruby 1.9.2 @Rails 3.1.rc4.

EDIT: I know that I could set dictionaries as a constant. But I would like to use TranslationEnhancer as a library, so I could use it unchanged in another project and install different directories, for example:

 TranslationEnhancer.install! EnglishDirectory, FrenchDirectory 

These values ​​will change at runtime; they simply change the project to project.

+6
source share
2 answers

Solved!

I realized that all application.rb and environment.rb files are reloaded along with all other files. The only thing that does not restart is the initializers (config / initializers / *). The solution was to move the initialization to application.rb.

+5
source

@dictionaries is not a "class variable". This is a "class instance variable". See here for a better explanation: Class and Instance Variables

Try using the dictionaries @@.

0
source

Source: https://habr.com/ru/post/893896/


All Articles