module ActiveSupport::Autoload

Autoload and eager load conveniences for your library.

This module allows you to define autoloads based on Rails conventions (i.e. no need to define the path it is automatically guessed based on the filename) and also define a set of constants that needs to be eager loaded:

module MyLib
  extend ActiveSupport::Autoload

  autoload :Model

  eager_autoload do
    autoload :Cache
  end
end

Then your library can be eager loaded by simply calling:

MyLib.eager_load!

Public Instance Methods

autoload(const_name, path = @_at_path) Show source
# File activesupport/lib/active_support/dependencies/autoload.rb, line 37
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
    @_autoloads[const_name] = path
  end

  super const_name, path
end
Calls superclass method
autoload_at(path) { || ... } Show source
# File activesupport/lib/active_support/dependencies/autoload.rb, line 57
def autoload_at(path)
  @_at_path, old_path = path, @_at_path
  yield
ensure
  @_at_path = old_path
end
autoload_under(path) { || ... } Show source
# File activesupport/lib/active_support/dependencies/autoload.rb, line 50
def autoload_under(path)
  @_under_path, old_path = path, @_under_path
  yield
ensure
  @_under_path = old_path
end
autoloads() Show source
# File activesupport/lib/active_support/dependencies/autoload.rb, line 75
def autoloads
  @_autoloads
end
eager_autoload() { || ... } Show source
# File activesupport/lib/active_support/dependencies/autoload.rb, line 64
def eager_autoload
  old_eager, @_eager_autoload = @_eager_autoload, true
  yield
ensure
  @_eager_autoload = old_eager
end
eager_load!() Show source
# File activesupport/lib/active_support/dependencies/autoload.rb, line 71
def eager_load!
  @_autoloads.each_value { |file| require file }
end

© 2004–2019 David Heinemeier Hansson
Licensed under the MIT License.