How the configuration symbol is allowed in / * environments. Rb

This is how a typical config/environments/*.rb file starts:

 MyApp::Application.configure do config.cache_classes = false ... end 

The block passed to configure separates the config symbol, which is apparently unrelated. How does it technically work? The characters used in the / Proc / lambda block must be linked in the context of its declaration, and not left to be resolved in the dynamic area of ​​the call site.

Related question: where exactly is the Application.configure method declared? It is not in application.rb , engine.rb , or railtie.rb . Maybe if I can find this method, I would find the answer to my main question.

Also, I studied the Rails initialization procedure in agonizing detail, and I cannot even find the mention of the config/environments/*.rb file. If I knew how these files are processed by the init procedure, this may shed some light on this.

+7
source share
1 answer

This is the config method in Rails::Application in the railties column in lib/rails/application.rb that returns the instance of Application::Configuration defined in lib/rails/application/configuration.rb .

The configure method is introduced in Railtie from the autoload ed Configurable module, lib/rails/railtie/configurable and is defined as

 def configure(&block) class_eval(&block) end 

which explains why the block that configure accepts can resolve the config character. Note that class_eval is another piece of rubyist magic that does this job: it re-installs the symbol of the passed self block into the call site class.

Check out the comments in the first file under the “Download Process” section, which explains where, how, and in what order all this kindness happens, including how the /config/environments directory is handled.

+2
source

All Articles