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.4'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#descendantsArray<Class<DescendantsTracker>> (readonly)

Return the descendants of this class

Examples:

descendants = ParentClass.descendants

Returns:



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

def descendants
  @descendants
end

Class Method Details

.setup(descendant) ⇒ undefined Also known as: extended

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.

Setup the class for descendant tracking

Parameters:

Returns:

  • (undefined)


25
26
27
# File 'lib/descendants_tracker.rb', line 25

def self.setup(descendant)
  descendant.instance_variable_set(:@descendants, ThreadSafe::Array.new)
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)


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

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