Some years ago while I was contributing to the awesome Open Source project Adhearsion, we decided to split the logic contained in 1.0 version into different modules that might be loaded by the developer when needed. Adhearsion 1.0 was tightly coupled with gems like activerecord and activesupport, which were not required for the framework basic functionality and did not provide any real value to most of the Adhearsion applications. Decoupling the logic allowed developers to include only the required dependencies in their applications.
Besides the primary goal of decoupling logic, developers should be able as well to create their own modules to extend the base functionality provided by Adhearsion. Those modules were called plugins.
While thinking about how to build this plugin functionality, I dug into different libraries trying to find a clean solution, and Rails came to the rescue. The developer that is using Rails can define her own plugins to extend the functionality of the framework or modify its initialization. These plugins are called Railties. Creating a Railtie is really simple:
- inherit from Railtie class.
- load your class during the Rails boot process.
But… how does Rails know that it should execute the code defined in the Railtie subclass while boosting itself? The trick is based on a cool feature from the Ruby language, which defines a hook in the parent class that is raised every time the class is inherited. Here you can see the snippet of code that builds the Railtie magic.
Eventually, for Adhearsion plugins I followed the same rule.
Find below two snippets of code that registers a list of subclasses in both ruby and python.
The output of both scripts is: