Module: Launchy::DescendantTracker

Overview

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.

Instance Method Summary collapse

Instance Method Details

#childrenObject

The list of children that are registered



31
32
33
34
35
36
# File 'lib/launchy/descendant_tracker.rb', line 31

def children
  unless defined? @children
    @children = Set.new
  end
  return @children
end

#find_child(method, *args) ⇒ Object

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



42
43
44
45
46
47
# File 'lib/launchy/descendant_tracker.rb', line 42

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

#inherited(klass) ⇒ Object



23
24
25
26
# File 'lib/launchy/descendant_tracker.rb', line 23

def inherited( klass )
  return unless klass.instance_of?( Class )
  self.children << klass
end