public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Make constantize look into ancestors

[#410 state:resolved]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
fcheung (author)
Sun Dec 14 02:07:06 -0800 2008
jeremy (committer)
Mon Dec 15 11:02:54 -0800 2008
commit  87790e00ec4a0b24146de3757d1d6892689b05e4
tree    dc7aff47de432c126bbcfe5b3aed393d5a4987a9
parent  bf0a8eb777ef7f796333b579329c5556f0bacc46
...
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
367
368
369
...
323
324
325
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
0
@@ -323,47 +323,30 @@ module ActiveSupport
0
       underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
0
     end
0
 
0
-    # Ruby 1.9 introduces an inherit argument for Module#const_get and
0
-    # #const_defined? and changes their default behavior.
0
-    if Module.method(:const_get).arity == 1
0
-      # Tries to find a constant with the name specified in the argument string:
0
-      #
0
-      #   "Module".constantize     # => Module
0
-      #   "Test::Unit".constantize # => Test::Unit
0
-      #
0
-      # The name is assumed to be the one of a top-level constant, no matter whether
0
-      # it starts with "::" or not. No lexical context is taken into account:
0
-      #
0
-      #   C = 'outside'
0
-      #   module M
0
-      #     C = 'inside'
0
-      #     C               # => 'inside'
0
-      #     "C".constantize # => 'outside', same as ::C
0
-      #   end
0
-      #
0
-      # NameError is raised when the name is not in CamelCase or the constant is
0
-      # unknown.
0
-      def constantize(camel_cased_word)
0
-        names = camel_cased_word.split('::')
0
-        names.shift if names.empty? || names.first.empty?
0
-
0
-        constant = Object
0
-        names.each do |name|
0
-          constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
0
-        end
0
-        constant
0
-      end
0
-    else
0
-      def constantize(camel_cased_word) #:nodoc:
0
-        names = camel_cased_word.split('::')
0
-        names.shift if names.empty? || names.first.empty?
0
-
0
-        constant = Object
0
-        names.each do |name|
0
-          constant = constant.const_get(name, false) || constant.const_missing(name)
0
-        end
0
-        constant
0
-      end
0
+    # Tries to find a constant with the name specified in the argument string:
0
+    #
0
+    #   "Module".constantize     # => Module
0
+    #   "Test::Unit".constantize # => Test::Unit
0
+    #
0
+    # The name is assumed to be the one of a top-level constant, no matter whether
0
+    # it starts with "::" or not. No lexical context is taken into account:
0
+    #
0
+    #   C = 'outside'
0
+    #   module M
0
+    #     C = 'inside'
0
+    #     C               # => 'inside'
0
+    #     "C".constantize # => 'outside', same as ::C
0
+    #   end
0
+    #
0
+    # NameError is raised when the name is not in CamelCase or the constant is
0
+    # unknown.
0
+    def constantize(camel_cased_word)
0
+      names = camel_cased_word.split('::')
0
+      names.shift if names.empty? || names.first.empty?
0
+
0
+      constant = Object
0
+      names.each { |name| constant = constant.const_get(name) }
0
+      constant
0
     end
0
 
0
     # Turns a number into an ordinal string used to denote the position in an
...
2
3
4
 
 
 
 
 
 
 
 
 
 
 
 
5
6
 
7
8
9
...
161
162
163
164
 
 
 
165
166
167
...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
174
175
176
 
177
178
179
180
181
182
0
@@ -2,8 +2,21 @@ require 'abstract_unit'
0
 require 'inflector_test_cases'
0
 
0
 module Ace
0
+  module Extension
0
+    def self.included(base)
0
+      base.extend(ClassMethods)
0
+    end
0
+
0
+    module ClassMethods
0
+      def mission_accomplished?
0
+        false
0
+      end
0
+    end
0
+  end
0
+
0
   module Base
0
     class Case
0
+      include Extension
0
     end
0
   end
0
 end
0
@@ -161,7 +174,9 @@ class InflectorTest < Test::Unit::TestCase
0
   end
0
 
0
   def test_constantize_does_lexical_lookup
0
-    assert_raises(NameError) { ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest") }
0
+    assert_equal InflectorTest, ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest")
0
+    assert_nothing_raised { Ace::Base::Case::ClassMethods }
0
+    assert_nothing_raised { assert_equal Ace::Base::Case::ClassMethods, ActiveSupport::Inflector.constantize("Ace::Base::Case::ClassMethods") }
0
   end
0
 
0
   def test_ordinal

Comments