aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-09-23 01:09:20 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-09-23 01:09:20 +0000
commitea456801182195b113e695ec9019757ce523be0f (patch)
treea574d6a90bca45442a348b2f7efc993e538fe118
parent2d02199e1581db8dc84361803950b1697f493fc0 (diff)
downloadrails-ea456801182195b113e695ec9019757ce523be0f.tar.gz
rails-ea456801182195b113e695ec9019757ce523be0f.tar.bz2
rails-ea456801182195b113e695ec9019757ce523be0f.zip
Object.subclasses_of includes anonymous subclasses.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7590 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/object/extending.rb18
-rw-r--r--activesupport/test/abstract_unit.rb1
-rw-r--r--activesupport/test/core_ext/object_and_class_ext_test.rb9
4 files changed, 22 insertions, 8 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index d2684da9a1..e92ceb1362 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Object.subclasses_of includes anonymous subclasses. [Jeremy Kemper]
+
* Fixed that pluralizing an empty string should return the same empty string, not "s" #7720 [josh]
* Added call to inspect on non-string classes for the logger #8533 [codahale]
diff --git a/activesupport/lib/active_support/core_ext/object/extending.rb b/activesupport/lib/active_support/core_ext/object/extending.rb
index 0815909410..7c79d73f21 100644
--- a/activesupport/lib/active_support/core_ext/object/extending.rb
+++ b/activesupport/lib/active_support/core_ext/object/extending.rb
@@ -5,17 +5,21 @@ class Object
def subclasses_of(*superclasses) #:nodoc:
subclasses = []
+
+ # Exclude this class unless it's a subclass of our supers and is defined.
+ # We check defined? in case we find a removed class that has yet to be
+ # garbage collected. This also fails for anonymous classes -- please
+ # submit a patch if you have a workaround.
ObjectSpace.each_object(Class) do |k|
- next unless # Exclude this class unless
- superclasses.any? { |superclass| k < superclass } && # It *is* a subclass of our supers
- eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id") # It *is* defined
- # Note that we check defined? in case we find a removed class that has
- # yet to be garbage collected.
- subclasses << k
+ if superclasses.any? { |superclass| k < superclass } &&
+ (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id"))
+ subclasses << k
+ end
end
+
subclasses
end
-
+
def extended_by #:nodoc:
ancestors = class << self; ancestors end
ancestors.select { |mod| mod.class == Module } - [ Object, Kernel ]
diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb
index fba9ff97b2..791efb549a 100644
--- a/activesupport/test/abstract_unit.rb
+++ b/activesupport/test/abstract_unit.rb
@@ -6,6 +6,7 @@ require 'active_support'
# Wrap tests that use Mocha and skip if unavailable.
def uses_mocha(test_name)
require 'rubygems'
+ gem 'mocha', '>= 0.5.5'
require 'mocha'
yield
rescue LoadError
diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb
index 1fbb853bc9..a23da097e9 100644
--- a/activesupport/test/core_ext/object_and_class_ext_test.rb
+++ b/activesupport/test/core_ext/object_and_class_ext_test.rb
@@ -97,7 +97,14 @@ class ClassExtTest < Test::Unit::TestCase
classes = Object.subclasses_of(ClassI, ClassK)
assert_equal %w(ClassJ Nested::ClassL), classes.collect(&:to_s).sort
end
-
+
+ def test_subclasses_of_doesnt_find_anonymous_classes
+ assert_equal [], Object.subclasses_of(Foo)
+ bar = Class.new(Foo)
+ assert_nothing_raised do
+ assert_equal [bar], Object.subclasses_of(Foo)
+ end
+ end
end
class ObjectTests < Test::Unit::TestCase