Permalink
Browse files

moves Class#reachable? to Module#reachable?, bases implementation on …

…anonymous? and constantize, and adds test coverage
  • Loading branch information...
1 parent b8bb54a commit aa82bdf92953824fd35db4a734bf6effa8de3329 @fxn fxn committed Feb 11, 2010
@@ -1,4 +1,5 @@
-require 'active_support/core_ext/object/blank'
+require 'active_support/core_ext/module/anonymous'
+require 'active_support/core_ext/module/reachable'
class Class #:nodoc:
# Returns an array with the names of the subclasses of +self+ as strings.
@@ -8,10 +9,6 @@ def subclasses
Class.subclasses_of(self).map { |o| o.to_s }
end
- def reachable? #:nodoc:
- eval("defined?(::#{self}) && ::#{self}.equal?(self)")
- end
-
# Rubinius
if defined?(Class.__subclasses__)
def descendents
@@ -51,7 +48,7 @@ def descendents
def self.subclasses_of(*superclasses) #:nodoc:
subclasses = []
superclasses.each do |klass|
- subclasses.concat klass.descendents.select {|k| k.name.blank? || k.reachable?}
+ subclasses.concat klass.descendents.select {|k| k.anonymous? || k.reachable?}
end
subclasses
end
@@ -1,6 +1,7 @@
require 'active_support/core_ext/module/aliasing'
require 'active_support/core_ext/module/introspection'
require 'active_support/core_ext/module/anonymous'
+require 'active_support/core_ext/module/reachable'
require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/core_ext/module/attr_internal'
require 'active_support/core_ext/module/attr_accessor_with_default'
@@ -0,0 +1,10 @@
+require 'active_support/core_ext/module/anonymous'
+require 'active_support/core_ext/string/inflections'
+
+class Module
+ def reachable? #:nodoc:
+ !anonymous? && name.constantize.equal?(self)
+ rescue NameError
+ false
+ end
+end
@@ -0,0 +1,41 @@
+require 'abstract_unit'
+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?
+ end
+
+ test "ordinary named classes or modules are reachable" do
+ assert Kernel.reachable?
+ assert Object.reachable?
+ end
+
+ test "a named class or module whose constant has gone is not reachable" do
+ c = eval "class C; end; C"
+ m = eval "module M; end; M"
+
+ self.class.send(:remove_const, :C)
+ self.class.send(:remove_const, :M)
+
+ assert !c.reachable?
+ assert !m.reachable?
+ end
+
+ test "a named class or module whose constants store different objects are not reachable" do
+ c = eval "class C; end; C"
+ m = eval "module M; end; M"
+
+ self.class.send(:remove_const, :C)
+ self.class.send(:remove_const, :M)
+
+ eval "class C; end"
+ eval "module M; end"
+
+ assert C.reachable?
+ assert M.reachable?
+ assert !c.reachable?
+ assert !m.reachable?
+ end
+end

0 comments on commit aa82bdf

Please sign in to comment.