Active Support 自动加载
Autoload
和为您的库提供便捷的预加载功能。
此模块允许您根据 Rails 约定定义自动加载(即无需定义路径,它会根据文件名自动推断),并定义一组需要预加载的常量。
module MyLib
extend ActiveSupport::Autoload
autoload :Model
eager_autoload do
autoload :Cache
end
end
然后,您的库可以通过简单地调用以下方法进行预加载:
MyLib.eager_load!
方法
实例公共方法
autoload(const_name, path = @_at_path) 链接
源代码:显示 | 在 GitHub 上
# File activesupport/lib/active_support/dependencies/autoload.rb, line 42 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) 链接
源代码:显示 | 在 GitHub 上
# File activesupport/lib/active_support/dependencies/autoload.rb, line 63 def autoload_at(path) @_at_path, old_path = path, @_at_path yield ensure @_at_path = old_path end
autoload_under(path) 链接
源代码:显示 | 在 GitHub 上
# File activesupport/lib/active_support/dependencies/autoload.rb, line 56 def autoload_under(path) @_under_path, old_path = path, @_under_path yield ensure @_under_path = old_path end
eager_autoload() 链接
源代码:显示 | 在 GitHub 上
# File activesupport/lib/active_support/dependencies/autoload.rb, line 70 def eager_autoload old_eager, @_eager_autoload = @_eager_autoload, true yield ensure @_eager_autoload = old_eager end
eager_load!() 链接
源代码:显示 | 在 GitHub 上
# File activesupport/lib/active_support/dependencies/autoload.rb, line 77 def eager_load! if @_eagerloaded_constants @_eagerloaded_constants.each { |const_name| const_get(const_name) } @_eagerloaded_constants = nil end end