summaryrefslogtreecommitdiff
path: root/lib/coderay/helpers/plugin.rb
diff options
context:
space:
mode:
authormurphy <murphy@rubychan.de>2005-11-06 01:28:19 +0000
committermurphy <murphy@rubychan.de>2005-11-06 01:28:19 +0000
commitcadd995d6b44dc5e2145904ea0a0fba95a6643b9 (patch)
tree2736fa1ffe3d4c844cc8b9d2749174f88cf21f07 /lib/coderay/helpers/plugin.rb
parent681b5008d2cf42bf61503a91ffe48c17430141ab (diff)
downloadcoderay-cadd995d6b44dc5e2145904ea0a0fba95a6643b9.tar.gz
helpers/plugin.rb: New plugin mapping system
_maps added; demos adjusted TODO updated
Diffstat (limited to 'lib/coderay/helpers/plugin.rb')
-rw-r--r--lib/coderay/helpers/plugin.rb38
1 files changed, 36 insertions, 2 deletions
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 +[]+.