Module: BBLib::FamilyTree
- Defined in:
- lib/bblib/core/mixins/family_tree.rb
Overview
Various methods for finding descendants and subclasses of a class. Intended as an extend mixin for any class.
Instance Method Summary collapse
- #_inherited_by ⇒ Object
-
#descendants(include_singletons = false) ⇒ Object
(also: #subclasses)
Return all classes that inherit from this class.
-
#direct_descendants(include_singletons = false) ⇒ Object
(also: #direct_subclasses)
Return all classes that directly inherit from this class.
- #inherited(klass) ⇒ Object
-
#instances(descendants = true) ⇒ Object
Return all live instances of the class Passing false will not include instances of sub classes.
- #namespace ⇒ Object
- #root_namespace ⇒ Object
Instance Method Details
#_inherited_by ⇒ Object
44 45 46 |
# File 'lib/bblib/core/mixins/family_tree.rb', line 44 def _inherited_by @_inherited_by ||= [] end |
#descendants(include_singletons = false) ⇒ Object Also known as: subclasses
Return all classes that inherit from this class
8 9 10 11 12 13 |
# 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 |
#direct_descendants(include_singletons = false) ⇒ Object Also known as: direct_subclasses
Return all classes that directly inherit from this class
18 19 20 21 22 23 |
# 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 |
#inherited(klass) ⇒ Object
48 49 50 |
# File 'lib/bblib/core/mixins/family_tree.rb', line 48 def inherited(klass) _inherited_by.push(klass) end |
#instances(descendants = true) ⇒ Object
Return all live instances of the class Passing false will not include instances of sub classes
27 28 29 30 |
# 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 ⇒ Object
32 33 34 |
# File 'lib/bblib/core/mixins/family_tree.rb', line 32 def namespace BBLib.namespace_of(self) end |
#root_namespace ⇒ Object
36 37 38 |
# File 'lib/bblib/core/mixins/family_tree.rb', line 36 def root_namespace BBLib.root_namespace_of(self) end |