Sha256: d187ac18f391053ebd325bfe3b4e0a33003f826140511ef22ec8cebf7d44fcb8

Contents?: true

Size: 995 Bytes

Versions: 1

Compression:

Stored size: 995 Bytes

Contents

require "reflexive/core_ext/kernel/singleton_class"

module Reflexive
  module_function
  
  # 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(klass, generations=nil)
    subclass = []
    
    ObjectSpace.each_object(Class) do |c|
      next if c == klass
      
      ancestors = c.ancestors[0 .. (generations || -1)]
      subclass << c if ancestors.include?(klass)

      ancestors = c.singleton_class.ancestors[0 .. (generations || -1)]
      subclass << c if ancestors.include?(klass)
    end
    
    subclass
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reflexive-0.0.6 lib/reflexive/descendants.rb