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
wycats 4aded43b73 Replace the placeholder base_hook API with on_load. To specify some code that
should run during framework load do:

ActiveSupport.on_load(:action_controller) do
  # Code run in the context of AC::Base
end
2010-03-29 17:08:50 -07:00

50 lines
1.1 KiB
Ruby

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)
full = [self.name, @@under_path, const_name.to_s, path].compact.join("::")
location = path || Inflector.underscore(full)
if @@eager_autoload
@@autoloads[const_name] = location
end
super const_name, location
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