Sha256: 70dcd082d2718b777299a225cf0219e4eae5310919bbc060f770febdee121525

Contents?: true

Size: 1.56 KB

Versions: 5

Compression:

Stored size: 1.56 KB

Contents


module BBLib
  # Various methods for finding descendants and subclasses of a class. Intended as an
  # extend mixin for any class.
  module FamilyTree
    # Return all classes that inherit from this class
    def descendants(include_singletons = false)
      return _inherited_by.map { |c| [c, c.descendants] }.flatten.uniq if BBLib.in_opal?
      ObjectSpace.each_object(Class).select do |c|
        (include_singletons || !c.singleton_class?) && c < self
      end
    end

    alias subclasses descendants

    # Return all classes that directly inherit from this class
    def direct_descendants(include_singletons = false)
      return _inherited_by if BBLib.in_opal?
      ObjectSpace.each_object(Class).select do |c|
        (include_singletons || !c.singleton_class?) && c.ancestors[1..-1].find { |k| k.is_a?(Class) } == self
      end
    end

    # Return all live instances of the class
    # Passing false will not include instances of sub classes
    def instances(descendants = true)
      inst = ObjectSpace.each_object(self).to_a
      descendants ? inst : inst.select { |i| i.class == self }
    end

    def namespace
      BBLib.namespace_of(self)
    end

    def root_namespace
      BBLib.root_namespace_of(self)
    end

    alias direct_subclasses direct_descendants

    # If we are in Opal we need to track descendants a bit differently
    if BBLib.in_opal?
      def _inherited_by
        @_inherited_by ||= []
      end

      def inherited(klass)
        _inherited_by.push(klass)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bblib-2.0.5 lib/bblib/core/mixins/family_tree.rb
bblib-2.0.4 lib/bblib/core/mixins/family_tree.rb
bblib-2.0.3 lib/bblib/core/mixins/family_tree.rb
bblib-2.0.1 lib/bblib/core/mixins/family_tree.rb
bblib-2.0.0 lib/bblib/core/mixins/family_tree.rb