module BBLib::FamilyTree
Various methods for finding descendants and subclasses of a class. Intended as an extend mixin for any class.
Public Instance Methods
_inherited_by()
click to toggle source
# File lib/bblib/core/mixins/family_tree.rb, line 44 def _inherited_by @_inherited_by ||= [] end
descendants(include_singletons = false)
click to toggle source
Return all classes that inherit from this class
# File lib/bblib/core/mixins/family_tree.rb, line 8 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
Also aliased as: subclasses
direct_descendants(include_singletons = false)
click to toggle source
Return all classes that directly inherit from this class
# File lib/bblib/core/mixins/family_tree.rb, line 18 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
Also aliased as: direct_subclasses
inherited(klass)
click to toggle source
# File lib/bblib/core/mixins/family_tree.rb, line 48 def inherited(klass) _inherited_by.push(klass) end
instances(descendants = true)
click to toggle source
Return all live instances of the class Passing false will not include instances of sub classes
# File lib/bblib/core/mixins/family_tree.rb, line 27 def instances(descendants = true) inst = ObjectSpace.each_object(self).to_a descendants ? inst : inst.select { |i| i.class == self } end
namespace()
click to toggle source
# File lib/bblib/core/mixins/family_tree.rb, line 32 def namespace BBLib.namespace_of(self) end
root_namespace()
click to toggle source
# File lib/bblib/core/mixins/family_tree.rb, line 36 def root_namespace BBLib.root_namespace_of(self) end