1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

renames load_(once_)paths to autoload_(once_)paths in dependencies and config

This commit is contained in:
Xavier Noria 2010-06-22 23:17:20 +02:00
parent 4a0a640d33
commit 6f83a5036d
10 changed files with 59 additions and 54 deletions

View file

@ -1,5 +1,7 @@
*Rails 3.0.0 [Release Candidate] (unreleased)* *Rails 3.0.0 [Release Candidate] (unreleased)*
* Renamed ActiveSupport::Dependecies.load_(once_)paths to autoload_(once_)paths. [fxn]
* Added ActiveSupport::FileUpdateChecker to execute a block only if a set of files changed, used by Router and I18n locale files. [José Valim] * Added ActiveSupport::FileUpdateChecker to execute a block only if a set of files changed, used by Router and I18n locale files. [José Valim]
* Added ActiveSupport::DescendantsTracker to track descendants with support to constants reloading. [José Valim] * Added ActiveSupport::DescendantsTracker to track descendants with support to constants reloading. [José Valim]

View file

@ -33,14 +33,14 @@ module ActiveSupport #:nodoc:
# The set of directories from which we may automatically load files. Files # The set of directories from which we may automatically load files. Files
# under these directories will be reloaded on each request in development mode, # under these directories will be reloaded on each request in development mode,
# unless the directory also appears in load_once_paths. # unless the directory also appears in autoload_once_paths.
mattr_accessor :load_paths mattr_accessor :autoload_paths
self.load_paths = [] self.autoload_paths = []
# The set of directories from which automatically loaded constants are loaded # The set of directories from which automatically loaded constants are loaded
# only once. All directories in this set must also be present in +load_paths+. # only once. All directories in this set must also be present in +autoload_paths+.
mattr_accessor :load_once_paths mattr_accessor :autoload_once_paths
self.load_once_paths = [] self.autoload_once_paths = []
# An array of qualified constant names that have been loaded. Adding a name to # An array of qualified constant names that have been loaded. Adding a name to
# this array will cause it to be unloaded the next time Dependencies are cleared. # this array will cause it to be unloaded the next time Dependencies are cleared.
@ -352,7 +352,7 @@ module ActiveSupport #:nodoc:
# Given +path+, a filesystem path to a ruby file, return an array of constant # Given +path+, a filesystem path to a ruby file, return an array of constant
# paths which would cause Dependencies to attempt to load this file. # paths which would cause Dependencies to attempt to load this file.
def loadable_constants_for_path(path, bases = load_paths) def loadable_constants_for_path(path, bases = autoload_paths)
expanded_path = Pathname.new(path[/\A(.*?)(\.rb)?\Z/, 1]).expand_path expanded_path = Pathname.new(path[/\A(.*?)(\.rb)?\Z/, 1]).expand_path
bases.inject([]) do |paths, root| bases.inject([]) do |paths, root|
@ -363,11 +363,11 @@ module ActiveSupport #:nodoc:
end.uniq end.uniq
end end
# Search for a file in load_paths matching the provided suffix. # Search for a file in autoload_paths matching the provided suffix.
def search_for_file(path_suffix) def search_for_file(path_suffix)
path_suffix = path_suffix.sub(/(\.rb)?$/, ".rb") path_suffix = path_suffix.sub(/(\.rb)?$/, ".rb")
load_paths.each do |root| autoload_paths.each do |root|
path = File.join(root, path_suffix) path = File.join(root, path_suffix)
return path if File.file? path return path if File.file? path
end end
@ -377,14 +377,14 @@ module ActiveSupport #:nodoc:
# Does the provided path_suffix correspond to an autoloadable module? # Does the provided path_suffix correspond to an autoloadable module?
# Instead of returning a boolean, the autoload base for this module is returned. # Instead of returning a boolean, the autoload base for this module is returned.
def autoloadable_module?(path_suffix) def autoloadable_module?(path_suffix)
load_paths.each do |load_path| autoload_paths.each do |load_path|
return load_path if File.directory? File.join(load_path, path_suffix) return load_path if File.directory? File.join(load_path, path_suffix)
end end
nil nil
end end
def load_once_path?(path) def load_once_path?(path)
load_once_paths.any? { |base| path.starts_with? base } autoload_once_paths.any? { |base| path.starts_with? base }
end end
# Attempt to autoload the provided module name by searching for a directory # Attempt to autoload the provided module name by searching for a directory
@ -396,7 +396,7 @@ module ActiveSupport #:nodoc:
return nil unless base_path = autoloadable_module?(path_suffix) return nil unless base_path = autoloadable_module?(path_suffix)
mod = Module.new mod = Module.new
into.const_set const_name, mod into.const_set const_name, mod
autoloaded_constants << qualified_name unless load_once_paths.include?(base_path) autoloaded_constants << qualified_name unless autoload_once_paths.include?(base_path)
return mod return mod
end end

