Class: Butler::Plugins
Overview
Plugins manages the plugins in Butler It uses
Constant Summary
collapse
- Suffix =
".rb"
Instance Attribute Summary collapse
Attributes included from Log::Comfort
#log_device
Instance Method Summary
collapse
#debug, #error, #exception, #fail, #info, #log, #warn
Methods included from Enumerable
#join
Constructor Details
#initialize(butler, plugin_dir) ⇒ Plugins
Returns a new instance of Plugins.
35
36
37
38
39
40
41
|
# File 'lib/butler/plugins.rb', line 35
def initialize(butler, plugin_dir)
@dir = File.expand_path(plugin_dir).freeze
raise ArgumentError, "#{@dir} is not a directory" unless File.directory?(@dir)
@plugins = {}
@constants = {}
@butler = butler
end
|
Instance Attribute Details
#dir ⇒ Object
Returns the value of attribute dir.
34
35
36
|
# File 'lib/butler/plugins.rb', line 34
def dir
@dir
end
|
Instance Method Details
#[](base) ⇒ Object
78
79
80
|
# File 'lib/butler/plugins.rb', line 78
def [](base)
@plugins[base]
end
|
#active ⇒ Object
returns a list with the names of active plugins
66
67
68
|
# File 'lib/butler/plugins.rb', line 66
def active
@plugins.keys
end
|
#all ⇒ Object
returns a list with the bases of all plugins
58
59
60
61
62
63
|
# File 'lib/butler/plugins.rb', line 58
def all
slice = @dir.length+1..-(Suffix.length+1)
Dir["#{@dir}/**/*#{Suffix}"].select { |file| File.file?(file) }.map { |file|
file[slice]
}
end
|
#groups ⇒ Object
returns a list with first-level groups
44
45
46
47
48
49
50
51
|
# File 'lib/butler/plugins.rb', line 44
def groups
slice = @dir.length+1..-1
Dir[@dir+"/*"].reject { |file|
File.extname(file).downcase == Suffix
}.map { |file|
file[slice]
}
end
|
#identify(name) ⇒ Object
82
83
84
85
|
# File 'lib/butler/plugins.rb', line 82
def identify(name)
plugin = Dir["#{@dir}/**/*#{name}#{Suffix}"].first
plugin && plugin[@dir.length+1..-(Suffix.length+1)]
end
|
#inactive ⇒ Object
70
71
72
|
# File 'lib/butler/plugins.rb', line 70
def inactive
all-active
end
|
#instances ⇒ Object
53
54
55
|
# File 'lib/butler/plugins.rb', line 53
def instances
@plugins.values
end
|
#load(base) ⇒ Object
Also known as:
reload
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/butler/plugins.rb', line 100
def load(base)
path = "#{@dir}/#{base.downcase}#{Suffix}"
name = File.basename(base).camelcase
codefile = path
unless File.file?(codefile) then
raise ArgumentError, "Invalid plugin '#{base}' (codefile: #{codefile})."
end
begin
unload(base) if loaded?(base) rescue Exception => e
exception(e)
end
begin
constant = "%s_%08X" % [name, rand(0xffffffff)]
end while Butler::Plugins.const_defined?(constant)
plugin = Butler::Plugins.const_set(constant, Class.new(Plugin))
plugin.log_device = @butler.log_device
begin
plugin.load_plugin(@butler, base, path)
plugin.class_eval(File.read(codefile), codefile)
plugin.on_load
rescue Exception => e
e.extend Exception::Detailed
e.prepend "Loading plugin #{base} failed."
exception(e)
end
@constants[base] = constant.freeze
@plugins[base] = plugin
end
|
#load_all ⇒ Object
87
88
89
90
91
|
# File 'lib/butler/plugins.rb', line 87
def load_all
all.each { |base|
load(base)
}
end
|
#load_group(group) ⇒ Object
93
94
95
96
97
98
|
# File 'lib/butler/plugins.rb', line 93
def load_group(group)
slice = @dir.length+1..-(Suffix.length+1)
Dir["#{@dir}/#{group}/**/*#{Suffix}"].select { |file| File.file?(file) }.each { |file|
load(file[slice])
}
end
|
#loaded?(base) ⇒ Boolean
74
75
76
|
# File 'lib/butler/plugins.rb', line 74
def loaded?(base)
@plugins.has_key?(base)
end
|
#unload(base) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/butler/plugins.rb', line 132
def unload(base)
begin
@plugins[base].on_unload
@plugins[base].unload_plugin
rescue Exception => e
e.extend Exception::Detailed
e.prepend "Exception raised while unloading plugin #{base}."
exception(e)
end
Butler::Plugins.send(:remove_const, @constants[base])
@plugins.delete(base)
@constants.delete(base)
true
end
|