diff options
author | Marcel Molina <marcel@vernix.org> | 2007-03-03 04:50:56 +0000 |
---|---|---|
committer | Marcel Molina <marcel@vernix.org> | 2007-03-03 04:50:56 +0000 |
commit | a7b90b7cdd779fcde2bc3f4c97f69a72ca714440 (patch) | |
tree | 6e9932c8c1b8e88a69612a8bea6ff7f1983ac69a /railties/lib/rails/plugin | |
parent | 9f53e09eb9b4eb2e1711a8e92b7244a1f1301bbc (diff) | |
download | rails-a7b90b7cdd779fcde2bc3f4c97f69a72ca714440.tar.gz rails-a7b90b7cdd779fcde2bc3f4c97f69a72ca714440.tar.bz2 rails-a7b90b7cdd779fcde2bc3f4c97f69a72ca714440.zip |
Move plugin classes into the rails directory to match their namespace
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6293 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties/lib/rails/plugin')
-rw-r--r-- | railties/lib/rails/plugin/loader.rb | 123 | ||||
-rw-r--r-- | railties/lib/rails/plugin/locator.rb | 59 |
2 files changed, 182 insertions, 0 deletions
diff --git a/railties/lib/rails/plugin/loader.rb b/railties/lib/rails/plugin/loader.rb new file mode 100644 index 0000000000..d5ad3aa8e3 --- /dev/null +++ b/railties/lib/rails/plugin/loader.rb @@ -0,0 +1,123 @@ +module Rails + module Plugin + class Loader + include Comparable + attr_reader :initializer, :directory, :name + + class << self + def load(*args) + new(*args).load + end + end + + def initialize(initializer, directory) + @initializer = initializer + @directory = directory + @name = File.basename(directory) + end + + def load + return false if loaded? + report_nonexistant_or_empty_plugin! + add_to_load_path! + register_plugin_as_loaded + evaluate + true + end + + def loaded? + initializer.loaded_plugins.include?(name) + end + + def plugin_path? + File.directory?(directory) && (has_lib_directory? || has_init_file?) + end + + def enabled? + !explicit_plugin_loading_order? || registered? + end + + def registered? + explicit_plugin_loading_order? && registered_plugins.include?(name) + end + + def plugin_does_not_exist!(plugin_name = name) + raise LoadError, "Can not find the plugin named: #{plugin_name}" + end + + private + # The plugins that have been explicitly listed with config.plugins. If this list is nil + # then it means the client does not care which plugins or in what order they are loaded, + # so we load all in alphabetical order. If it is an empty array, we load no plugins, if it is + # non empty, we load the named plugins in the order specified. + def registered_plugins + config.plugins + end + + def explicit_plugin_loading_order? + !registered_plugins.nil? + end + + def report_nonexistant_or_empty_plugin! + plugin_does_not_exist! unless plugin_path? + 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 add_to_load_path! + # Add lib to load path *after* the application lib, to allow + # application libraries to override plugin libraries. + if has_lib_directory? + application_lib_index = $LOAD_PATH.index(application_library_path) || 0 + $LOAD_PATH.insert(application_lib_index + 1, lib_path) + Dependencies.load_paths << lib_path + Dependencies.load_once_paths << lib_path + end + end + + def application_library_path + File.join(RAILS_ROOT, 'lib') + end + + # Allow plugins to reference the current configuration object + def config + initializer.configuration + end + + def register_plugin_as_loaded + initializer.loaded_plugins << name + end + + # Evaluate in init.rb + def evaluate + silence_warnings { eval(IO.read(init_path), binding, init_path)} if has_init_file? + end + + def <=>(other_plugin_loader) + if explicit_plugin_loading_order? + if non_existent_plugin = [self, other_plugin_loader].detect {|plugin| !registered_plugins.include?(plugin.name)} + plugin_does_not_exist!(non_existent_plugin.name) + end + + registered_plugins.index(name) <=> registered_plugins.index(other_plugin_loader.name) + else + name <=> other_plugin_loader.name + end + end + end + end +end diff --git a/railties/lib/rails/plugin/locator.rb b/railties/lib/rails/plugin/locator.rb new file mode 100644 index 0000000000..6c4f2605bb --- /dev/null +++ b/railties/lib/rails/plugin/locator.rb @@ -0,0 +1,59 @@ +module Rails + module Plugin + class Locator + include Enumerable + attr_reader :initializer + + def initialize(initializer) + @initializer = initializer + end + + def plugins + located_plugins.select(&:enabled?).sort + end + + def each(&block) + plugins.each(&block) + end + + def plugin_names + plugins.map(&:name) + end + + private + def located_plugins + raise "The `located_plugins' method must be defined by concrete subclasses of #{self.class}" + end + end + + class FileSystemLocator < Locator + private + def located_plugins + initializer.configuration.plugin_paths.flatten.inject([]) do |plugins, path| + plugins.concat locate_plugins_under(path) + plugins + end.flatten + end + + # This starts at the base path looking for directories that pass the plugin_path? test of the Plugin::Loader. + # Since plugins can be nested arbitrarily deep within an unspecified number of intermediary directories, + # this method runs recursively until it finds a plugin directory. + # + # e.g. + # + # locate_plugins_under('vendor/plugins/acts/acts_as_chunky_bacon') + # => 'acts_as_chunky_bacon' + def locate_plugins_under(base_path) + Dir.glob(File.join(base_path, '*')).inject([]) do |plugins, path| + plugin_loader = initializer.configuration.plugin_loader.new(initializer, path) + if plugin_loader.plugin_path? && plugin_loader.enabled? + plugins << plugin_loader + elsif File.directory?(path) + plugins.concat locate_plugins_under(path) + end + plugins + end + end + end + end +end
\ No newline at end of file |