Class: Olelo::Plugin
- Defined in:
- lib/olelo/plugin.rb
Overview
Olelo plugin system
Class Attribute Summary collapse
-
.dir ⇒ Object
Returns the value of attribute dir.
-
.disabled ⇒ Object
Returns the value of attribute disabled.
-
.failed ⇒ Object
readonly
Get failed plugins.
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
-
.enabled?(path) ⇒ Boolean
Check if plugin is enabled.
- .for(obj) ⇒ Object
-
.load(path) ⇒ Boolean
Load plugin by path.
-
.load_all ⇒ Object
Load all plugins.
-
.loaded ⇒ Object
Get loaded plugins.
- .register(path, plugin) ⇒ Object
-
.start ⇒ void
Start plugins.
Instance Method Summary collapse
-
#dependencies(*list) ⇒ Object
Load specified plugins and fail with LoadError if dependencies are missing.
-
#initialize(path, file) ⇒ Plugin
constructor
A new instance of Plugin.
- #setup(&block) ⇒ Object
-
#start ⇒ Boolean
Start the plugin by calling the #setup method.
-
#virtual_fs ⇒ Object
Virtual filesystem used to load plugin assets.
Methods included from Hooks
included, #invoke_hook, #with_hooks
Methods included from Util
#check, #decode64, #deep_copy, #encode64, #escape, #escape_html, #escape_javascript, included, #md5, #no_cache?, #sha256, #titlecase, #truncate, #unescape, #unescape_backslash, #unescape_html, #valid_xml_chars?
Methods inherited from Module
#attr_reader?, #attr_setter, #private_constant, #redefine_method
Constructor Details
#initialize(path, file) ⇒ Plugin
Returns a new instance of Plugin.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/olelo/plugin.rb', line 108 def initialize(path, file) @setup = nil @path, @file = path, file @started = false @dependencies = Set.new const_set(:PLUGIN, self) with_hooks :load do names = path.split('/') names[0..-2].inject('') do |parent, x| parent /= x Plugin.load(parent) parent end (0...names.length).inject(Plugin) do |mod, i| elem = names[i].split('_').map(&:capitalize).join if mod.const_defined?(elem, false) mod.const_get(elem) else child = i == names.length - 1 ? self : Module.new child.module_eval { include mod } if mod != Plugin # Include parent module mod.const_set(elem, child) end end Plugin.register(path, self) module_eval(File.read(file), file) Olelo.logger.debug("Plugin #{path} successfully loaded") end end |
Class Attribute Details
.dir ⇒ Object
Returns the value of attribute dir.
15 16 17 |
# File 'lib/olelo/plugin.rb', line 15 def dir @dir end |
.disabled ⇒ Object
Returns the value of attribute disabled.
15 16 17 |
# File 'lib/olelo/plugin.rb', line 15 def disabled @disabled end |
.failed ⇒ Object (readonly)
Get failed plugins
18 19 20 |
# File 'lib/olelo/plugin.rb', line 18 def failed @failed end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
104 105 106 |
# File 'lib/olelo/plugin.rb', line 104 def file @file end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
104 105 106 |
# File 'lib/olelo/plugin.rb', line 104 def path @path end |
Class Method Details
.enabled?(path) ⇒ Boolean
Check if plugin is enabled
80 81 82 83 84 85 86 87 |
# File 'lib/olelo/plugin.rb', line 80 def enabled?(path) path.split('/').inject('') do |parent, x| parent /= x return false if disabled.include?(parent) parent end true end |
.for(obj) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/olelo/plugin.rb', line 89 def for(obj) if Module === obj names = obj.name.split('::') mod = Object names.map {|name| mod = mod.const_get(name) }.reverse.each do |m| return m if Plugin === m end elsif Proc === obj return obj.binding.eval('PLUGIN') else raise 'Plugin cannot be found for #{obj}' end end |
.load(path) ⇒ Boolean
Load plugin by path
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/olelo/plugin.rb', line 39 def load(path) path = path.sub(%r{/main$}, '') files = [File.join(@dir, path, 'main.rb'), File.join(@dir, "#{path}.rb")].select {|file| File.file?(file) } if files.size == 2 Olelo.logger.error "Duplicate plugin #{files.join(', ')}" return false end return false if files.empty? if @loaded.include?(path) true elsif @failed.include?(path) || !enabled?(path) false else begin new(path, files.first) true rescue Exception => ex @failed << path if LoadError === ex Olelo.logger.warn "Plugin #{path} could not be loaded due to: #{ex.} (Missing gem?)" else Olelo.logger.error "Plugin #{path} could not be loaded due to: #{ex.}" Olelo.logger.debug ex end @loaded.delete(path) false end end end |
.load_all ⇒ Object
Load all plugins
71 72 73 |
# File 'lib/olelo/plugin.rb', line 71 def load_all Dir[File.join(@dir, '**', '*.rb')].inject(true) {|result,file| load(file[(@dir.size+1)..-4]) && result } end |
.loaded ⇒ Object
Get loaded plugins
21 22 23 |
# File 'lib/olelo/plugin.rb', line 21 def loaded @loaded.values end |
.register(path, plugin) ⇒ Object
31 32 33 |
# File 'lib/olelo/plugin.rb', line 31 def register(path, plugin) @loaded[path] = plugin end |
.start ⇒ void
This method returns an undefined value.
Start plugins
27 28 29 |
# File 'lib/olelo/plugin.rb', line 27 def start @loaded.each_value {|plugin| plugin.start } end |
Instance Method Details
#dependencies(*list) ⇒ Object
Load specified plugins and fail with LoadError if dependencies are missing
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/olelo/plugin.rb', line 164 def dependencies(*list) if !list.empty? raise 'Plugin is already started' if started? @dependencies.merge(list) list.each do |dep| raise(LoadError, "Could not load dependency #{dep} for #{path}") if !Plugin.load(dep) end end @dependencies end |
#setup(&block) ⇒ Object
175 176 177 |
# File 'lib/olelo/plugin.rb', line 175 def setup(&block) @setup = block end |
#start ⇒ Boolean
Start the plugin by calling the #setup method
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/olelo/plugin.rb', line 149 def start return true if @started module_eval(&@setup) if @setup Olelo.logger.debug "Plugin #{path} successfully started" @started = true rescue Exception => ex Olelo.logger.error "Plugin #{path} failed to start due to: #{ex.}" Olelo.logger.error ex false end |