lib/core/facets/class/descendants.rb in facets-2.8.4 vs lib/core/facets/class/descendants.rb in facets-2.9.0.pre.1
- old
+ new
@@ -1,48 +1,35 @@
+require 'facets/class/subclasses'
+
class Class
# List all descedents of this class.
#
- # class X ; end
- # class A < X; end
- # class B < X; end
- # X.descendents #=> [A,B]
+ # class A ; end
+ # class B < A; end
+ # class C < A; end
+ # A.descendants #=> [B,C]
#
- # You may also limit the generational distance
- # the subclass may be from the parent class.
+ # You may also limit the generational distance the subclass may be from
+ # the parent class.
#
# class X ; end
- # class A < X; end
- # class B < A; end
- # X.descendents #=> [A, B]
- # X.descendents(1) #=> [A]
+ # class Y < X; end
+ # class Z < Y; end
+ # X.descendants #=> [Y,Z]
+ # X.descendants(1) #=> [Y]
#
- # NOTE: This is a intensive operation. Do not
- # expect it to be super fast.
+ # NOTE: This is a intensive operation. Do not expect it to be very fast.
- def descendants(generations=nil)
- subclass = []
- ObjectSpace.each_object(Class) do |c|
- ancestors = c.ancestors[0..(generations || -1)]
- if ancestors.include?(self) and self != c
- subclass << c
+ def descendants(generations=-1)
+ descendants = []
+ subclasses.each do |k|
+ descendants << k
+ if generations != 1
+ descendants.concat(k.descendants(generations - 1))
end
end
- return subclass
- end
-
- #
- alias_method :descendents, :descendants
-
- unless defined?(::ActiveSupport)
-
- # Obvious alias for descendants.
- #
- # NOTE: ActiveSupport returns string names rather
- # then actual classes, so this is excluded
- # if ActiveSupport has already been loaded.
- alias_method :subclasses, :descendants
-
+ descendants
end
end