diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-05-01 13:34:07 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-05-01 13:34:31 -0700 |
commit | c699a4daf1d496fe6d902c845a4d4c108dda3a6b (patch) | |
tree | 320db660a57b1d4e2acda74ac95f5a280de5f605 | |
parent | 1f2a4b37accc023c96148d54d8daa3a751446c44 (diff) | |
download | rails-c699a4daf1d496fe6d902c845a4d4c108dda3a6b.tar.gz rails-c699a4daf1d496fe6d902c845a4d4c108dda3a6b.tar.bz2 rails-c699a4daf1d496fe6d902c845a4d4c108dda3a6b.zip |
Ruby 1.9 compat: compatibility wrapper for new Module#const_defined? behavior
-rw-r--r-- | activesupport/lib/active_support/dependencies.rb | 21 | ||||
-rw-r--r-- | activesupport/test/inflector_test.rb | 10 |
2 files changed, 18 insertions, 13 deletions
diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 70b34c90e8..eaba46dd9c 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -120,15 +120,26 @@ module Dependencies #:nodoc: # We can't use defined? because it will invoke const_missing for the parent # of the name we are checking. names.inject(Object) do |mod, name| - return false unless mod.const_defined? name + return false unless uninherited_const_defined?(mod, name) mod.const_get name end return true end + if Module.method(:const_defined?).arity == 1 + # Does this module define this constant? + # Wrapper to accomodate changing Module#const_defined? in Ruby 1.9 + def uninherited_const_defined?(mod, const) + mod.const_defined?(const) + end + else + def uninherited_const_defined?(mod, const) #:nodoc: + mod.const_defined?(const, false) + end + end + # 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. - # def loadable_constants_for_path(path, bases = load_paths) path = $1 if path =~ /\A(.*)\.rb\Z/ expanded_path = File.expand_path(path) @@ -237,7 +248,7 @@ module Dependencies #:nodoc: raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!" end - raise ArgumentError, "#{from_mod} is not missing constant #{const_name}!" if from_mod.const_defined?(const_name) + raise ArgumentError, "#{from_mod} is not missing constant #{const_name}!" if uninherited_const_defined?(from_mod, const_name) qualified_name = qualified_name_for from_mod, const_name path_suffix = qualified_name.underscore @@ -246,12 +257,12 @@ module Dependencies #:nodoc: file_path = search_for_file(path_suffix) if file_path && ! loaded.include?(File.expand_path(file_path)) # We found a matching file to load require_or_load file_path - raise LoadError, "Expected #{file_path} to define #{qualified_name}" unless from_mod.const_defined?(const_name) + raise LoadError, "Expected #{file_path} to define #{qualified_name}" unless uninherited_const_defined?(from_mod, const_name) return from_mod.const_get(const_name) elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix) return mod elsif (parent = from_mod.parent) && parent != from_mod && - ! from_mod.parents.any? { |p| p.const_defined?(const_name) } + ! from_mod.parents.any? { |p| uninherited_const_defined?(p, const_name) } # If our parents do not have a constant named +const_name+ then we are free # to attempt to load upwards. If they do have such a constant, then this # const_missing must be due to from_mod::const_name, which should not diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index 00d8f45028..26af245ff7 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -120,14 +120,8 @@ class InflectorTest < Test::Unit::TestCase assert_raises(NameError) { Inflector.constantize("InvalidClass\n") } end - if RUBY_VERSION < '1.9.0' - def test_constantize_does_lexical_lookup - assert_raises(NameError) { Inflector.constantize("Ace::Base::InflectorTest") } - end - else - def test_constantize_does_dynamic_lookup - assert_equal self.class, Inflector.constantize("Ace::Base::InflectorTest") - end + def test_constantize_does_lexical_lookup + assert_raises(NameError) { Inflector.constantize("Ace::Base::InflectorTest") } end def test_ordinal |