如何在环境/ * .rb中解析配置符号

时间:2022-12-12 20:51:59

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

这是典型的config / environments / * .rb文件的开头方式:

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

The block passed to configure dereferences the symbol config which is apparently unbound. How does this technically work? The symbols used in a block/Proc/lambda should be bound in the context of its declaration, not left to be resolved in the dynamic scope at the call site.

传递给配置的块解除引用显然未绑定的符号配置。这在技术上如何工作?块/ Proc / lambda中使用的符号应该在其声明的上下文中绑定,而不是在调用站点的动态范围中解析。

A related question is, where exactly is the Application.configure method declared? It's not in either application.rb, engine.rb, or railtie.rb. Maybe if I managed to find this method, I would have found the answer to my main question.

一个相关的问题是,Application.configure方法究竟在哪里声明?它不在application.rb,engine.rb或railtie.rb中。也许如果我设法找到这个方法,我会找到我的主要问题的答案。

Also related, I have studied the Rails initialization procedure in excruciating detail, and I can't find even a mention of a config/environments/*.rb file. If I knew how these files were treated by the init procedure, that may shed some light on this.

还有相关的,我已经研究了Rails初始化过程中令人难以忍受的细节,我甚至找不到提到config / environments / * .rb文件。如果我知道init程序如何处理这些文件,那可能会对此有所了解。

1 个解决方案

#1


3  

It's a method config in Rails::Application in the railties gem in lib/rails/application.rb which returns an instance of Application::Configuration, defined in lib/rails/application/configuration.rb.

它是在lib / rails / application.rb中的railties gem中的Rails :: Application中的方法配置,它返回在lib / rails / application / configuration.rb中定义的Application :: Configuration实例。

The method configure is contributed to Railtie from the autoloaded module Configurable, lib/rails/railtie/configurable, and is defined as

配置方法由自动加载模块Configurable,lib / rails / railtie / configurable贡献给Railtie,并定义为

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

which explains why the block that configure accepts can resolve the config symbol. Note that class_eval is another piece of rubyist magic that makes this work: it rebinds the passed-in block's self symbol to the call site's class.

这解释了为什么配置接受的块可以解析配置符号。请注意,class_eval是另一个使用它的rubyist魔法:它将传入块的自我符号重新绑定到调用站点的类。

Check the comments in the first file in the Booting Process section, which explains where, how and in what order all this goodness comes from, including how the /config/environments directory is processed.

检查“引导过程”部分中第一个文件中的注释,该部分说明了所有这些优点的来源,方式和顺序,包括如何处理/ config / environments目录。

#1


3  

It's a method config in Rails::Application in the railties gem in lib/rails/application.rb which returns an instance of Application::Configuration, defined in lib/rails/application/configuration.rb.

它是在lib / rails / application.rb中的railties gem中的Rails :: Application中的方法配置,它返回在lib / rails / application / configuration.rb中定义的Application :: Configuration实例。

The method configure is contributed to Railtie from the autoloaded module Configurable, lib/rails/railtie/configurable, and is defined as

配置方法由自动加载模块Configurable,lib / rails / railtie / configurable贡献给Railtie,并定义为

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

which explains why the block that configure accepts can resolve the config symbol. Note that class_eval is another piece of rubyist magic that makes this work: it rebinds the passed-in block's self symbol to the call site's class.

这解释了为什么配置接受的块可以解析配置符号。请注意,class_eval是另一个使用它的rubyist魔法:它将传入块的自我符号重新绑定到调用站点的类。

Check the comments in the first file in the Booting Process section, which explains where, how and in what order all this goodness comes from, including how the /config/environments directory is processed.

检查“引导过程”部分中第一个文件中的注释,该部分说明了所有这些优点的来源,方式和顺序,包括如何处理/ config / environments目录。