Module: Mystro::Plugin
- Defined in:
- lib/mystro/plugin.rb
Defined Under Namespace
Modules: Base
Class Method Summary collapse
- .disabled?(name) ⇒ Boolean
- .jobs ⇒ Object
- .load(plugins = { }) ⇒ Object
- .load_plugin(plugin, data) ⇒ Object
- .on(klass, event, &block) ⇒ Object
- .register(key, opts = {}) ⇒ Object
- .run(event, *args) ⇒ Object
- .schedule ⇒ Object
- .ui ⇒ Object
Class Method Details
.disabled?(name) ⇒ Boolean
54 55 56 57 |
# File 'lib/mystro/plugin.rb', line 54 def disabled?(name) Mystro::Log.debug "disabled? #{name}" Mystro.config.plugins && Mystro.config.plugins.disabled && Mystro.config.plugins.disabled[name] end |
.jobs ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/mystro/plugin.rb', line 79 def jobs @jobs ||= begin jobs = {} (@plugins||{}).each do |key, opts| if opts[:jobs] jobs[key] = opts[:jobs] end end jobs end end |
.load(plugins = { }) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/mystro/plugin.rb', line 26 def load(plugins={ }) list = plugins.reject {|k, v| k == 'disabled'} list.each do |plugin, data| load_plugin(plugin, data) end end |
.load_plugin(plugin, data) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mystro/plugin.rb', line 33 def load_plugin(plugin, data) begin # load library, if it exists # this is for plugins in gems require "mystro/plugin/#{plugin}" rescue => e Mystro::Log.debug "load plugin: #{plugin}: #{e.}" end begin # load file in config directory if it exists file = "#{Mystro.directory}/plugins/#{plugin}" if File.exists?("#{file}.rb") Mystro::Log.debug "plugin: #{plugin} = #{file}" require file end rescue LoadError, StandardError => e Mystro::Log.error "load plugin: #{plugin}: #{e.}" end end |
.on(klass, event, &block) ⇒ Object
21 22 23 24 |
# File 'lib/mystro/plugin.rb', line 21 def on(klass, event, &block) @hooks ||= [] @hooks << { :event => event, :class => klass, :block => block } end |
.register(key, opts = {}) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/mystro/plugin.rb', line 59 def register(key, opts={}) @plugins ||= {} @plugins[key] = opts name = key.to_s.capitalize @plugins[key][:name] = name end |
.run(event, *args) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/mystro/plugin.rb', line 4 def run(event, *args) return if Mystro.config.mock @hooks ||= [] @hooks.select { |e| e[:event] == event }.each do |plugin| klass = plugin[:class] block = plugin[:block] begin Mystro::Log.debug "calling #{klass} :: #{event}" block.call(args.dup) rescue => e Mystro::Log.error "failed to run event #{event} for #{klass}: #{e.}" Mystro::Log.debug e end end end |
.schedule ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/mystro/plugin.rb', line 91 def schedule @schedule ||= begin s = {} (@plugins||{}).each do |key, opts| if opts[:schedule] opts[:schedule].each do |w, o| n = w.to_s.capitalize + "Worker" s[n] = { "cron" => o} end end end s end end |
.ui ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mystro/plugin.rb', line 66 def ui @ui ||= begin ui = {} (@plugins||{}).each do |key, opts| if opts[:ui] n = opts[:name] ui[n] = opts[:ui] end end ui end end |