diff options
author | Nicholas Seckar <nseckar@gmail.com> | 2006-02-03 20:29:39 +0000 |
---|---|---|
committer | Nicholas Seckar <nseckar@gmail.com> | 2006-02-03 20:29:39 +0000 |
commit | ad9cabd77c26cf58c7179a19caa9efb7e5e0c831 (patch) | |
tree | 4a0269477045a701e55542f4a370525f312e4f47 /activesupport | |
parent | 05eb14d55a2e336b82a57b57c83fad2b64c9b0f5 (diff) | |
download | rails-ad9cabd77c26cf58c7179a19caa9efb7e5e0c831.tar.gz rails-ad9cabd77c26cf58c7179a19caa9efb7e5e0c831.tar.bz2 rails-ad9cabd77c26cf58c7179a19caa9efb7e5e0c831.zip |
Remove LoadingModule
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3526 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
9 files changed, 2 insertions, 268 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index bd001f3196..89ec622abc 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Remove LoadingModule. [Nicholas Seckar] + * Add documentation for Reloadable::OnlySubclasses. [Nicholas Seckar] * Add Reloadable::OnlySubclasses which handles the common case where a base class should not be reloaded, but its subclasses should be. [Nicholas Seckar] diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 0bedbf093c..6d5adb6605 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -68,136 +68,6 @@ module Dependencies #:nodoc: # Record history *after* loading so first load gets warnings. history << file_name end - - # LoadingModules implement namespace-safe dynamic loading. - # They support automatic loading via const_missing, allowing contained items to be automatically - # loaded when required. No extra syntax is required, as expressions such as Controller::Admin::UserController - # load the relavent files automatically. - # - # Ruby-style modules are supported, as a folder named 'submodule' will load 'submodule.rb' when available. - class LoadingModule < Module #:nodoc: - attr_reader :path - attr_reader :root - - class << self - def root(*load_paths) - RootLoadingModule.new(*load_paths) - end - end - - def initialize(root, path=[]) - @path = path.clone.freeze - @root = root - end - - def root?() self.root == self end - def load_paths() self.root.load_paths end - - # Load missing constants if possible. - def const_missing(name) - const_load!(name) ? const_get(name) : super(name) - end - - # Load the controller class or a parent module. - def const_load!(name, file_name = nil) - file_name ||= 'application' if root? && name.to_s == 'ApplicationController' - path = self.path + [file_name || name] - - load_paths.each do |load_path| - fs_path = load_path.filesystem_path(path) - next unless fs_path - - case - when File.directory?(fs_path) - new_module = LoadingModule.new(self.root, self.path + [name]) - self.const_set name, new_module - if self.root? - if Object.const_defined?(name) - msg = "Cannot load module #{name}: Object::#{name} is set to #{Object.const_get(name).inspect}" - raise NameError, msg - end - Object.const_set(name, new_module) - end - break - when File.file?(fs_path) - loaded_file = self.root.load_file!(fs_path) - - # Import the loaded constant from Object provided we are the root node. - self.const_set(name, Object.const_get(name)) if self.root? && Object.const_defined?(name) - - # Throw an error if we load the file but we don't find the Object we expect - if loaded_file and not self.const_defined?(name) - msg = "Already loaded file '#{fs_path}' but '#{name.to_s}' was not set, perhaps you need to rename '#{fs_path}'?" - raise LoadError, msg - end - break - end - end - - self.const_defined?(name) - end - - # Is this name present or loadable? - # This method is used by Routes to find valid controllers. - def const_available?(name) - self.const_defined?(name) || load_paths.any? {|lp| lp.filesystem_path(path + [name])} - end - - # Erase all items in this module - def clear! - constants.each do |name| - Object.send(:remove_const, name) if Object.const_defined?(name) && Object.const_get(name).object_id == self.const_get(name).object_id - self.send(:remove_const, name) - end - end - end - - class RootLoadingModule < LoadingModule #:nodoc: - attr_reader :load_paths - - def initialize(*paths) - @load_paths = paths.flatten.collect {|p| p.kind_of?(ConstantLoadPath) ? p : ConstantLoadPath.new(p)} - end - - def root() self end - - def path() [] end - - # Load the source file at the given file path - def load_file!(file_path) - require_dependency(file_path) - end - end - - # This object defines a path from which Constants can be loaded. - class ConstantLoadPath #:nodoc: - # Create a new load path with the filesystem path - def initialize(root) @root = root end - - # Return nil if the path does not exist, or the path to a directory - # if the path leads to a module, or the path to a file if it leads to an object. - def filesystem_path(path, allow_module=true) - fs_path = [@root] - fs_path += path[0..-2].map {|name| const_name_to_module_name name} - - if allow_module - result = File.join(fs_path, const_name_to_module_name(path.last)) - return result if File.directory? result # Return the module path if one exists - end - - result = File.join(fs_path, const_name_to_file_name(path.last)) - - File.file?(result) ? result : nil - end - - def const_name_to_file_name(name) - name.to_s.underscore + '.rb' - end - - def const_name_to_module_name(name) - name.to_s.underscore - end - end end Object.send(:define_method, :require_or_load) { |file_name| Dependencies.require_or_load(file_name) } unless Object.respond_to?(:require_or_load) diff --git a/activesupport/test/loading_module/admin/access_controller.rb b/activesupport/test/loading_module/admin/access_controller.rb deleted file mode 100644 index ddcbda8132..0000000000 --- a/activesupport/test/loading_module/admin/access_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Admin::AccessController -end diff --git a/activesupport/test/loading_module/admin/content_controller.rb b/activesupport/test/loading_module/admin/content_controller.rb deleted file mode 100644 index 454dbac690..0000000000 --- a/activesupport/test/loading_module/admin/content_controller.rb +++ /dev/null @@ -1,3 +0,0 @@ -class Admin::ContentController - def identifier() :inner end -end diff --git a/activesupport/test/loading_module/admin/user_controller.rb b/activesupport/test/loading_module/admin/user_controller.rb deleted file mode 100644 index f265f1597a..0000000000 --- a/activesupport/test/loading_module/admin/user_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Admin::UserController -end diff --git a/activesupport/test/loading_module/content_controller.rb b/activesupport/test/loading_module/content_controller.rb deleted file mode 100644 index 78317b0518..0000000000 --- a/activesupport/test/loading_module/content_controller.rb +++ /dev/null @@ -1,3 +0,0 @@ -class ContentController - def identifier() :outer end -end diff --git a/activesupport/test/loading_module/resource_controller.rb b/activesupport/test/loading_module/resource_controller.rb deleted file mode 100644 index d948f366bf..0000000000 --- a/activesupport/test/loading_module/resource_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class ResourceController -end diff --git a/activesupport/test/loading_module_components/list/list_controller.rb b/activesupport/test/loading_module_components/list/list_controller.rb deleted file mode 100644 index 415db868b2..0000000000 --- a/activesupport/test/loading_module_components/list/list_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class List::ListController -end
\ No newline at end of file diff --git a/activesupport/test/loading_module_tests.rb b/activesupport/test/loading_module_tests.rb deleted file mode 100644 index 22fc00db7e..0000000000 --- a/activesupport/test/loading_module_tests.rb +++ /dev/null @@ -1,124 +0,0 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../lib/active_support/core_ext.rb' -require File.dirname(__FILE__) + '/../lib/active_support/dependencies.rb' - -STAGING_DIRECTORY = File.join(File.dirname(__FILE__), 'loading_module') -COMPONENTS_DIRECTORY = File.join(File.dirname(__FILE__), 'loading_module_components') - -class LoadingModuleTests < Test::Unit::TestCase - def setup - @loading_module = Dependencies::LoadingModule.root(STAGING_DIRECTORY) - Object.send(:remove_const, :Controllers) if Object.const_defined?(:Controllers) - Object.const_set(:Controllers, @loading_module) - end - def teardown - Object.send :remove_const, :Controllers - @loading_module.clear! - Dependencies.clear - end - - def test_setup - assert_kind_of Dependencies::LoadingModule, @loading_module - end - - def test_const_available - assert @loading_module.const_available?(:Admin) - assert @loading_module.const_available?(:ResourceController) - assert @loading_module.const_available?(:ContentController) - assert @loading_module.const_available?("ContentController") - - assert_equal false, @loading_module.const_available?(:AdminController) - assert_equal false, @loading_module.const_available?(:RandomName) - end - - def test_nested_const_available - assert @loading_module::Admin.const_available?(:AccessController) - assert @loading_module::Admin.const_available?(:UserController) - assert @loading_module::Admin.const_available?(:ContentController) - assert ! @loading_module::Admin.const_available?(:ResourceController) - end - - def test_nested_module_export - @loading_module::Admin - assert_equal @loading_module::Admin.object_id, Object::Admin.object_id - assert_equal @loading_module::Admin.object_id, Controllers::Admin.object_id - end - - def test_const_load_module - assert @loading_module.const_load!(:Admin) - assert_kind_of Module, @loading_module::Admin - assert_kind_of Dependencies::LoadingModule, @loading_module::Admin - end - - def test_const_load_controller - assert @loading_module.const_load!(:ContentController) - assert_kind_of Class, @loading_module::ContentController - end - - def test_const_load_nested_controller - assert @loading_module.const_load!(:Admin) - assert_kind_of Dependencies::LoadingModule, @loading_module::Admin - assert @loading_module::Admin.const_available?(:UserController) - assert_kind_of Class, @loading_module::Admin::UserController - end - - def test_pretty_access - assert_kind_of Module, @loading_module::Admin - assert_kind_of Dependencies::LoadingModule, @loading_module::Admin - - assert_kind_of Class, @loading_module::Admin::UserController - assert_kind_of Class, @loading_module::Admin::AccessController - assert_kind_of Class, @loading_module::ResourceController - assert_kind_of Class, @loading_module::ContentController - end - - def test_missing_name - assert_raises(NameError) {@loading_module::PersonController} - assert_raises(NameError) {@loading_module::Admin::FishController} - end - - def test_name_clash - assert ! @loading_module::const_defined?(:ContentController) - assert_equal :outer, @loading_module::ContentController.new.identifier - assert ! @loading_module::Admin.const_defined?(:ContentController) - assert_equal :inner, @loading_module::Admin::ContentController.new.identifier - assert @loading_module::ContentController.object_id != @loading_module::Admin::ContentController.object_id - end - - def test_name_clash_other_way - assert ! @loading_module::Admin.const_defined?(:ContentController) - assert_equal :inner, @loading_module::Admin::ContentController.new.identifier - assert ! @loading_module::const_defined?(:ContentController) - assert_equal :outer, @loading_module::ContentController.new.identifier - assert @loading_module::ContentController.object_id != @loading_module::Admin::ContentController.object_id - end - - def test_access_to_controller_with_numbers - assert @loading_module.const_available?(:Area51Controller) - assert_not_nil @loading_module::Area51Controller - end -end - -class LoadingModuleMultiPathTests < Test::Unit::TestCase - def setup - @loading_module = Dependencies::LoadingModule.root(STAGING_DIRECTORY, COMPONENTS_DIRECTORY) - Object.const_set(:Controllers, @loading_module) - end - def teardown - Object.send :remove_const, :Controllers - @loading_module.clear! - Dependencies.clear - end - - def test_access_from_first - assert_kind_of Module, @loading_module::Admin - assert_kind_of Dependencies::LoadingModule, @loading_module::Admin - assert_kind_of Class, @loading_module::Admin::UserController - end - def test_access_from_second - assert_kind_of Module, @loading_module::List - assert_kind_of Dependencies::LoadingModule, @loading_module::List - assert @loading_module::List.const_load!(:ListController) - assert_kind_of Class, @loading_module::List::ListController - end -end |