Sha256: 97563cc1291f71c11e440a716fd03e77be60b3a30d2c7f3c52cae4197b357b2a

Contents?: true

Size: 1.24 KB

Versions: 10

Compression:

Stored size: 1.24 KB

Contents

# encoding: utf-8

require 'thread_safe'

# Module that adds descendant tracking to a class
module DescendantsTracker

  # Return the descendants of this class
  #
  # @example
  #   descendants = ParentClass.descendants
  #
  # @return [Array<Class<DescendantsTracker>>]
  #
  # @api public
  attr_reader :descendants

  # Setup the class for descendant tracking
  #
  # @param [Class<DescendantsTracker>] descendant
  #
  # @return [undefined]
  #
  # @api private
  def self.setup(descendant)
    descendant.instance_variable_set(:@descendants, ThreadSafe::Array.new)
  end

  class << self
    alias_method :extended, :setup
    private :extended
  end

  # Add the descendant to this class and the superclass
  #
  # @param [Class] descendant
  #
  # @return [self]
  #
  # @api private
  def add_descendant(descendant)
    ancestor = superclass
    if ancestor.respond_to?(:add_descendant)
      ancestor.add_descendant(descendant)
    end
    descendants.unshift(descendant)
    self
  end

private

  # Hook called when class is inherited
  #
  # @param [Class] descendant
  #
  # @return [self]
  #
  # @api private
  def inherited(descendant)
    super
    DescendantsTracker.setup(descendant)
    add_descendant(descendant)
  end

end # module DescendantsTracker

Version data entries

10 entries across 8 versions & 4 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/descendants_tracker-0.0.4/lib/descendants_tracker.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/descendants_tracker-0.0.4/lib/descendants_tracker.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/descendants_tracker-0.0.4/lib/descendants_tracker.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/descendants_tracker-0.0.4/lib/descendants_tracker.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/descendants_tracker-0.0.4/lib/descendants_tracker.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/descendants_tracker-0.0.4/lib/descendants_tracker.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/descendants_tracker-0.0.4/lib/descendants_tracker.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/descendants_tracker-0.0.4/lib/descendants_tracker.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/descendants_tracker-0.0.4/lib/descendants_tracker.rb
descendants_tracker-0.0.4 lib/descendants_tracker.rb