diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index c19d4c7b32c682896d010209d6ea43059b085863..c339e9380812ecc1298c574ced2c9f88ee9e5772 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,5 +1,8 @@ ## Rails 4.0.0 (unreleased) ## +* Deprecates the compatibility method Module#local_constant_names, + use Module#local_constants instead (which returns symbols). *fxn* + * Deletes the compatibility method Module#method_names, use Module#methods from now on (which returns symbols). *fxn* diff --git a/activesupport/lib/active_support/core_ext/module/introspection.rb b/activesupport/lib/active_support/core_ext/module/introspection.rb index 1893a9cfa65385304ba29a92798657219c67a13a..743db47bac9398509fd05814a4e91aca9defc787 100644 --- a/activesupport/lib/active_support/core_ext/module/introspection.rb +++ b/activesupport/lib/active_support/core_ext/module/introspection.rb @@ -61,9 +61,19 @@ def local_constants #:nodoc: constants(false) end - # Returns the names of the constants defined locally rather than the - # constants themselves. See local_constants. + # *DEPRECATED*: Use +local_constants+ instead. + # + # Returns the names of the constants defined locally as strings. + # + # module M + # X = 1 + # end + # M.local_constant_names # => ["X"] + # + # This method is useful for forward compatibility, since Ruby 1.8 returns + # constant names as strings, whereas 1.9 returns them as symbols. def local_constant_names + ActiveSupport::Deprecation.warn('Module#local_constant_names is deprecated, use Module#local_constants instead', caller) local_constants.map { |c| c.to_s } end end diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index e121e452a3f20fcfeeba94f7d4f54beea31190c3..2c5950edf51dc1f36f39b7b599408a05e401d53d 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -105,7 +105,7 @@ def new_constants next unless mod.is_a?(Module) # Get a list of the constants that were added - new_constants = mod.local_constant_names - original_constants + new_constants = mod.local_constants - original_constants # self[namespace] returns an Array of the constants that are being evaluated # for that namespace. For instance, if parent.rb requires child.rb, the first @@ -133,7 +133,7 @@ def watch_namespaces(namespaces) namespaces.map do |namespace| module_name = Dependencies.to_constant_name(namespace) original_constants = Dependencies.qualified_const_defined?(module_name) ? - Inflector.constantize(module_name).local_constant_names : [] + Inflector.constantize(module_name).local_constants : [] watching << module_name @stack[module_name] << original_constants diff --git a/activesupport/test/core_ext/module_test.rb b/activesupport/test/core_ext/module_test.rb index 950ef82a3c343ff073ecaae8a5b63a738392b217..09ca4e729624bfa10bfb70c61dbf3aec8ad7e39d 100644 --- a/activesupport/test/core_ext/module_test.rb +++ b/activesupport/test/core_ext/module_test.rb @@ -212,6 +212,12 @@ def test_parents def test_local_constants assert_equal %w(Constant1 Constant3), Ab.local_constants.sort.map(&:to_s) end + + def test_local_constant_names + ActiveSupport::Deprecation.silence do + assert_equal %w(Constant1 Constant3), Ab.local_constant_names + end + end end module BarMethodAliaser diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile index 7b3878d2229c910d50bc75043c6451f302efe5a9..bf8d328020147963e935a05520701375feaf0cc3 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/railties/guides/source/active_support_core_extensions.textile @@ -692,7 +692,8 @@ NOTE: Defined in +active_support/core_ext/module/introspection.rb+. h4. Constants -The method +local_constants+ returns the names of the constants that have been defined in the receiver module: +The method +local_constants+ returns the names of the constants that have been +defined in the receiver module: module X @@ -708,7 +709,8 @@ X.local_constants # => [:X1, :X2, :Y] X::Y.local_constants # => [:Y1, :X1] -The names are returned as symbols. The method +local_constant_names+ always returns strings. +The names are returned as symbols. (The method +local_constant_names+ returns +strings, but +local_constant_names+ is deprecated.) NOTE: Defined in +active_support/core_ext/module/introspection.rb+.