View file

@ -25,11 +25,11 @@ class DependenciesTest < Test::Unit::TestCase
this_dir = File.dirname(__FILE__) this_dir = File.dirname(__FILE__)
parent_dir = File.dirname(this_dir) parent_dir = File.dirname(this_dir)
$LOAD_PATH.unshift(parent_dir) unless $LOAD_PATH.include?(parent_dir) $LOAD_PATH.unshift(parent_dir) unless $LOAD_PATH.include?(parent_dir)
prior_load_paths = ActiveSupport::Dependencies.load_paths prior_autoload_paths = ActiveSupport::Dependencies.autoload_paths
ActiveSupport::Dependencies.load_paths = from.collect { |f| "#{this_dir}/#{f}" } ActiveSupport::Dependencies.autoload_paths = from.collect { |f| "#{this_dir}/#{f}" }
yield yield
ensure ensure
ActiveSupport::Dependencies.load_paths = prior_load_paths ActiveSupport::Dependencies.autoload_paths = prior_autoload_paths
ActiveSupport::Dependencies.mechanism = old_mechanism ActiveSupport::Dependencies.mechanism = old_mechanism
ActiveSupport::Dependencies.explicitly_unloadable_constants = [] ActiveSupport::Dependencies.explicitly_unloadable_constants = []
end end
@ -264,7 +264,7 @@ class DependenciesTest < Test::Unit::TestCase
def test_loadable_constants_for_path_should_provide_all_results def test_loadable_constants_for_path_should_provide_all_results
fake_root = '/usr/apps/backpack' fake_root = '/usr/apps/backpack'
with_loading fake_root, fake_root + '/lib' do with_loading fake_root, fake_root + '/lib' do
root = ActiveSupport::Dependencies.load_paths.first root = ActiveSupport::Dependencies.autoload_paths.first
assert_equal ["Lib::A::B", "A::B"], ActiveSupport::Dependencies.loadable_constants_for_path(root + '/lib/a/b') assert_equal ["Lib::A::B", "A::B"], ActiveSupport::Dependencies.loadable_constants_for_path(root + '/lib/a/b')
end end
end end
@ -272,7 +272,7 @@ class DependenciesTest < Test::Unit::TestCase
def test_loadable_constants_for_path_should_uniq_results def test_loadable_constants_for_path_should_uniq_results
fake_root = '/usr/apps/backpack/lib' fake_root = '/usr/apps/backpack/lib'
with_loading fake_root, fake_root + '/' do with_loading fake_root, fake_root + '/' do
root = ActiveSupport::Dependencies.load_paths.first root = ActiveSupport::Dependencies.autoload_paths.first
assert_equal ["A::B"], ActiveSupport::Dependencies.loadable_constants_for_path(root + '/a/b') assert_equal ["A::B"], ActiveSupport::Dependencies.loadable_constants_for_path(root + '/a/b')
end end
end end
@ -344,7 +344,7 @@ class DependenciesTest < Test::Unit::TestCase
def test_file_search def test_file_search
with_loading 'dependencies' do with_loading 'dependencies' do
root = ActiveSupport::Dependencies.load_paths.first root = ActiveSupport::Dependencies.autoload_paths.first
assert_equal nil, ActiveSupport::Dependencies.search_for_file('service_three') assert_equal nil, ActiveSupport::Dependencies.search_for_file('service_three')
assert_equal nil, ActiveSupport::Dependencies.search_for_file('service_three.rb') assert_equal nil, ActiveSupport::Dependencies.search_for_file('service_three.rb')
assert_equal root + '/service_one.rb', ActiveSupport::Dependencies.search_for_file('service_one') assert_equal root + '/service_one.rb', ActiveSupport::Dependencies.search_for_file('service_one')
@ -354,14 +354,14 @@ class DependenciesTest < Test::Unit::TestCase
def test_file_search_uses_first_in_load_path def test_file_search_uses_first_in_load_path
with_loading 'dependencies', 'autoloading_fixtures' do with_loading 'dependencies', 'autoloading_fixtures' do
deps, autoload = ActiveSupport::Dependencies.load_paths deps, autoload = ActiveSupport::Dependencies.autoload_paths
assert_match %r/dependencies/, deps assert_match %r/dependencies/, deps
assert_match %r/autoloading_fixtures/, autoload assert_match %r/autoloading_fixtures/, autoload
assert_equal deps + '/conflict.rb', ActiveSupport::Dependencies.search_for_file('conflict') assert_equal deps + '/conflict.rb', ActiveSupport::Dependencies.search_for_file('conflict')
end end
with_loading 'autoloading_fixtures', 'dependencies' do with_loading 'autoloading_fixtures', 'dependencies' do
autoload, deps = ActiveSupport::Dependencies.load_paths autoload, deps = ActiveSupport::Dependencies.autoload_paths
assert_match %r/dependencies/, deps assert_match %r/dependencies/, deps
assert_match %r/autoloading_fixtures/, autoload assert_match %r/autoloading_fixtures/, autoload
@ -418,7 +418,7 @@ class DependenciesTest < Test::Unit::TestCase
def test_removal_from_tree_should_be_detected def test_removal_from_tree_should_be_detected
with_loading 'dependencies' do with_loading 'dependencies' do
root = ActiveSupport::Dependencies.load_paths.first root = ActiveSupport::Dependencies.autoload_paths.first
c = ServiceOne c = ServiceOne
ActiveSupport::Dependencies.clear ActiveSupport::Dependencies.clear
assert ! defined?(ServiceOne) assert ! defined?(ServiceOne)
@ -433,7 +433,7 @@ class DependenciesTest < Test::Unit::TestCase
def test_references_should_work def test_references_should_work
with_loading 'dependencies' do with_loading 'dependencies' do
root = ActiveSupport::Dependencies.load_paths.first root = ActiveSupport::Dependencies.autoload_paths.first
c = ActiveSupport::Dependencies.ref("ServiceOne") c = ActiveSupport::Dependencies.ref("ServiceOne")
service_one_first = ServiceOne service_one_first = ServiceOne
assert_equal service_one_first, c.get assert_equal service_one_first, c.get
@ -460,9 +460,9 @@ class DependenciesTest < Test::Unit::TestCase
end end
end end
def test_load_once_paths_do_not_add_to_autoloaded_constants def test_autoload_once_paths_do_not_add_to_autoloaded_constants
with_autoloading_fixtures do with_autoloading_fixtures do
ActiveSupport::Dependencies.load_once_paths = ActiveSupport::Dependencies.load_paths.dup ActiveSupport::Dependencies.autoload_once_paths = ActiveSupport::Dependencies.autoload_paths.dup
assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder") assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder")
assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder::NestedClass") assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder::NestedClass")
@ -473,7 +473,7 @@ class DependenciesTest < Test::Unit::TestCase
end end
ensure ensure
Object.class_eval { remove_const :ModuleFolder } Object.class_eval { remove_const :ModuleFolder }
ActiveSupport::Dependencies.load_once_paths = [] ActiveSupport::Dependencies.autoload_once_paths = []
end end
def test_application_should_special_case_application_controller def test_application_should_special_case_application_controller
@ -760,20 +760,20 @@ class DependenciesTest < Test::Unit::TestCase
def test_load_once_constants_should_not_be_unloaded def test_load_once_constants_should_not_be_unloaded
with_autoloading_fixtures do with_autoloading_fixtures do
ActiveSupport::Dependencies.load_once_paths = ActiveSupport::Dependencies.load_paths ActiveSupport::Dependencies.autoload_once_paths = ActiveSupport::Dependencies.autoload_paths
::A.to_s ::A.to_s
assert defined?(A) assert defined?(A)
ActiveSupport::Dependencies.clear ActiveSupport::Dependencies.clear
assert defined?(A) assert defined?(A)
end end
ensure ensure
ActiveSupport::Dependencies.load_once_paths = [] ActiveSupport::Dependencies.autoload_once_paths = []
Object.class_eval { remove_const :A if const_defined?(:A) } Object.class_eval { remove_const :A if const_defined?(:A) }
end end
def test_load_once_paths_should_behave_when_recursively_loading def test_autoload_once_paths_should_behave_when_recursively_loading
with_loading 'dependencies', 'autoloading_fixtures' do with_loading 'dependencies', 'autoloading_fixtures' do
ActiveSupport::Dependencies.load_once_paths = [ActiveSupport::Dependencies.load_paths.last] ActiveSupport::Dependencies.autoload_once_paths = [ActiveSupport::Dependencies.autoload_paths.last]
assert !defined?(CrossSiteDependency) assert !defined?(CrossSiteDependency)
assert_nothing_raised { CrossSiteDepender.nil? } assert_nothing_raised { CrossSiteDepender.nil? }
assert defined?(CrossSiteDependency) assert defined?(CrossSiteDependency)
@ -784,7 +784,7 @@ class DependenciesTest < Test::Unit::TestCase
"CrossSiteDependency shouldn't have been unloaded!" "CrossSiteDependency shouldn't have been unloaded!"
end end
ensure ensure
ActiveSupport::Dependencies.load_once_paths = [] ActiveSupport::Dependencies.autoload_once_paths = []
end end
def test_hook_called_multiple_times def test_hook_called_multiple_times

