Design question: How to best "discover" classes declared by plugins

Hi there,

I’m currently in the early stages of creating a command-line tool for a bit of fun and one of the big goals design-wise is to make it as extensible as possible. What are the typical ways to do this in Ruby? I do most of my 9 to 5 work with Java where annotations would be the answer, but it doesn’t seem that a similar meta data approach could be used to find the classes you are interested in.

I’ve seen that the logging gem (which I am a big fan off) achieves this by having plugins declare their extensions in a specific name space:

This is what I have gone for at the moment, but are their other methods for discovering classes that you do not know about at the time of writing your code? Would these other methods be preferable? I definitely don’t know much about convention for this kind of thing with Ruby.

As a bonus question, what about finding multiple related classes? Currently I am solving this by requiring the classes to have the same name in different name spaces (e.g. Action::ShellCmd and Result::ShellCmd - not the actual names but you get the idea), but are there other approaches that could be taken here?

You can use Hook methods :
https://aashishgarg.github.io/ruby/hook-methods/

Or define some conventions for plugin register himself to a manager :

class myPlugin
     ::ManagerPlugin.register(self)
   . . . .
end

Or useObjectSpace for discover some unknnwn class.
See big applications which accept plugin : Logstash, Jekyll, Metasploit …

1 Like

Thanks, I’ll definitely look at those apps. That was an interesting post about hook methods also. Appreciate you taking the time to respond