Class: PhusionPassenger::Plugin

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

Constant Summary collapse

@@hooks =
{}
@@classes =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *args, &block) ⇒ Plugin

Returns a new instance of Plugin.



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/phusion_passenger/plugin.rb', line 71

def initialize(name, *args, &block)
	Plugin.load(name)
	classes = @@classes[name]
	if classes
		@instances = classes.map do |klass|
			klass.new(*args, &block)
		end
	else
		return nil
	end
end

Class Method Details

.call_hook(name, *args, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/phusion_passenger/plugin.rb', line 56

def self.call_hook(name, *args, &block)
	last_result = nil
	if (hooks_list = @@hooks[name])
		hooks_list.each do |callback|
			last_result = callback.call(*args, &block)
		end
	end
	return last_result
end

.load(name, load_once = true) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/phusion_passenger/plugin.rb', line 33

def self.load(name, load_once = true)
	PLUGIN_DIRS.each do |plugin_dir|
		if plugin_dir =~ /\A~/
			# File.expand_path uses ENV['HOME'] which we don't want.
			home = Etc.getpwuid(Process.uid).dir
			plugin_dir = plugin_dir.sub(/\A~/, home)
		end
		plugin_dir = File.expand_path(plugin_dir)
		Dir["#{plugin_dir}/*/#{name}.rb"].each do |filename|
			if load_once
				require(filename)
			else
				load(filename)
			end
		end
	end
end

.register(name, klass) ⇒ Object



66
67
68
69
# File 'lib/phusion_passenger/plugin.rb', line 66

def self.register(name, klass)
	classes = (@@classes[name] ||= [])
	classes << klass
end

.register_hook(name, &block) ⇒ Object



51
52
53
54
# File 'lib/phusion_passenger/plugin.rb', line 51

def self.register_hook(name, &block)
	hooks_list = (@@hooks[name] ||= [])
	hooks_list << block
end

Instance Method Details

#call_hook(name, *args, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/phusion_passenger/plugin.rb', line 83

def call_hook(name, *args, &block)
	last_result = nil
	if @instances
		@instances.each do |instance|
			if instance.respond_to?(name.to_sym)
				last_result = instance.__send__(name.to_sym, *args, &block)
			end
		end
	end
	return last_result
end