Module: ActiveSupport::Autoload

Included in:
AbstractController, ActionController, ActionController::Caching, ActionDispatch, ActionDispatch::Http, ActionDispatch::Routing, ActionMailer, ActionView, ActionView::Helpers, ActionView::Helpers::Tags, ActionView::Template, ActiveModel, ActiveModel::Serializers, ActiveRecord, ActiveRecord::Associations, ActiveRecord::Associations::Preloader, ActiveRecord::AttributeMethods, ActiveRecord::ConnectionAdapters, ActiveRecord::Locking, ActiveRecord::Scoping, ActiveRecord::Tasks, ActiveSupport, NumberHelper, HTML, Rails
Defined in:
activesupport/lib/active_support/dependencies/autoload.rb

Overview

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!

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) extended(base)

:nodoc:



26
27
28
29
30
31
32
33
# File 'activesupport/lib/active_support/dependencies/autoload.rb', line 26

def self.extended(base) # :nodoc:
  base.class_eval do
    @_autoloads = {}
    @_under_path = nil
    @_at_path = nil
    @_eager_autoload = false
  end
end

Instance Method Details

- (Object) autoload(const_name, path = @_at_path)



35
36
37
38
39
40
41
42
43
44
45
46
# File 'activesupport/lib/active_support/dependencies/autoload.rb', line 35

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

- (Object) autoload_at(path)



55
56
57
58
59
60
# File 'activesupport/lib/active_support/dependencies/autoload.rb', line 55

def autoload_at(path)
  @_at_path, old_path = path, @_at_path
  yield
ensure
  @_at_path = old_path
end

- (Object) autoload_under(path)



48
49
50
51
52
53
# File 'activesupport/lib/active_support/dependencies/autoload.rb', line 48

def autoload_under(path)
  @_under_path, old_path = path, @_under_path
  yield
ensure
  @_under_path = old_path
end

- (Object) autoloads



73
74
75
# File 'activesupport/lib/active_support/dependencies/autoload.rb', line 73

def autoloads
  @_autoloads
end

- (Object) eager_autoload



62
63
64
65
66
67
# File 'activesupport/lib/active_support/dependencies/autoload.rb', line 62

def eager_autoload
  old_eager, @_eager_autoload = @_eager_autoload, true
  yield
ensure
  @_eager_autoload = old_eager
end

- (Object) eager_load!



69
70
71
# File 'activesupport/lib/active_support/dependencies/autoload.rb', line 69

def eager_load!
  @_autoloads.values.each { |file| require file }
end