Sha256: 06d1ab8d151e4e4486db269a5e590ae97ac03516c92f508a67f84e58ba8f6f24

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

class Class

  # List all descedents of this class.
  #
  #   class X ; end
  #   class A < X; end
  #   class B < X; end
  #   X.descendents  #=> [A,B]
  #
  # 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]
  #
  # NOTE: This is a intensive operation. Do not
  # expect it to be super 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
      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

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/class/descendants.rb
facets-2.8.3 lib/core/facets/class/descendants.rb
facets-2.8.2 lib/core/facets/class/descendants.rb
facets-2.8.1 lib/core/facets/class/descendants.rb