Sha256: 77277be742dc163c78d623cc1abc1198080bd3bfc3450879625aa252afa4cf36

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

require "set"

module Launchy
  #
  # Use by either
  #
  #   class Foo
  #     extend DescendantTracker
  #   end
  #
  # or
  #
  #   class Foo
  #     class << self
  #       include DescendantTracker
  #     end
  #   end
  #
  # It will track all the classes that inherit from the extended class and keep
  # them in a Set that is available via the 'children' method.
  #
  module DescendantTracker
    def inherited(klass)
      super
      return unless klass.instance_of?(Class)

      children << klass
    end

    #
    # The list of children that are registered
    #
    def children
      @children = [] unless defined? @children
      @children
    end

    #
    # Find one of the child classes by calling the given method
    # and passing all the rest of the parameters to that method in
    # each child
    def find_child(method, *args)
      children.find do |child|
        Launchy.log "Checking if class #{child} is the one for #{method}(#{args.join(', ')})}"
        child.send(method, *args)
      end
    end
  end
end

Version data entries

4 entries across 3 versions & 2 rubygems

Version Path
launchy-3.1.0 lib/launchy/descendant_tracker.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/launchy-3.0.1/lib/launchy/descendant_tracker.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/launchy-3.0.1/lib/launchy/descendant_tracker.rb
launchy-3.0.1 lib/launchy/descendant_tracker.rb