Tutorial on plugins

« Tutorial 6 Tutorial 8 »


Recap what we've done. Get a deeper understanding of how it works and what goes on behind the scenes.


So far you've written code and defined methods you knew under which conditions they'd be invoked but you didn't really know 'where' that code is.
Butlers plugin system creates an anonymous class using Class.new(Butler::Plugin) and then loads your plugins code into that class. That means your methods are instance methods of a subclass of Butler::Plugin, that you can access all class methods and all instance methods of that class.


The next questions involve timing.
When butler (Butler::Bot) is instanciated, it will load (even before loging in) the plugins that are registered to be loaded on start.
After creating a class with a random part in the name (enabling reloads of the plugin) and loading the metadata into the plugin, the code is class_evaled.
After that is done, butler calls Butler::Plugin::on_load. You can use that callback to initialize things.
When the plugin becomes unloaded, butler calls Butler::Plugin::on_unload. Even before that, the plugin will unhook all subscriptions and commands and timers.


Now that you know where and when things happen, the question is how do you use that.
Since you can alter virtually any aspect of butler through plugins, that's a very broad one. Butler::Plugin provides a lot of mechanisms to deal with day to day plugin problems. But there will come a time where you want to go beyond. When you do that, ensure that:


If you use answer with a Symbol, it will look up the name in the strings-metadata. With single-file plugins, you can use the __END__ part in the ruby file to place yaml-data with all metadata-sections. Also take a look at the list of all metadata.
You should use that even if you write your plugin only for a single language. Simply because it makes it possible for other users to just add another language to your plugin.

« Tutorial 6 Tutorial 8 »

Valid XHTML, 2008-21-05 21:22 CEST