From cadd995d6b44dc5e2145904ea0a0fba95a6643b9 Mon Sep 17 00:00:00 2001 From: murphy Date: Sun, 6 Nov 2005 01:28:19 +0000 Subject: helpers/plugin.rb: New plugin mapping system _maps added; demos adjusted TODO updated --- lib/coderay/helpers/plugin.rb | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'lib/coderay/helpers/plugin.rb') diff --git a/lib/coderay/helpers/plugin.rb b/lib/coderay/helpers/plugin.rb index 63c3739..6fcfe35 100644 --- a/lib/coderay/helpers/plugin.rb +++ b/lib/coderay/helpers/plugin.rb @@ -69,6 +69,7 @@ module PluginHost def plugin_path *args unless args.empty? @plugin_path = File.join(*args) + load_map end @plugin_path end @@ -110,6 +111,7 @@ module PluginHost @plugin_hash ||= create_plugin_hash end + # Makes a map of all loaded scanners. def inspect map = plugin_hash.dup map.each do |id, plugin| @@ -118,6 +120,32 @@ module PluginHost map.inspect end + # Map a plugin_id to another. + # + # Usage: Put this in a file plugin_path/_map.rb. + # + # class MyColorHost < PluginHost + # map :navy => :dark_blue, + # :maroon => :brown, + # :luna => :moon + # end + + def map hash + for from, to in hash + from = validate_id from + to = validate_id to + plugin_hash[from] = to unless plugin_hash.has_key? from + end + end + + def load_map + begin + require path_to('_map') + rescue LoadError + warn 'no _map.rb found for %s' % name if $DEBUG + end + end + # Every plugin must register itself for one or more # +ids+ by calling register_for, which calls this method. @@ -138,7 +166,9 @@ module PluginHost # # The extension .rb is not included. def all_plugin_names - Dir[path_to('*')].map do |file| + Dir[path_to('*')].select do |file| + File.basename(file)[/^(?!_)\w+\.rb$/] + end.map do |file| File.basename file, '.rb' end end @@ -156,7 +186,11 @@ module PluginHost # Example: # yaml_plugin = MyPluginHost[:yaml] def [] id, *args, &blk - plugin_hash.[] validate_id(id), *args, &blk + plugin = validate_id(id) + begin + plugin = plugin_hash.[] plugin, *args, &blk + end while plugin.is_a? Symbol + plugin end # Alias for +[]+. -- cgit v1.2.1