diff options
author | Nick Sieger <nick@nicksieger.com> | 2008-09-30 12:08:33 -0500 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2008-10-03 21:28:47 +0200 |
commit | 395369bc2b85346e8f196f202941e5015dc1481e (patch) | |
tree | 4d2a5cb4cc67244ca28f518e22ec817139e85b5c /activesupport | |
parent | 8d337e9ec2e25007d557150dbe7557ab3c3bd05f (diff) | |
download | rails-395369bc2b85346e8f196f202941e5015dc1481e.tar.gz rails-395369bc2b85346e8f196f202941e5015dc1481e.tar.bz2 rails-395369bc2b85346e8f196f202941e5015dc1481e.zip |
Provide alternate implementation of Object#subclasses_of for JRuby
Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#1144 state:committed]
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/extending.rb | 45 |
1 files changed, 33 insertions, 12 deletions
diff --git a/activesupport/lib/active_support/core_ext/object/extending.rb b/activesupport/lib/active_support/core_ext/object/extending.rb index 082e98a297..bbf6f8563b 100644 --- a/activesupport/lib/active_support/core_ext/object/extending.rb +++ b/activesupport/lib/active_support/core_ext/object/extending.rb @@ -3,22 +3,43 @@ class Object Class.remove_class(*subclasses_of(*superclasses)) end - # 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. - def subclasses_of(*superclasses) #:nodoc: - subclasses = [] - - superclasses.each do |sup| - ObjectSpace.each_object(class << sup; self; end) do |k| - if k != sup && (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id")) - subclasses << k + begin + ObjectSpace.each_object(Class.new) {} + + # 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. + def subclasses_of(*superclasses) #:nodoc: + subclasses = [] + + superclasses.each do |sup| + ObjectSpace.each_object(class << sup; self; end) do |k| + if k != sup && (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id")) + subclasses << k + end end end + + subclasses end + rescue RuntimeError + # JRuby and any implementations which cannot handle the objectspace traversal + # above fall back to this implementation + def subclasses_of(*superclasses) #:nodoc: + subclasses = [] - subclasses + superclasses.each do |sup| + ObjectSpace.each_object(Class) do |k| + if superclasses.any? { |superclass| k < superclass } && + (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id")) + subclasses << k + end + end + subclasses.uniq! + end + subclasses + end end def extended_by #:nodoc: |