Module: Module::Pluggable

Defined in:
lib/module/pluggable.rb

Defined Under Namespace

Classes: Plugins

Constant Summary collapse

DEFAULT_OPTS =
{
	:search_path => "plugins",
	:except      => /_$/,
	:base_class  => nil,
}.freeze

Instance Method Summary collapse

Instance Method Details

#pluggable(name = :plugins, o = {}) ⇒ Object

pluggable make the name of instance method returning instance of Module::Pluggable::Plugin.

All loaded plugins are in anonymous module, so you can’t access the classes directly, and you can create some plugin-sets without confusing class variables etc.

opts => {
    :search_path => name,
    :base_class => nil,
    :except => /_$/, # not yet
}


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/module/pluggable.rb', line 27

def pluggable(name=:plugins, o={})
	opts = DEFAULT_OPTS.merge(o)
	opts[:search_path] = name ? name.to_s : opts[:name].to_s unless opts[:search_path]

	class_eval <<-EOS
		def #{name}
			@#{name} ||= Module::Pluggable::Plugins.new(@@pluggable_opts[:#{name}])
		end

		def self.set_pluggable_opts(name, opts)
			(@@pluggable_opts ||= {})[name] = opts
		end
	EOS
	self.set_pluggable_opts(name, opts)
	(class << self; self; end).instance_eval do
		remove_method(:set_pluggable_opts)
	end
end