跳到內文 跳到搜尋

Active Support 自動載入

自動載入和急切載入便利功能。

此模組允許您根據 Rails 慣例定義自動載入(即,無需定義路徑,因為路徑是根據檔案名稱自動猜測的),而且它也能夠定義一組需要急切載入的常數

module MyLib
  extend ActiveSupport::Autoload

  autoload :Model

  eager_autoload do
    autoload :Cache
  end
end

然後,只要呼叫,你的程式庫就可以急切載入了

MyLib.eager_load!
方法
A
E

實體公開方法

autoload(const_name, path = @_at_path)

# File activesupport/lib/active_support/dependencies/autoload.rb, line 30
def autoload(const_name, path = @_at_path)
  unless path
    full = [name, @_under_path, const_name.to_s].compact.join("::")
    path = Inflector.underscore(full)
  end

  if @_eager_autoload
    @_eagerloaded_constants ||= []
    @_eagerloaded_constants << const_name
  end

  super const_name, path
end

autoload_at(path)

# File activesupport/lib/active_support/dependencies/autoload.rb, line 51
def autoload_at(path)
  @_at_path, old_path = path, @_at_path
  yield
ensure
  @_at_path = old_path
end

autoload_under(path)

# File activesupport/lib/active_support/dependencies/autoload.rb, line 44
def autoload_under(path)
  @_under_path, old_path = path, @_under_path
  yield
ensure
  @_under_path = old_path
end

eager_autoload()

# File activesupport/lib/active_support/dependencies/autoload.rb, line 58
def eager_autoload
  old_eager, @_eager_autoload = @_eager_autoload, true
  yield
ensure
  @_eager_autoload = old_eager
end

eager_load!()

# File activesupport/lib/active_support/dependencies/autoload.rb, line 65
def eager_load!
  if @_eagerloaded_constants
    @_eagerloaded_constants.each { |const_name| const_get(const_name) }
    @_eagerloaded_constants = nil
  end
end