2 # The Plugin class should be an object which provides the following methods:
4 # * +name+ - Used during initialisation to order the plugin (based on name and
5 # the contents of <tt>config.plugins</tt>).
6 # * +valid?+ - Returns true if this plugin can be loaded.
7 # * +load_paths+ - Each path within the returned array will be added to the <tt>$LOAD_PATH</tt>.
8 # * +load+ - Finally 'load' the plugin.
10 # These methods are expected by the Rails::Plugin::Locator and Rails::Plugin::Loader classes.
11 # The default implementation returns the <tt>lib</tt> directory as its <tt>load_paths</tt>,
12 # and evaluates <tt>init.rb</tt> when <tt>load</tt> is called.
14 # You can also inspect the about.yml data programmatically:
16 # plugin = Rails::Plugin.new(path_to_my_plugin)
17 # plugin.about["author"] # => "James Adam"
18 # plugin.about["url"] # => "http://interblah.net"
22 attr_reader
:directory, :name
24 def initialize(directory
)
25 @directory = directory
26 @name = File
.basename(@directory) rescue nil
31 File
.directory
?(directory
) && (has_app_directory
? || has_lib_directory
? || has_init_file
?)
34 # Returns a list of paths this plugin wishes to make available in <tt>$LOAD_PATH</tt>.
36 report_nonexistant_or_empty_plugin
! unless valid
?
38 returning
[] do |load_paths
|
39 load_paths
<< lib_path
if has_lib_directory
?
40 load_paths
<< app_paths
if has_app_directory
?
44 # Evaluates a plugin's init.rb file.
47 report_nonexistant_or_empty_plugin
! unless valid
?
48 evaluate_init_rb(initializer
)
57 name
<=> other_plugin
.name
61 @about ||= load_about_information
64 # Engines are plugins with an app/ directory.
69 # Returns true if the engine ships with a routing file
71 File
.exist
?(routing_file
)
76 File
.join(directory
, 'app', 'views')
80 File
.join(directory
, 'app', 'controllers')
84 File
.join(directory
, 'app', 'metal')
88 File
.join(directory
, 'config', 'routes.rb')
93 def load_about_information
94 about_yml_path
= File
.join(@directory, "about.yml")
95 parsed_yml
= File
.exist
?(about_yml_path
) ? YAML
.load(File
.read(about_yml_path
)) : {}
101 def report_nonexistant_or_empty_plugin
!
102 raise LoadError
, "Can not find the plugin named: #{name}"
107 [ File
.join(directory
, 'app', 'models'), File
.join(directory
, 'app', 'helpers'), controller_path
, metal_path
]
111 File
.join(directory
, 'lib')
114 def classic_init_path
115 File
.join(directory
, 'init.rb')
119 File
.join(directory
, 'rails', 'init.rb')
123 File
.file
?(gem_init_path
) ? gem_init_path
: classic_init_path
127 def has_app_directory
?
128 File
.directory
?(File
.join(directory
, 'app'))
131 def has_lib_directory
?
132 File
.directory
?(lib_path
)
136 File
.file
?(init_path
)
140 def evaluate_init_rb(initializer
)
143 # Allow plugins to reference the current configuration object
144 config
= initializer
.configuration
146 eval(IO
.read(init_path
), binding
, init_path
)
152 # This Plugin subclass represents a Gem plugin. Although RubyGems has already
153 # taken care of $LOAD_PATHs, it exposes its load_paths to add them
154 # to Dependencies.load_paths.
155 class GemPlugin
< Plugin
156 # Initialize this plugin from a Gem::Specification.
157 def initialize(spec
, gem
)
158 directory
= spec
.full_gem_path
164 File
.join(directory
, 'rails', 'init.rb')