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.1.1-java lib/launchy/descendant_tracker.rb
launchy-2.1.1 lib/launchy/descendant_tracker.rb
sunrise-cms-0.3.3 vendor/bundle/ruby/1.9.1/gems/launchy-2.1.0/lib/launchy/descendant_tracker.rb
sunrise-cms-0.3.2 vendor/bundle/ruby/1.9.1/gems/launchy-2.1.0/lib/launchy/descendant_tracker.rb
sunrise-cms-0.3.1 vendor/bundle/ruby/1.9.1/gems/launchy-2.1.0/lib/launchy/descendant_tracker.rb
sunrise-cms-0.3.0 vendor/bundle/ruby/1.9.1/gems/launchy-2.1.0/lib/launchy/descendant_tracker.rb
sunrise-cms-0.3.0.rc vendor/bundle/ruby/1.9.1/gems/launchy-2.1.0/lib/launchy/descendant_tracker.rb
launchy-2.1.0-java lib/launchy/descendant_tracker.rb
launchy-2.1.0 lib/launchy/descendant_tracker.rb
launchy-2.0.5-java lib/launchy/descendant_tracker.rb
wijet-launchy-2.0.6 lib/launchy/descendant_tracker.rb
launchy-2.0.5 lib/launchy/descendant_tracker.rb
launchy-2.0.4-java lib/launchy/descendant_tracker.rb
launchy-2.0.4 lib/launchy/descendant_tracker.rb
launchy-2.0.3-java lib/launchy/descendant_tracker.rb
launchy-2.0.3 lib/launchy/descendant_tracker.rb
launchy-2.0.2-java lib/launchy/descendant_tracker.rb
launchy-2.0.2 lib/launchy/descendant_tracker.rb
launchy-2.0.1 lib/launchy/descendant_tracker.rb
launchy-2.0.1-java lib/launchy/descendant_tracker.rb