From 9efca53908c09b7f188183aec6c0a4a2df347316 Mon Sep 17 00:00:00 2001 From: Nicholas Seckar Date: Mon, 27 Mar 2006 05:13:46 +0000 Subject: Dependencies cleanup. Fixes #4221. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4060 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/active_support/core_ext/module.rb | 4 +++- .../core_ext/module/introspection.rb | 21 ++++++++++++++++++++ .../lib/active_support/core_ext/module/loading.rb | 13 ++++++++++++ activesupport/lib/active_support/dependencies.rb | 23 +++++----------------- 4 files changed, 42 insertions(+), 19 deletions(-) create mode 100644 activesupport/lib/active_support/core_ext/module/introspection.rb create mode 100644 activesupport/lib/active_support/core_ext/module/loading.rb (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/module.rb b/activesupport/lib/active_support/core_ext/module.rb index bd6d62468a..e67cf940f6 100644 --- a/activesupport/lib/active_support/core_ext/module.rb +++ b/activesupport/lib/active_support/core_ext/module.rb @@ -1,3 +1,5 @@ require File.dirname(__FILE__) + '/module/inclusion' require File.dirname(__FILE__) + '/module/attribute_accessors' -require File.dirname(__FILE__) + '/module/delegation' \ No newline at end of file +require File.dirname(__FILE__) + '/module/delegation' +require File.dirname(__FILE__) + '/module/introspection' +require File.dirname(__FILE__) + '/module/loading' diff --git a/activesupport/lib/active_support/core_ext/module/introspection.rb b/activesupport/lib/active_support/core_ext/module/introspection.rb new file mode 100644 index 0000000000..0cd0d1ff2c --- /dev/null +++ b/activesupport/lib/active_support/core_ext/module/introspection.rb @@ -0,0 +1,21 @@ +class Module + # Return the module which contains this one; if this is a root module, such as + # +::MyModule+, then Object is returned. + def parent + parent_name = name.split('::')[0..-2] * '::' + parent_name.empty? ? Object : parent_name.constantize + end + + # Return all the parents of this module, ordered from nested outwards. The + # receiver is not contained within the result. + def parents + parents = [] + parts = name.split('::')[0..-2] + until parts.empty? + parents << (parts * '::').constantize + parts.pop + end + parents << Object unless parents.include? Object + parents + end +end diff --git a/activesupport/lib/active_support/core_ext/module/loading.rb b/activesupport/lib/active_support/core_ext/module/loading.rb new file mode 100644 index 0000000000..36c0c61405 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/module/loading.rb @@ -0,0 +1,13 @@ +class Module + def as_load_path + if self == Object || self == Kernel + '' + elsif is_a? Class + parent == self ? '' : parent.as_load_path + else + name.split('::').collect do |word| + word.underscore + end * '/' + end + end +end \ No newline at end of file diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index b82c7e56f3..ffddda6f03 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -78,23 +78,6 @@ class Module #:nodoc: # Rename the original handler so we can chain it to the new one alias :rails_original_const_missing :const_missing - def parent - parent_name = name.split('::')[0..-2] * '::' - parent_name.empty? ? Object : parent_name.constantize - end - - def as_load_path - if self == Object || self == Kernel - '' - elsif is_a? Class - parent == self ? '' : parent.as_load_path - else - name.split('::').collect do |word| - word.underscore - end * '/' - end - end - # Use const_missing to autoload associations so we don't have to # require_association when using single-table inheritance. def const_missing(class_id) @@ -116,7 +99,11 @@ class Module #:nodoc: return mod end - if parent && parent != self + # Attempt to access the name from the parent, unless we don't have a valid + # parent, or the constant is already defined in the parent. If the latter + # is the case, then we are being queried via self::class_id, and we should + # avoid returning the constant from the parent if possible. + if parent && parent != self && ! parents.any? { |p| p.const_defined?(class_id) } suppress(NameError) do return parent.send(:const_missing, class_id) end -- cgit v1.2.3