No commit activity in last 3 years
No release in over 3 years
This gem is an Object.descendants and Object.subclasses loader. The desired classes will have an autoload of its hierarchy. This aims a problem with lazy loading ruby classes (usually under development environment). For more information please check README.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 10.0
~> 3.0
~> 0.7

Runtime

 Project Readme

descendants-loader Build Status Dependency Status Inline docs Gem Version

If you had any trouble trying to find dynamically the classes that implements a base/parent class, maybe this gem could help you.

This is often seen when working with Rails on development/test environment since the eager_load is set to false by default, therefore all the classes are loaded dynamically when requested instead of being loaded in the bootstrap.

Due to that, when you call something like MyBaseClass.descendants or MyBaseClass.subclasses it will return an empty array if the descendants/subclasses weren't accessed earlier.

Getting started

  1. Add descendants-loader to your Gemfile and bundle install:
gem 'descendants-loader'
  1. Load the module and use it according to the example bellow:
require 'descendants_loader'

class MyBaseClass
  include DescendantsLoader

  ... methods / your implementation ...
end

When the module DescendantsLoader is included it overwrites the static methods Object.descendants and Object.subclasses. Basically it forces the autoloading of all classes under the same directory (recursivelly) of the base class. Finally it calls super, returning the expect subclasses.

Continuing with the example (subclasses under the same directory structure of MyBaseClass):

class SonA < MyBaseClass
  ...
end
class SonB < MyBaseClass
  ...
end
class SonC < MyBaseClass
  ...
end
class GrandSonA < SonA
  ...
end

Calling those methods we get the following output:

MyBaseClass.subclasses => [SonA, SonB, SonC] MyBaseClass.descendants => [SonA, SonB, SonC, GrandSonA]

Contact

Code and Bug Reports

Questions, Problems, Suggestions, etc.

Contributing

To fetch & test the library for development, do:

git clone https://github.com/djlebersilvestre/descendants-loader.git
cd descendants-loader
./script/setup_dev_env.sh

If you want to contribute, please:

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Bonus Points go out to anyone who also updates CHANGELOG.md :)
  • Send me a pull request on Github.

Running Individual Tests

This project uses Rspec. Individual tests can be run like this:

bundle exec rspec path/to/test.rb

Copyright

Copyright (c) 2015 Daniel Silvestre. See MIT-LICENSE for details.

TODOs

  • Test compatibility with other ruby versions.