Module: Strelka::Plugin

Overview

Plugin Module extension -- adds registration, load-order support, etc.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pluggableObject

The Class/Module that this plugin belongs to



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

def pluggable
  @pluggable
end

#successorsObject

An Array that tracks which plugins should be installed after itself.



74
75
76
# File 'lib/strelka/plugins.rb', line 74

def successors
  @successors
end

Class Method Details

.extended(object) ⇒ Object

Extension hook -- Extend the given object with methods for setting it up as a plugin for its containing namespace.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/strelka/plugins.rb', line 40

def self::extended( object )
	super
	object.extend( Loggability )
	object.log_to( :strelka )

	# Find the plugin's namespace container, which will be the
	# pluggable class/module
	pluggable_name = object.name.split( '::' )[ 0..-2 ]
	pluggable = pluggable_name.inject( Object ) do |mod, name|
		mod.const_get( name )
	end

	self.log.debug "Extending %p as a Strelka::Plugin for %p" % [ object, pluggable ]
	object.successors = Set.new
	object.pluggable = pluggable

	# Register any pending dependencies for the newly-loaded plugin
	name = object.plugin_name
	if (( deps = pluggable.loaded_plugins[name] ))
		self.log.debug "  installing deferred deps for %p" % [ name ]
		object.run_inside( *deps )
	end

	self.log.debug "  adding %p (%p) to the plugin registry for %p" %
		[ name, object, pluggable ]
	pluggable.loaded_plugins[ name ] = object
end

Instance Method Details

#plugin_nameObject

Return the name of the receiving plugin



81
82
83
84
85
# File 'lib/strelka/plugins.rb', line 81

def plugin_name
	name = self.name || "anonymous#{self.object_id}"
	name = name.sub( /.*::/, '' )
	return name.downcase.to_sym
end

#run_inside(*other_plugins) ⇒ Object Also known as: run_after

Register the receiver as needing to be run after other_plugins for requests, and before them for responses.



109
110
111
112
# File 'lib/strelka/plugins.rb', line 109

def run_inside( *other_plugins )
	self.log.debug "  %p will run after %p" % [ self, other_plugins ]
	self.successors.merge( other_plugins )
end

#run_outside(*other_plugins) ⇒ Object Also known as: run_before

Register the receiver as needing to be run before other_plugins for requests, and after them for responses.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/strelka/plugins.rb', line 90

def run_outside( *other_plugins )
	name = self.plugin_name
	other_plugins.each do |other_name|
		self.pluggable.loaded_plugins[ other_name ] ||= []
		mod = self.pluggable.loaded_plugins[ other_name ]

		if mod.respond_to?( :run_inside )
			mod.run_inside( name )
		else
			self.log.debug "%p plugin not yet loaded; setting up pending deps" % [ other_name ]
			mod << name
		end
	end
end