Skip to content

Commit

Permalink
Merge pull request #30624 from bogdanvlviv/deprecate_Module_reachable
Browse files Browse the repository at this point in the history
Deprecate `Module#reachable?` method
  • Loading branch information
sgrif committed Sep 20, 2017
2 parents e7f39af + 2750653 commit eddb9ba
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 52 deletions.
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

0 comments on commit eddb9ba

Please sign in to comment.