Permalink
Browse files

Merge remote branch 'fxn/master'

  • Loading branch information...
2 parents eea28b0 + a506bac commit 2b874f8e812f5fe817ee87c5f2b7cd5addf058bc @jeremy jeremy 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,24 @@
+require 'active_support/core_ext/object/blank'
+
+class Module
+ # A module may or may not have a name.
+ #
+ # module M; end
+ # M.name # => "M"
+ #
+ # m = Module.new
+ # m.name # => ""
+ #
+ # A module gets a name when it is first assigned to a constant. Either
+ # via the +module+ or +class+ keyword or by an explicit assignment:
+ #
+ # m = Module.new # creates an anonymous module
+ # M = m # => m gets a name here as a side-effect
+ # m.name # => "M"
+ #
+ def anonymous?
+ # Uses blank? because the name of an anonymous class is an empty
+ # string in 1.8, and nil in 1.9.
+ name.blank?
+ end
+end
@@ -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,14 @@
+require 'abstract_unit'
+require 'active_support/core_ext/module/anonymous'
+
+class AnonymousTest < ActiveSupport::TestCase
+ test "an anonymous class or module are anonymous" do
+ assert Module.new.anonymous?
+ assert Class.new.anonymous?
+ end
+
+ test "a named class or module are not anonymous" do
+ assert !Kernel.anonymous?
+ assert !Object.anonymous?
+ 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 2b874f8

Please sign in to comment.