From a1687e48cba2f0447f02248939cde05502562e87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 1 Aug 2012 18:23:11 +0200 Subject: Make ActiveSupport::Autoload local Previously, ActiveSupport::Autoload was global and reserved for usage inside Rails. This pull request makes it local, fixes its test (they were not being run because its file was named wrongly) and make it part of Rails public API. --- activesupport/lib/active_support.rb | 3 +- .../lib/active_support/dependencies/autoload.rb | 60 ++++++++++++++++------ 2 files changed, 44 insertions(+), 19 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index 56d6676961..41d77ab6c1 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -37,7 +37,6 @@ module ActiveSupport autoload :LogSubscriber autoload :Notifications - # TODO: Narrow this list down eager_autoload do autoload :BacktraceCleaner autoload :BasicObject @@ -55,12 +54,12 @@ module ActiveSupport autoload :OptionMerger autoload :OrderedHash autoload :OrderedOptions - autoload :Rescuable autoload :StringInquirer autoload :TaggedLogging autoload :XmlMini end + autoload :Rescuable autoload :SafeBuffer, "active_support/core_ext/string/output_safety" autoload :TestCase end diff --git a/activesupport/lib/active_support/dependencies/autoload.rb b/activesupport/lib/active_support/dependencies/autoload.rb index 4045db3232..df490ae298 100644 --- a/activesupport/lib/active_support/dependencies/autoload.rb +++ b/activesupport/lib/active_support/dependencies/autoload.rb @@ -1,52 +1,78 @@ require "active_support/inflector/methods" module ActiveSupport + # 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! + # module Autoload - @@autoloads = {} - @@under_path = nil - @@at_path = nil - @@eager_autoload = false + def self.extended(base) + base.class_eval do + @_autoloads = {} + @_under_path = nil + @_at_path = nil + @_eager_autoload = false + end + end - def autoload(const_name, path = @@at_path) + def autoload(const_name, path = @_at_path) unless path - full = [name, @@under_path, const_name.to_s, path].compact.join("::") + full = [name, @_under_path, const_name.to_s, path].compact.join("::") path = Inflector.underscore(full) end - if @@eager_autoload - @@autoloads[const_name] = path + if @_eager_autoload + @_autoloads[const_name] = path end super const_name, path end def autoload_under(path) - @@under_path, old_path = path, @@under_path + @_under_path, old_path = path, @_under_path yield ensure - @@under_path = old_path + @_under_path = old_path end def autoload_at(path) - @@at_path, old_path = path, @@at_path + @_at_path, old_path = path, @_at_path yield ensure - @@at_path = old_path + @_at_path = old_path end def eager_autoload - old_eager, @@eager_autoload = @@eager_autoload, true + old_eager, @_eager_autoload = @_eager_autoload, true yield ensure - @@eager_autoload = old_eager + @_eager_autoload = old_eager end - def self.eager_autoload! - @@autoloads.values.each { |file| require file } + def eager_load! + @_autoloads.values.each { |file| require file } end def autoloads - @@autoloads + @_autoloads end end end -- cgit v1.2.3 From 2801786e1a51b7cf7d7c3fd72b5fc9974f83f435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 1 Aug 2012 20:54:22 +0200 Subject: Get rid of config.preload_frameworks in favor of config.eager_load_namespaces The new option allows any Ruby namespace to be registered and set up for eager load. We are effectively exposing the structure existing in Rails since v3.0 for all developers in order to make their applications thread-safe and CoW friendly. --- activesupport/lib/active_support/railtie.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/railtie.rb b/activesupport/lib/active_support/railtie.rb index 30ac881090..aa8d408da9 100644 --- a/activesupport/lib/active_support/railtie.rb +++ b/activesupport/lib/active_support/railtie.rb @@ -5,6 +5,8 @@ module ActiveSupport class Railtie < Rails::Railtie config.active_support = ActiveSupport::OrderedOptions.new + config.eager_load_namespaces << ActiveSupport + initializer "active_support.deprecation_behavior" do |app| if deprecation = app.config.active_support.deprecation ActiveSupport::Deprecation.behavior = deprecation -- cgit v1.2.3