View file

@ -1,6 +1,9 @@
*Rails 3.0.0 [Release Candidate] (unreleased)* *Rails 3.0.0 [Release Candidate] (unreleased)*
* config.load_(once_)paths in config/application.rb got renamed to config.autoload_(once_)paths. [fxn]
* Abort generation/booting on Ruby 1.9.1. [fxn] * Abort generation/booting on Ruby 1.9.1. [fxn]
* Made the rails command work even when you're in a subdirectory [Chad Fowler] * Made the rails command work even when you're in a subdirectory [Chad Fowler]

View file

@ -67,7 +67,7 @@ module Rails
raise "You cannot have more than one Rails::Application" if Rails.application raise "You cannot have more than one Rails::Application" if Rails.application
super super
Rails.application = base.instance Rails.application = base.instance
Rails.application.add_lib_to_load_paths! Rails.application.add_lib_to_load_path!
ActiveSupport.run_load_hooks(:before_configuration, base.instance) ActiveSupport.run_load_hooks(:before_configuration, base.instance)
end end
@ -97,7 +97,7 @@ module Rails
# are changing config.root inside your application definition or having a custom # are changing config.root inside your application definition or having a custom
# Rails application, you will need to add lib to $LOAD_PATH on your own in case # Rails application, you will need to add lib to $LOAD_PATH on your own in case
# you need to load files in lib/ during the application configuration as well. # you need to load files in lib/ during the application configuration as well.
def add_lib_to_load_paths! #:nodoc: def add_lib_to_load_path! #:nodoc:
path = config.root.join('lib').to_s path = config.root.join('lib').to_s
$LOAD_PATH.unshift(path) if File.exists?(path) $LOAD_PATH.unshift(path) if File.exists?(path)
end end

