提交 aa82bdf9 编写于 作者: X Xavier Noria

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

moves Class#reachable? to Module#reachable?, bases implementation on anonymous? and constantize, and adds test coverage
上级 b8bb54af
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
......
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'
......
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
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
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册