aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/dependencies/autoload.rb
blob: a1626ebeba72f194a1b410d5ed9c5c766c7f77c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require "active_support/inflector/methods"
require "active_support/lazy_load_hooks"

module ActiveSupport
  module Autoload
    @@autoloads = {}
    @@under_path = nil
    @@at_path = nil
    @@eager_autoload = false

    def autoload(const_name, path = @@at_path)
      unless path
        full = [name, @@under_path, const_name.to_s, path].compact.join("::")
        path = Inflector.underscore(full)
      end

      if @@eager_autoload
        @@autoloads[const_name] = path
      end

      super const_name, path
    end

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

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

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

    def self.eager_autoload!
      @@autoloads.values.each { |file| require file }
    end

    def autoloads
      @@autoloads
    end
  end
end