Module: DescendantsTracker

Defined in:
lib/descendants_tracker.rb,
lib/descendants_tracker/version.rb

Overview

Module that adds descendant tracking to a class

Constant Summary collapse

VERSION =

Unreleased gem version

'0.0.3'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setup(descendant) ⇒ Array

Returns:

  • (Array)


15
16
17
# File 'lib/descendants_tracker.rb', line 15

def self.setup(descendant)
  descendant.instance_variable_set('@descendants', [])
end

Instance Method Details

#add_descendant(descendant) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add the descendant to this class and the superclass

Parameters:

  • descendant (Class)

Returns:

  • (self)


39
40
41
42
43
44
45
46
# File 'lib/descendants_tracker.rb', line 39

def add_descendant(descendant)
  ancestor = superclass
  if ancestor.respond_to?(:add_descendant)
    ancestor.add_descendant(descendant)
  end
  descendants.unshift(descendant)
  self
end

#descendantsArray<Class>

Return the descendants of this class

Examples:

descendants = ParentClass.descendants

Returns:

  • (Array<Class>)


28
29
30
# File 'lib/descendants_tracker.rb', line 28

def descendants
  @descendants
end