Module: CodeRay::PluginHost
Overview
PluginHost
A simple subclass/subfolder plugin system.
Example:
class Generators
extend PluginHost
plugin_path 'app/generators'
end
class Generator
extend Plugin
PLUGIN_HOST = Generators
end
class FancyGenerator < Generator
register_for :fancy
end
Generators[:fancy] #-> FancyGenerator
# or
CodeRay.require_plugin 'Generators/fancy'
# or
Generators::Fancy
Constant Summary collapse
- PluginNotFound =
Raised if Encoders::[] fails because:
-
a file could not be found
-
the requested Plugin is not registered
-
Class.new LoadError
- HostNotFound =
Class.new LoadError
- PLUGIN_HOSTS =
[]
- PLUGIN_HOSTS_BY_ID =
dummy hash
{}
Class Method Summary collapse
-
.extended(mod) ⇒ Object
Adds the module/class to the PLUGIN_HOSTS list.
Instance Method Summary collapse
-
#[](id, *args, &blk) ⇒ Object
(also: #load)
Returns the Plugin for
id
. -
#all_plugins ⇒ Object
Returns an array of all Plugins.
-
#const_missing(const) ⇒ Object
Tries to
load
the missing plugin by translatingconst
to the underscore form (eg. LinesOfCode becomes lines_of_code). -
#default(id = nil) ⇒ Object
Define the default plugin to use when no plugin is found for a given id, or return the default plugin.
-
#list ⇒ Object
Returns an array of all .rb files in the plugin path.
-
#load_all ⇒ Object
Loads all plugins using list and load.
-
#load_plugin_map ⇒ Object
Loads the map file (see map).
-
#map(hash) ⇒ Object
Map a plugin_id to another.
-
#plugin_hash ⇒ Object
A Hash of plugion_id => Plugin pairs.
-
#plugin_path(*args) ⇒ Object
The path where the plugins can be found.
-
#register(plugin, id) ⇒ Object
Every plugin must register itself for
id
by calling register_for, which calls this method.
Class Method Details
.extended(mod) ⇒ Object
Adds the module/class to the PLUGIN_HOSTS list.
72 73 74 |
# File 'lib/coderay/helpers/plugin_host.rb', line 72 def extended mod PLUGIN_HOSTS << mod end |
Instance Method Details
#[](id, *args, &blk) ⇒ Object Also known as: load
Returns the Plugin for id
.
Example:
yaml_plugin = MyPluginHost[:yaml]
49 50 51 52 53 54 55 |
# File 'lib/coderay/helpers/plugin_host.rb', line 49 def [] id, *args, &blk plugin = validate_id(id) begin plugin = plugin_hash.[](plugin, *args, &blk) end while plugin.is_a? String plugin end |
#all_plugins ⇒ Object
Returns an array of all Plugins.
Note: This loads all plugins using load_all.
151 152 153 154 |
# File 'lib/coderay/helpers/plugin_host.rb', line 151 def all_plugins load_all plugin_hash.values.grep(Class) end |
#const_missing(const) ⇒ Object
Tries to load
the missing plugin by translating const
to the underscore form (eg. LinesOfCode becomes lines_of_code).
61 62 63 64 65 66 67 |
# File 'lib/coderay/helpers/plugin_host.rb', line 61 def const_missing const id = const.to_s. gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). downcase load id end |
#default(id = nil) ⇒ Object
Define the default plugin to use when no plugin is found for a given id, or return the default plugin.
See also map.
class MyColorHost < PluginHost
map :navy => :dark_blue
default :gray
end
MyColorHost.default # loads and returns the Gray plugin
114 115 116 117 118 119 120 121 122 |
# File 'lib/coderay/helpers/plugin_host.rb', line 114 def default id = nil if id id = validate_id id raise "The default plugin can't be named \"default\"." if id == :default plugin_hash[:default] = id else load :default end end |
#list ⇒ Object
Returns an array of all .rb files in the plugin path.
The extension .rb is not included.
140 141 142 143 144 145 146 |
# File 'lib/coderay/helpers/plugin_host.rb', line 140 def list Dir[path_to('*')].select do |file| File.basename(file)[/^(?!_)\w+\.rb$/] end.map do |file| File.basename(file, '.rb').to_sym end end |
#load_all ⇒ Object
Loads all plugins using list and load.
39 40 41 42 43 |
# File 'lib/coderay/helpers/plugin_host.rb', line 39 def load_all for plugin in list load plugin end end |
#load_plugin_map ⇒ Object
Loads the map file (see map).
This is done automatically when plugin_path is called.
159 160 161 162 163 164 165 166 167 |
# File 'lib/coderay/helpers/plugin_host.rb', line 159 def load_plugin_map mapfile = path_to '_map' if File.exist? mapfile require mapfile true else false end end |
#map(hash) ⇒ Object
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
95 96 97 98 99 100 101 |
# File 'lib/coderay/helpers/plugin_host.rb', line 95 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 |
#plugin_hash ⇒ Object
A Hash of plugion_id => Plugin pairs.
133 134 135 |
# File 'lib/coderay/helpers/plugin_host.rb', line 133 def plugin_hash @plugin_hash ||= (@plugin_hash = make_plugin_hash).tap { load_plugin_map } end |
#plugin_path(*args) ⇒ Object
The path where the plugins can be found.
79 80 81 82 83 84 |
# File 'lib/coderay/helpers/plugin_host.rb', line 79 def plugin_path *args unless args.empty? @plugin_path = File. File.join(*args) end @plugin_path ||= '' end |
#register(plugin, id) ⇒ Object
Every plugin must register itself for id
by calling register_for, which calls this method.
See Plugin#register_for.
128 129 130 |
# File 'lib/coderay/helpers/plugin_host.rb', line 128 def register plugin, id plugin_hash[validate_id(id)] = plugin end |