View file

@ -7,14 +7,14 @@ module Rails
config.generators.templates.unshift(*paths.lib.templates.to_a) config.generators.templates.unshift(*paths.lib.templates.to_a)
end end
initializer :ensure_load_once_paths_as_subset do initializer :ensure_autoload_once_paths_as_subset do
extra = ActiveSupport::Dependencies.load_once_paths - extra = ActiveSupport::Dependencies.autoload_once_paths -
ActiveSupport::Dependencies.load_paths ActiveSupport::Dependencies.autoload_paths
unless extra.empty? unless extra.empty?
abort <<-end_error abort <<-end_error
load_once_paths must be a subset of the load_paths. autoload_once_paths must be a subset of the autoload_paths.
Extra items in load_once_paths: #{extra * ','} Extra items in autoload_once_paths: #{extra * ','}
end_error end_error
end end
end end

View file

@ -32,14 +32,14 @@ module Rails
# == Configuration # == Configuration
# #
# Besides the Railtie configuration which is shared across the application, in a # Besides the Railtie configuration which is shared across the application, in a
# Rails::Engine you can access load_paths, eager_load_paths and load_once_paths, # Rails::Engine you can access autoload_paths, eager_load_paths and autoload_once_paths,
# which differently from a Railtie, are scoped to the current Engine. # which differently from a Railtie, are scoped to the current Engine.
# #
# Example: # Example:
# #
# class MyEngine < Rails::Engine # class MyEngine < Rails::Engine
# # Add a load path for this specific Engine # # Add a load path for this specific Engine
# config.load_paths << File.expand_path("../lib/some/path", __FILE__) # config.autoload_paths << File.expand_path("../lib/some/path", __FILE__)
# #
# initializer "my_engine.add_middleware" do |app| # initializer "my_engine.add_middleware" do |app|
# app.middleware.use MyEngine::Middleware # app.middleware.use MyEngine::Middleware
@ -142,7 +142,7 @@ module Rails
# Add configured load paths to ruby load paths and remove duplicates. # Add configured load paths to ruby load paths and remove duplicates.
initializer :set_load_path, :before => :bootstrap_hook do initializer :set_load_path, :before => :bootstrap_hook do
config.load_paths.reverse_each do |path| config.autoload_paths.reverse_each do |path|
$LOAD_PATH.unshift(path) if File.directory?(path) $LOAD_PATH.unshift(path) if File.directory?(path)
end end
$LOAD_PATH.uniq! $LOAD_PATH.uniq!
@ -154,17 +154,17 @@ module Rails
# This needs to be an initializer, since it needs to run once # This needs to be an initializer, since it needs to run once
# per engine and get the engine as a block parameter # per engine and get the engine as a block parameter
initializer :set_autoload_paths, :before => :bootstrap_hook do |app| initializer :set_autoload_paths, :before => :bootstrap_hook do |app|
ActiveSupport::Dependencies.load_paths.unshift(*config.load_paths) ActiveSupport::Dependencies.autoload_paths.unshift(*config.autoload_paths)
if reloadable?(app) if reloadable?(app)
ActiveSupport::Dependencies.load_once_paths.unshift(*config.load_once_paths) ActiveSupport::Dependencies.autoload_once_paths.unshift(*config.autoload_once_paths)
else else
ActiveSupport::Dependencies.load_once_paths.unshift(*config.load_paths) ActiveSupport::Dependencies.autoload_once_paths.unshift(*config.autoload_paths)
end end
# Freeze so future modifications will fail rather than do nothing mysteriously # Freeze so future modifications will fail rather than do nothing mysteriously
config.load_paths.freeze config.autoload_paths.freeze
config.load_once_paths.freeze config.autoload_once_paths.freeze
end end
initializer :add_routing_paths do |app| initializer :add_routing_paths do |app|

