mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Handle case where ancestor is not the end of the chain
This commit is contained in:
parent
eb09411460
commit
3d0e4895cd
2 changed files with 14 additions and 7 deletions
|
@ -198,21 +198,26 @@ module ActiveSupport
|
|||
#
|
||||
# NameError is raised when the name is not in CamelCase or the constant is
|
||||
# unknown.
|
||||
def constantize(camel_cased_word) #:nodoc:
|
||||
def constantize(camel_cased_word)
|
||||
names = camel_cased_word.split('::')
|
||||
names.shift if names.empty? || names.first.empty?
|
||||
|
||||
names.inject(Object) do |constant, name|
|
||||
candidate = constant.const_get(name)
|
||||
if constant.const_defined?(name, false) || constant == Object || !Object.const_defined?(name)
|
||||
candidate
|
||||
if constant == Object
|
||||
constant.const_get(name)
|
||||
else
|
||||
candidate = constant.const_get(name)
|
||||
next candidate if constant.const_defined?(name, false)
|
||||
next candidate unless Object.const_defined?(name)
|
||||
|
||||
# Go down the ancestors to check it it's owned
|
||||
# directly before we reach Object or the end of ancestors.
|
||||
constant.ancestors.each do |ancestor|
|
||||
break if ancestor == Object
|
||||
return candidate if ancestor.const_defined?(name, false)
|
||||
constant = constant.ancestors.inject do |constant, ancestor|
|
||||
break constant if ancestor == Object
|
||||
break ancestor if ancestor.const_defined?(name, false)
|
||||
constant
|
||||
end
|
||||
|
||||
# owner is in Object, so raise
|
||||
constant.const_get(name, false)
|
||||
end
|
||||
|
|
|
@ -29,6 +29,7 @@ module ConstantizeTestCases
|
|||
assert_nothing_raised { assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Case::Dice") }
|
||||
assert_nothing_raised { assert_equal Ace::Base::Fase::Dice, yield("Ace::Base::Fase::Dice") }
|
||||
assert_nothing_raised { assert_equal Ace::Gas::Case, yield("Ace::Gas::Case") }
|
||||
assert_nothing_raised { assert_equal Ace::Gas::Case::Dice, yield("Ace::Gas::Case::Dice") }
|
||||
assert_nothing_raised { assert_equal Case::Dice, yield("Case::Dice") }
|
||||
assert_nothing_raised { assert_equal Case::Dice, yield("Object::Case::Dice") }
|
||||
assert_nothing_raised { assert_equal ConstantizeTestCases, yield("ConstantizeTestCases") }
|
||||
|
@ -52,6 +53,7 @@ module ConstantizeTestCases
|
|||
assert_nothing_raised { assert_equal Ace::Base::Case::Dice, yield("Ace::Base::Case::Dice") }
|
||||
assert_nothing_raised { assert_equal Ace::Base::Fase::Dice, yield("Ace::Base::Fase::Dice") }
|
||||
assert_nothing_raised { assert_equal Ace::Gas::Case, yield("Ace::Gas::Case") }
|
||||
assert_nothing_raised { assert_equal Ace::Gas::Case::Dice, yield("Ace::Gas::Case::Dice") }
|
||||
assert_nothing_raised { assert_equal Case::Dice, yield("Case::Dice") }
|
||||
assert_nothing_raised { assert_equal Case::Dice, yield("Object::Case::Dice") }
|
||||
assert_nothing_raised { assert_equal ConstantizeTestCases, yield("ConstantizeTestCases") }
|
||||
|
|
Loading…
Reference in a new issue