Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Plugin Loader. Add plugin lib paths early, and add lots of t…
…ests. Closes #9795 [lazyatom] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8115 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
- Loading branch information
1 parent
c443a61
commit f1b1af8
Showing
10 changed files
with
679 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
module Rails | ||
|
||
# The Plugin class should be an object which provides the following methods: | ||
# | ||
# * +name+ - used during initialisation to order the plugin (based on name and | ||
# the contents of <tt>config.plugins</tt>) | ||
# * +valid?+ - returns true if this plugin can be loaded | ||
# * +load_paths+ - each path within the returned array will be added to the $LOAD_PATH | ||
# * +load+ - finally 'load' the plugin. | ||
# | ||
# These methods are expected by the Rails::Plugin::Locator and Rails::Plugin::Loader classes. | ||
# The default implementation returns the <tt>lib</tt> directory as its </tt>load_paths</tt>, | ||
# and evaluates <tt>init.rb</tt> when <tt>load</tt> is called. | ||
class Plugin | ||
include Comparable | ||
|
||
attr_reader :directory, :name | ||
|
||
def initialize(directory) | ||
@directory = directory | ||
@name = File.basename(@directory) rescue nil | ||
@loaded = false | ||
end | ||
|
||
def valid? | ||
File.directory?(directory) && (has_lib_directory? || has_init_file?) | ||
end | ||
|
||
# Returns a list of paths this plugin wishes to make available in $LOAD_PATH | ||
def load_paths | ||
report_nonexistant_or_empty_plugin! unless valid? | ||
has_lib_directory? ? [lib_path] : [] | ||
end | ||
|
||
# Evaluates a plugin's init.rb file | ||
def load(initializer) | ||
report_nonexistant_or_empty_plugin! unless valid? | ||
evaluate_init_rb(initializer) | ||
@loaded = true | ||
end | ||
|
||
def loaded? | ||
@loaded | ||
end | ||
|
||
def <=>(other_plugin) | ||
name <=> other_plugin.name | ||
end | ||
|
||
private | ||
|
||
def report_nonexistant_or_empty_plugin! | ||
raise LoadError, "Can not find the plugin named: #{name}" | ||
end | ||
|
||
def lib_path | ||
File.join(directory, 'lib') | ||
end | ||
|
||
def init_path | ||
File.join(directory, 'init.rb') | ||
end | ||
|
||
def has_lib_directory? | ||
File.directory?(lib_path) | ||
end | ||
|
||
def has_init_file? | ||
File.file?(init_path) | ||
end | ||
|
||
def evaluate_init_rb(initializer) | ||
if has_init_file? | ||
silence_warnings do | ||
# Allow plugins to reference the current configuration object | ||
config = initializer.configuration | ||
|
||
eval(IO.read(init_path), binding, init_path) | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.