Class: Butler::Plugins

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Log::Comfort
Defined in:
lib/butler/plugins.rb

Overview

Plugins manages the plugins in Butler It uses

Constant Summary collapse

Suffix =
".rb"

Instance Attribute Summary collapse

Attributes included from Log::Comfort

#logger

Instance Method Summary collapse

Methods included from Log::Comfort

#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.

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
# File 'lib/butler/plugins.rb', line 29

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
	@logger    = butler.logger
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



28
29
30
# File 'lib/butler/plugins.rb', line 28

def dir
  @dir
end

Instance Method Details

#[](base) ⇒ Object



73
74
75
# File 'lib/butler/plugins.rb', line 73

def [](base)
	@plugins[base]
end

#activeObject

returns a list with the names of active plugins



61
62
63
# File 'lib/butler/plugins.rb', line 61

def active
	@plugins.keys
end

#allObject

returns a list with the bases of all plugins



53
54
55
56
57
58
# File 'lib/butler/plugins.rb', line 53

def all
	slice = @dir.length+1..-(Suffix.length+1)
	Dir["#{@dir}/**/*#{Suffix}"].select { |file| File.file?(file) }.map { |file|
		file[slice]
	}
end

#groupsObject

returns a list with first-level groups



39
40
41
42
43
44
45
46
# File 'lib/butler/plugins.rb', line 39

def groups
	slice = @dir.length+1..-1
	Dir[@dir+"/*"].reject { |file|
		File.extname(file).downcase == Suffix
	}.map { |file|
		file[slice]
	}
end

#identify(name) ⇒ Object



77
78
79
80
# File 'lib/butler/plugins.rb', line 77

def identify(name)
	plugin = Dir["#{@dir}/**/#{name}#{Suffix}"].first
	plugin && plugin[@dir.length+1..-(Suffix.length+1)]
end

#inactiveObject



65
66
67
# File 'lib/butler/plugins.rb', line 65

def inactive
	all-active
end

#instancesObject



48
49
50
# File 'lib/butler/plugins.rb', line 48

def instances
	@plugins.values
end

#load(base) ⇒ Object Also known as: reload



95
96
97
98
99
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
# File 'lib/butler/plugins.rb', line 95

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) # a plugin may not be loaded twice
	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.logger = @butler.logger
	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_allObject



82
83
84
85
86
# File 'lib/butler/plugins.rb', line 82

def load_all
	all.each { |base|
		load(base)
	}
end

#load_group(group) ⇒ Object



88
89
90
91
92
93
# File 'lib/butler/plugins.rb', line 88

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

Returns:

  • (Boolean)


69
70
71
# File 'lib/butler/plugins.rb', line 69

def loaded?(base)
	@plugins.has_key?(base)
end

#unload(base) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/butler/plugins.rb', line 127

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