提交 eddb9baf 编写于 作者: S Sean Griffin 提交者: GitHub

Merge pull request #30624 from bogdanvlviv/deprecate_Module_reachable

Deprecate `Module#reachable?` method
* Deprecate `Module#reachable?` method.
*bogdanvlviv*
* Add `config/credentials.yml.enc` to store production app secrets.
Allows saving any authentication credentials for third party services
......
# frozen_string_literal: true
require_relative "../module/anonymous"
require_relative "../module/reachable"
class Class
begin
# Test if this Ruby supports each_object against singleton_class
......
......@@ -7,4 +7,5 @@ class Module
def reachable? #:nodoc:
!anonymous? && name.safe_constantize.equal?(self)
end
deprecate :reachable?
end
......@@ -5,13 +5,17 @@
class AnonymousTest < ActiveSupport::TestCase
test "an anonymous class or module is not reachable" do
assert !Module.new.reachable?
assert !Class.new.reachable?
assert_deprecated do
assert !Module.new.reachable?
assert !Class.new.reachable?
end
end
test "ordinary named classes or modules are reachable" do
assert Kernel.reachable?
assert Object.reachable?
assert_deprecated do
assert Kernel.reachable?
assert Object.reachable?
end
end
test "a named class or module whose constant has gone is not reachable" do
......@@ -21,8 +25,10 @@ class AnonymousTest < ActiveSupport::TestCase
self.class.send(:remove_const, :C)
self.class.send(:remove_const, :M)
assert !c.reachable?
assert !m.reachable?
assert_deprecated do
assert !c.reachable?
assert !m.reachable?
end
end
test "a named class or module whose constants store different objects are not reachable" do
......@@ -35,9 +41,11 @@ class AnonymousTest < ActiveSupport::TestCase
eval "class C; end"
eval "module M; end"
assert C.reachable?
assert M.reachable?
assert !c.reachable?
assert !m.reachable?
assert_deprecated do
assert C.reachable?
assert M.reachable?
assert !c.reachable?
assert !m.reachable?
end
end
end
......@@ -674,44 +674,6 @@ M.parents # => [X::Y, X, Object]
NOTE: Defined in `active_support/core_ext/module/introspection.rb`.
### Reachable
A named module is reachable if it is stored in its corresponding constant. It means you can reach the module object via the constant.
That is what ordinarily happens, if a module is called "M", the `M` constant exists and holds it:
```ruby
module M
end
M.reachable? # => true
```
But since constants and modules are indeed kind of decoupled, module objects can become unreachable:
```ruby
module M
end
orphan = Object.send(:remove_const, :M)
# The module object is orphan now but it still has a name.
orphan.name # => "M"
# You cannot reach it via the constant M because it does not even exist.
orphan.reachable? # => false
# Let's define a module called "M" again.
module M
end
# The constant M exists now again, and it stores a module
# object called "M", but it is a new instance.
orphan.reachable? # => false
```
NOTE: Defined in `active_support/core_ext/module/reachable.rb`.
### Anonymous
A module may or may not have a name:
......@@ -745,7 +707,6 @@ end
m = Object.send(:remove_const, :M)
m.reachable? # => false
m.anonymous? # => false
```
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册