mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #30624 from bogdanvlviv/deprecate_Module_reachable
Deprecate `Module#reachable?` method
This commit is contained in:
commit
eddb9baf39
5 changed files with 23 additions and 52 deletions
|
@ -1,3 +1,7 @@
|
|||
* Deprecate `Module#reachable?` method.
|
||||
|
||||
*bogdanvlviv*
|
||||
|
||||
* Add `config/credentials.yml.enc` to store production app secrets.
|
||||
|
||||
Allows saving any authentication credentials for third party services
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
# 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 @@ require "active_support/core_ext/module/reachable"
|
|||
|
||||
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
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in a new issue