Sha256: 13bea86f7f8f7066f707d30a87702075ec03ffa7485dfd82386eed94b7a33aef
Contents?: true
Size: 1.5 KB
Versions: 4
Compression:
Stored size: 1.5 KB
Contents
# frozen_string_literal: true require 'concurrent/array' module Dry module Core # An implementation of descendants tracker, heavily inspired # by the descendants_tracker gem. # # @example # # class Base # extend Dry::Core::DescendantsTracker # end # # class A < Base # end # # class B < Base # end # # class C < A # end # # Base.descendants # => [C, B, A] # A.descendants # => [C] # B.descendants # => [] # module DescendantsTracker class << self # @api private def setup(target) target.instance_variable_set(:@descendants, Concurrent::Array.new) end private # @api private def extended(base) super DescendantsTracker.setup(base) end end # Return the descendants of this class # # @example # descendants = Parent.descendants # # @return [Array<Class>] # # @api public attr_reader :descendants protected # @api private def add_descendant(descendant) ancestor = superclass if ancestor.respond_to?(:add_descendant, true) ancestor.add_descendant(descendant) end descendants.unshift(descendant) end private # @api private def inherited(descendant) super DescendantsTracker.setup(descendant) add_descendant(descendant) end end end end
Version data entries
4 entries across 4 versions & 2 rubygems