Module that adds descendant tracking to a class
Unreleased gem version
@return [Array]
@private
# File lib/descendants_tracker.rb, line 14 def self.setup(descendant) descendant.instance_variable_set('@descendants', []) end
@private
# File lib/descendants_tracker.rb, line 6 def self.extended(descendant) setup(descendant) end
Add the descendant to this class and the superclass
@param [Class] descendant
@return [self]
@api private
# File lib/descendants_tracker.rb, line 38 def add_descendant(descendant) ancestor = superclass if ancestor.respond_to?(:add_descendant) ancestor.add_descendant(descendant) end descendants.unshift(descendant) self end
Return the descendants of this class
@example
descendants = ParentClass.descendants
@return [Array<Class>]
@api public
# File lib/descendants_tracker.rb, line 27 def descendants @descendants end
Hook called when class is inherited
@param [Class] descendant
@return [self]
@api private
# File lib/descendants_tracker.rb, line 56 def inherited(descendant) super DescendantsTracker.setup(descendant) add_descendant(descendant) end