Class: PluginManager

Inherits:
Object
  • Object
show all
Defined in:
lib/zmb/plugin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate = nil) ⇒ PluginManager

Returns a new instance of PluginManager.



4
5
6
7
8
# File 'lib/zmb/plugin.rb', line 4

def initialize(delegate=nil)
  @delegate = delegate
  @plugins = Array.new
  @plugin_sources = Array.new
end

Instance Attribute Details

#plugin_sourcesObject

Returns the value of attribute plugin_sources.



2
3
4
# File 'lib/zmb/plugin.rb', line 2

def plugin_sources
  @plugin_sources
end

#pluginsObject

Returns the value of attribute plugins.



2
3
4
# File 'lib/zmb/plugin.rb', line 2

def plugins
  @plugins
end

Instance Method Details

#add_plugin_source(directory) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/zmb/plugin.rb', line 30

def add_plugin_source(directory)
  @plugin_sources << directory
  
  definition_files = Dir[
    File.join(File.expand_path(directory), "*.rb"),
    File.join(File.expand_path(directory), "*", "plugin.rb")
  ]
  
  definition_files.map do |file|
    load_plugin_source(file)
  end
end

#load_plugin_source(file) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/zmb/plugin.rb', line 20

def load_plugin_source(file)
  begin
    definition = instance_eval(File.read(file))
    definition.definitition_file = File.expand_path(file)
    @plugins << definition
  rescue Exception
    @delegate.debug(self, "Cannot load source `#{file}`", $!)
  end
end

#plugin(name) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/zmb/plugin.rb', line 10

def plugin(name)
  plugin = @plugins.find{|plugin| plugin.name == name}
  
  if plugin then
    plugin.object
  else
    nil
  end
end

#refresh_plugin_sourcesObject



43
44
45
46
47
48
49
50
# File 'lib/zmb/plugin.rb', line 43

def refresh_plugin_sources
  sources = @plugin_sources
  
  @plugins = Array.new
  @plugin_sources = Array.new
  
  sources.each{|directory| add_plugin_source directory}
end

#reload_plugin(name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/zmb/plugin.rb', line 52

def reload_plugin(name)
  plugin = @plugins.find{|plugin| plugin.name == name}
  
  if plugin then
    @plugins.delete(plugin)
    reloaded = load_plugin_source plugin.definitition_file
    @plugins << plugin if not reloaded
    reloaded
  end
end