On 14-03-23, 16:50, Martin DeMello wrote:
> I'm writing a crossword format interchange app, modelled on pandoc
> [https://github.com/martindemello/pangrid], which I'd like to do with an
> "open set of plugins" architecture.
>
> The basic idea is that the library provides a base crossword format, and
> converters provide one or both of read (string -> crossword) and write
> (crossword -> string) methods, and a description. The main program would
> scan the plugin directory and know all the available readers and writers.
>
> So a couple of questions:
>
> 1. It's been a while (1.9.3) since I've used ruby for anything other
> than small scripts; is inherited hooks still the best way to do this? Or
> perhaps an explicit register class method, so that I don't need to
> subclass anything just to get the hook?
>
> 2. Some of the plugins have dependencies on other gems (e.g. axslx).
> What's a good way to determine at load time whether a plugin has its
> dependencies met or not?
>
> martin

Hi Martin,

I personally like the explicit register method since that gives the most 
flexibility but there are many ways to do it.

If your plugins are such that they'll generally require inheritance, you 
can use that as a hook. You can also use an included hook for a module. 
Including a module gives more flexibility than subclassing, leaving the 
inheritance free for the user's own code.

You could use a module namespace. Open a Plugins module as a space for 
defined plugins. Any classes under this module would be exposed to your 
app, via Plugins.constants.

Or use a directory. Configure a "load path" where plugins will be found. 
You might still need to have the files register themselves, but provide 
a place for loading them.

Some gems use a name prefix for autoloading, such as pry. Having 
pry-debugger installed will autoload it, even if you just require pry. 
(This can be controlled with a .pryrc file.)

The choice for how you do it will depend on how your plugins will be 
written, installed, and used, and how flexible the loading/unloading 
needs to be.

Andrew Vit