I said I couldn’t do it
It was not long ago I said in the Sentinel Update I didn’t know how to do plugins. Well less than a week after writing it I was reading a few articles by Gregory Brown on modules and Mixins. These are the first time I’ve read something that explains them in a way I actually understand.
I was doing research into modules and mixins as they seemed a bit pointless but thanks to the articles by Gregory I was able to understand them and right in the middle of reading some of the examples and having a play a lightning bolt struck me, it all became clear on how to implement the a plugin manager.
Some Bad Code
Based on some of the stuff I saw I came up with the following, ignore most of it I was just hacking around to see if I could get it to work the names meant more in a previous iteration.
module PluginManager #Just seeing if this works like magic... class LoadPlugin def initialize #The key is a plugin_name the &block is the code so in theory when initialzed it can be run @plugins={} unless !@plugins.nil? end def add_plugin (key,&block) @plugins.merge!({key=>block}) end def run_plugin (key) puts "Plugin to Run = #{key}" puts "Plugin does:\n" @plugins[key].call end def list_plugins @plugins.each_key {|key| puts key} end end end plugins = PluginManager::LoadPlugin.new plugins.add_plugin (:say_hello) do puts "Hello" end plugins.add_plugin (:count_to_10) do for i in 0..10 puts "#{i}" end end plugins.add_plugin (:woop) do puts "Wooop!" end plugins.add_plugin (:say_goodbye) do puts "Good Bye :D" end puts "in theory... Multiple plugins have been loaded" puts "listing plugins:" plugins.list_plugins puts "running plugins:" plugins.run_plugin (:say_hello) plugins.run_plugin (:woop) plugins.run_plugin (:count_to_10) plugins.run_plugin (:say_goodbye)
And when it runs:
in theory... Multiple plugins have been loaded listing plugins: say_hello count_to_10 woop say_goodbye running plugins: Plugin to Run = say_hello Plugin does: Hello Plugin to Run = woop Plugin does: Wooop! Plugin to Run = count_to_10 Plugin does: 0 1 2 3 4 5 6 7 8 9 10 Plugin to Run = say_goodbye Plugin does: Good Bye :D
This is good news I and I really like the site, I will be using it a lot more as I learn more about ruby it explains things really well, and it looks like if you can afford the $8/month you can get a lot more articles by the same guy at practicingruby.com
Summary
So in short… Sentinel will have plugins, I like the blogs at ruby best practices and This blog will also be short :D