Sha256: 4b9b79b6a46440f2077baf1b33a5c375e6159673e21eab8f0d96df05751a16d2

Contents?: true

Size: 1.06 KB

Versions: 22

Compression:

Stored size: 1.06 KB

Contents

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 )
      return unless klass.instance_of?( Class )
      self.children << klass
    end

    #
    # The list of children that are registered
    #
    def children
      unless defined? @children
        @children = Set.new
      end
      return @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 )
      klass = 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

22 entries across 22 versions & 3 rubygems

Version Path
launchy-2.0.0-java lib/launchy/descendant_tracker.rb
launchy-2.0.0 lib/launchy/descendant_tracker.rb