From d1938953f416ac945b591251567d51e30ee2a6a4 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 9 Feb 2010 18:03:14 -0800 Subject: Reinstate Object.subclasses_of and Class#descendents for plugin compat. This reverts commits 7d312e54bad9c39634c137caec07dfc8df471650, 5f981ff0294ba45aa44ad15aa063970b29aeec44, f85f5dfc8ffefff174b695c6363211d342f77a57, 245bfafe335ff883f7a096eab95ac22fe2848679, and ec7c642f5fe60afc857aa64f1a9b4c2be56f9d70 --- activesupport/lib/active_support/core_ext/class.rb | 1 + .../active_support/core_ext/class/subclasses.rb | 58 ++++++++++++++++++++++ .../active_support/core_ext/object/extending.rb | 11 ++++ 3 files changed, 70 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/class/subclasses.rb create mode 100644 activesupport/lib/active_support/core_ext/object/extending.rb (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/class.rb b/activesupport/lib/active_support/core_ext/class.rb index 62df7d8b82..f2ca9c7cc9 100644 --- a/activesupport/lib/active_support/core_ext/class.rb +++ b/activesupport/lib/active_support/core_ext/class.rb @@ -1,3 +1,4 @@ require 'active_support/core_ext/class/attribute_accessors' require 'active_support/core_ext/class/inheritable_attributes' require 'active_support/core_ext/class/delegating_attributes' +require 'active_support/core_ext/class/subclasses' diff --git a/activesupport/lib/active_support/core_ext/class/subclasses.rb b/activesupport/lib/active_support/core_ext/class/subclasses.rb new file mode 100644 index 0000000000..c166ce8079 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/class/subclasses.rb @@ -0,0 +1,58 @@ +require 'active_support/core_ext/object/blank' + +class Class #:nodoc: + # Returns an array with the names of the subclasses of +self+ as strings. + # + # Integer.subclasses # => ["Bignum", "Fixnum"] + 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 + subclasses = [] + __subclasses__.each {|k| subclasses << k; subclasses.concat k.descendents } + subclasses + end + else + # MRI + begin + ObjectSpace.each_object(Class.new) {} + + def descendents + subclasses = [] + ObjectSpace.each_object(class << self; self; end) do |k| + subclasses << k unless k == self + end + subclasses + end + # JRuby + rescue StandardError + def descendents + subclasses = [] + ObjectSpace.each_object(Class) do |k| + subclasses << k if k < self + end + subclasses.uniq! + subclasses + end + end + 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 self.subclasses_of(*superclasses) #:nodoc: + subclasses = [] + superclasses.each do |klass| + subclasses.concat klass.descendents.select {|k| k.name.blank? || k.reachable?} + end + subclasses + end +end diff --git a/activesupport/lib/active_support/core_ext/object/extending.rb b/activesupport/lib/active_support/core_ext/object/extending.rb new file mode 100644 index 0000000000..c4c37b6a2a --- /dev/null +++ b/activesupport/lib/active_support/core_ext/object/extending.rb @@ -0,0 +1,11 @@ +require 'active_support/core_ext/class/subclasses' + +class Object + # 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: + Class.subclasses_of(*superclasses) + end +end -- cgit v1.2.3