Module: ActiveSupport::DescendantsTracker

Defined in:
lib/active_support/descendants_tracker.rb

Overview

This module provides an internal implementation to track descendants which is faster than iterating through ObjectSpace.

Constant Summary collapse

@@direct_descendants =
Hash.new { |h, k| h[k] = [] }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clearObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_support/descendants_tracker.rb', line 18

def self.clear
  if defined? ActiveSupport::Dependencies
    @@direct_descendants.each do |klass, descendants|
      if ActiveSupport::Dependencies.autoloaded?(klass)
        @@direct_descendants.delete(klass)
      else
        descendants.reject! { |v| ActiveSupport::Dependencies.autoloaded?(v) }
      end
    end
  else
    @@direct_descendants.clear
  end
end

.descendants(klass) ⇒ Object



11
12
13
14
15
16
# File 'lib/active_support/descendants_tracker.rb', line 11

def self.descendants(klass)
  @@direct_descendants[klass].inject([]) do |descendants, _klass|
    descendants << _klass
    descendants.concat _klass.descendants
  end
end

.direct_descendants(klass) ⇒ Object



7
8
9
# File 'lib/active_support/descendants_tracker.rb', line 7

def self.direct_descendants(klass)
  @@direct_descendants[klass]
end

Instance Method Details

#descendantsObject



41
42
43
# File 'lib/active_support/descendants_tracker.rb', line 41

def descendants
  DescendantsTracker.descendants(self)
end

#direct_descendantsObject



37
38
39
# File 'lib/active_support/descendants_tracker.rb', line 37

def direct_descendants
  DescendantsTracker.direct_descendants(self)
end

#inherited(base) ⇒ Object



32
33
34
35
# File 'lib/active_support/descendants_tracker.rb', line 32

def inherited(base)
  self.direct_descendants << base
  super
end