2009-12-02 23:01:01 -05:00
|
|
|
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
|
2009-12-02 23:01:01 -05:00
|
|
|
|
|
|
|
def autoload(const_name, path = @@at_path)
|
2012-03-12 02:18:03 -04:00
|
|
|
unless path
|
|
|
|
full = [name, @@under_path, const_name.to_s, path].compact.join("::")
|
|
|
|
path = Inflector.underscore(full)
|
|
|
|
end
|
2009-12-02 23:01:01 -05:00
|
|
|
|
2009-12-22 18:27:37 -05:00
|
|
|
if @@eager_autoload
|
2012-03-12 02:18:03 -04:00
|
|
|
@@autoloads[const_name] = path
|
2009-12-12 19:41:26 -05:00
|
|
|
end
|
2012-03-12 02:18:03 -04:00
|
|
|
|
|
|
|
super const_name, path
|
2009-12-02 23:01:01 -05:00
|
|
|
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
|
2009-12-12 19:41:26 -05:00
|
|
|
yield
|
|
|
|
ensure
|
2009-12-22 18:27:37 -05:00
|
|
|
@@eager_autoload = old_eager
|
2009-12-12 19:41:26 -05:00
|
|
|
end
|
|
|
|
|
2009-12-02 23:01:01 -05:00
|
|
|
def self.eager_autoload!
|
2009-12-12 19:41:26 -05:00
|
|
|
@@autoloads.values.each { |file| require file }
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
|
|
|
|
2009-12-12 19:41:26 -05:00
|
|
|
def autoloads
|
|
|
|
@@autoloads
|
|
|
|
end
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
2009-12-12 19:41:26 -05:00
|
|
|
end
|