Skip to content

Deprecate Module#reachable? method #30624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 0 additions & 3 deletions activesupport/lib/active_support/core_ext/class/subclasses.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ class Module
def reachable? #:nodoc:
!anonymous? && name.safe_constantize.equal?(self)
end
deprecate :reachable?
end
28 changes: 18 additions & 10 deletions activesupport/test/core_ext/module/reachable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
39 changes: 0 additions & 39 deletions guides/source/active_support_core_extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -745,7 +707,6 @@ end

m = Object.send(:remove_const, :M)

m.reachable? # => false
m.anonymous? # => false
```

Expand Down