Sha256: 0aeef09134234882246e1b64834124101a7a7e755e3bc35ea5ba101fca6b5a10

Contents?: true

Size: 1.05 KB

Versions: 6

Compression:

Stored size: 1.05 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 )
      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

6 entries across 6 versions & 2 rubygems

Version Path
launchy-2.3.0 lib/launchy/descendant_tracker.rb
launchy-2.2.0-java lib/launchy/descendant_tracker.rb
launchy-2.2.0 lib/launchy/descendant_tracker.rb
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/launchy-2.1.2/lib/launchy/descendant_tracker.rb
launchy-2.1.2-java lib/launchy/descendant_tracker.rb
launchy-2.1.2 lib/launchy/descendant_tracker.rb