1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activesupport/lib/active_support/dependencies/autoload.rb

53 lines
1 KiB
Ruby
Raw Normal View History

require "active_support/inflector/methods"
module ActiveSupport
module Autoload
@@autoloads = {}
@@under_path = nil
@@at_path = nil
2009-12-22 18:27:37 -05:00
@@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
2009-12-22 18:27:37 -05:00
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
2009-12-22 18:27:37 -05:00
def eager_autoload
old_eager, @@eager_autoload = @@eager_autoload, true
yield
ensure
2009-12-22 18:27:37 -05:00
@@eager_autoload = old_eager
end
def self.eager_autoload!
@@autoloads.values.each { |file| require file }
end
def autoloads
@@autoloads
end
end
end