3 # The Plugin class should be an object which provides the following methods:
5 # * +name+ - used during initialisation to order the plugin (based on name and
6 # the contents of <tt>config.plugins</tt>)
7 # * +valid?+ - returns true if this plugin can be loaded
8 # * +load_paths+ - each path within the returned array will be added to the $LOAD_PATH
9 # * +load+ - finally 'load' the plugin.
11 # These methods are expected by the Rails::Plugin::Locator and Rails::Plugin::Loader classes.
12 # The default implementation returns the <tt>lib</tt> directory as its </tt>load_paths</tt>,
13 # and evaluates <tt>init.rb</tt> when <tt>load</tt> is called.
17 attr_reader :directory, :name
19 def initialize(directory)
20 @directory = directory
21 @name = File.basename(@directory) rescue nil
26 File.directory?(directory) && (has_lib_directory? || has_init_file?)
29 # Returns a list of paths this plugin wishes to make available in $LOAD_PATH
31 report_nonexistant_or_empty_plugin! unless valid?
32 has_lib_directory? ? [lib_path] : []
35 # Evaluates a plugin's init.rb file
38 report_nonexistant_or_empty_plugin! unless valid?
39 evaluate_init_rb(initializer)
48 name <=> other_plugin.name
53 def report_nonexistant_or_empty_plugin!
54 raise LoadError, "Can not find the plugin named: #{name}"
58 File.join(directory, 'lib')
62 File.join(directory, 'init.rb')
65 def has_lib_directory?
66 File.directory?(lib_path)
73 def evaluate_init_rb(initializer)
76 # Allow plugins to reference the current configuration object
77 config = initializer.configuration
79 eval(IO.read(init_path), binding, init_path)