module DescendantsLoader::ClassMethods

Definition of class methods.

Public Instance Methods

descendants() click to toggle source

Overwrites Object.descendants injecting an autoload process.

Calls superclass method
# File lib/descendants_loader.rb, line 34
def descendants
  if (classes = super).empty?
    load_self_descendants
    classes = super
  end

  classes
end
subclasses() click to toggle source

Overwrites Object.subclasses injecting an autoload process.

Calls superclass method
# File lib/descendants_loader.rb, line 46
def subclasses
  if (classes = super).empty?
    load_self_descendants
    classes = super
  end

  classes
end

Private Instance Methods

load_self_descendants() click to toggle source

Find and load all ruby files under the same directory structure of the declared class which is including DescendantsLoader module.

This is the trick to put the classes into ObjectSpace and have the desired behavior of Object.descendants and Object.subclasses.

# File lib/descendants_loader.rb, line 64
def load_self_descendants
  file = ClassFinder.where_is(self)
  path = File.expand_path(File.dirname(file))
  Dir["#{path}/**/*.rb"].each { |f| load f }
end