View file

@ -4,7 +4,7 @@ module Rails
class Engine class Engine
class Configuration < ::Rails::Railtie::Configuration class Configuration < ::Rails::Railtie::Configuration
attr_reader :root attr_reader :root
attr_writer :eager_load_paths, :load_once_paths, :load_paths attr_writer :eager_load_paths, :autoload_once_paths, :autoload_paths
def initialize(root=nil) def initialize(root=nil)
super() super()
@ -41,12 +41,12 @@ module Rails
@eager_load_paths ||= paths.eager_load @eager_load_paths ||= paths.eager_load
end end
def load_once_paths def autoload_once_paths
@load_once_paths ||= paths.load_once @autoload_once_paths ||= paths.load_once
end end
def load_paths def autoload_paths
@load_paths ||= paths.load_paths @autoload_paths ||= paths.load_paths
end end
end end
end end

View file

@ -22,7 +22,7 @@ module <%= app_const_base %>
# -- all .rb files in that directory are automatically loaded. # -- all .rb files in that directory are automatically loaded.
# Add additional load paths for your own custom dirs # Add additional load paths for your own custom dirs
# config.load_paths += %W( #{config.root}/extras ) # config.autoload_paths += %W( #{config.root}/extras )
# Only load the plugins named here, in the order given (default is alphabetical). # Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named # :all can be used as a placeholder for all plugins not explicitly named

View file

@ -130,9 +130,9 @@ class ActionsTest < Rails::Generators::TestCase
def test_environment_should_include_data_in_environment_initializer_block def test_environment_should_include_data_in_environment_initializer_block
run_generator run_generator
load_paths = 'config.load_paths += %w["#{Rails.root}/app/extras"]' autoload_paths = 'config.autoload_paths += %w["#{Rails.root}/app/extras"]'
action :environment, load_paths action :environment, autoload_paths
assert_file 'config/application.rb', /#{Regexp.escape(load_paths)}/ assert_file 'config/application.rb', /#{Regexp.escape(autoload_paths)}/
end end
def test_environment_with_block_should_include_block_contents_in_environment_initializer_block def test_environment_with_block_should_include_block_contents_in_environment_initializer_block