Ruby on rails rails自动加载和初始化器

Ruby on rails rails自动加载和初始化器,ruby-on-rails,Ruby On Rails,我有一个带有基本配置模式和API连接方法的简单模块。我正在初始化器中配置此模块 服务/任务\u manager.rb: module TasksManager class << self attr_reader :client end def self.configuration @configuration ||= Configuration.new end def self.configure yield configuration

我有一个带有基本配置模式和API连接方法的简单模块。我正在初始化器中配置此模块

服务/任务\u manager.rb:

module TasksManager
  class << self
    attr_reader :client
  end

  def self.configuration
    @configuration ||= Configuration.new
  end

  def self.configure
    yield configuration
  end

  def self.connect
    @client ||= SecretAPI::Client.new do |config|
      config.url = configuration.url
      config.token = configuration.token
      config.username = configuration.username
      config.password = configuration.password
    end
    self
  end

#.
#.
# other stuff
#.
#.

  class Configuration
    attr_accessor :url
    attr_accessor :username
    attr_accessor :token
    attr_accessor :password
  end
end
当我启动rails应用程序时,一切正常,我可以通过不同的对象使用TasksManager,它使用的是在初始化器中设置的配置。但是

当我对services/tasks_manager.rb文件做一些小的更改时,比如注释一些内容或向其中添加新方法。我需要重新启动rails应用程序。TasksManager.configuration在此阶段为空。看起来对文件进行更改会强制创建新模块,并且未加载初始值设定项

这可能是一种正常的行为,但我花了一段时间才弄明白,我想也许有人能向我解释一下


我将rails 4.2与spring一起使用(这是为什么?)

您可以将初始化代码放入
ActionDispatch::Callbacks.to_prepare{}
块中。这将在rails重新加载类时对其进行评估

TasksManager.configure do |config|                                                                                                 
  config.url = "a..."                                                                                         
  config.username = "b..."                                                                               
  config.password = "c..."                                                                               
  config.token = "d..."                                                                                      
end