Class: ActiveSupport::DescendantsTracker::DescendantsArray

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_support/descendants_tracker.rb

Overview

DescendantsArray is an array that contains weak references to classes. Note: DescendantsArray is redundant with WeakSet, however WeakSet when used on Ruby 2.7 or 3.0 can trigger a Ruby crash: bugs.ruby-lang.org/issues/18928

Instance Method Summary collapse

Methods included from Enumerable

#as_json, #compact_blank, #exclude?, #excluding, #in_order_of, #including, #index_by, #index_with, #many?, #maximum, #minimum, #pick, #pluck, #sole

Constructor Details

#initializeDescendantsArray

Returns a new instance of DescendantsArray.



120
121
122
# File 'lib/active_support/descendants_tracker.rb', line 120

def initialize
  @refs = []
end

Instance Method Details

#<<(klass) ⇒ Object



124
125
126
# File 'lib/active_support/descendants_tracker.rb', line 124

def <<(klass)
  @refs << WeakRef.new(klass)
end

#cleanup!Object



142
143
144
# File 'lib/active_support/descendants_tracker.rb', line 142

def cleanup!
  @refs.delete_if { |ref| !ref.weakref_alive? }
end

#eachObject



128
129
130
131
132
133
134
135
136
# File 'lib/active_support/descendants_tracker.rb', line 128

def each
  @refs.reject! do |ref|
    yield ref.__getobj__
    false
  rescue WeakRef::RefError
    true
  end
  self
end

#refs_sizeObject



138
139
140
# File 'lib/active_support/descendants_tracker.rb', line 138

def refs_size
  @refs.size
end

#reject!Object



146
147
148
149
150
151
152
# File 'lib/active_support/descendants_tracker.rb', line 146

def reject!
  @refs.reject! do |ref|
    yield ref.__getobj__
  rescue WeakRef::RefError
    true
  end
end