Permalink
Browse files

Added Plugin#about method to programmatically access the about.yml in…

… a plugin (closes #10979) [lazyatom]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9098 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 35d3ede commit f5b991d76dc5d21f1870da067fb3101440256fba @dhh dhh committed Mar 27, 2008
Showing with 52 additions and 17 deletions.
  1. +6 −0 railties/CHANGELOG
  2. +19 −3 railties/lib/rails/plugin.rb
  3. +9 −14 railties/test/plugin_loader_test.rb
  4. +18 −0 railties/test/plugin_test.rb
View
@@ -1,5 +1,11 @@
*SVN*
+* Added Plugin#about method to programmatically access the about.yml in a plugin #10979 [lazyatom]
+
+ plugin = Rails::Plugin.new(path_to_my_plugin)
+ plugin.about["author"] # => "James Adam"
+ plugin.about["url"] # => "http://interblah.net"
+
* Improve documentation. [Radar, Jan De Poorter, chuyeow, xaviershay, danger, miloops, Xavier Noria, Sunny Ripert]
* Added config.time_zone = 'UTC' in the default environment.rb [Geoff Buesing]
@@ -1,5 +1,4 @@
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
@@ -11,15 +10,21 @@ module Rails
# 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.
+ #
+ # You can also inspect the about.yml data programmatically:
+ #
+ # plugin = Rails::Plugin.new(path_to_my_plugin)
+ # plugin.about["author"] # => "James Adam"
+ # plugin.about["url"] # => "http://interblah.net"
class Plugin
include Comparable
attr_reader :directory, :name
def initialize(directory)
@directory = directory
- @name = File.basename(@directory) rescue nil
- @loaded = false
+ @name = File.basename(@directory) rescue nil
+ @loaded = false
end
def valid?
@@ -47,8 +52,19 @@ def loaded?
def <=>(other_plugin)
name <=> other_plugin.name
end
+
+ def about
+ @about ||= load_about_information
+ end
private
+ def load_about_information
+ about_yml_path = File.join(@directory, "about.yml")
+ parsed_yml = File.exist?(about_yml_path) ? YAML.load(File.read(about_yml_path)) : {}
+ parsed_yml || {}
+ rescue Exception
+ {}
+ end
def report_nonexistant_or_empty_plugin!
raise LoadError, "Can not find the plugin named: #{name}"
@@ -14,6 +14,8 @@ def setup
@valid_plugin_path = plugin_fixture_path('default/stubby')
@empty_plugin_path = plugin_fixture_path('default/empty')
+ @failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
+
@loader = Rails::Plugin::Loader.new(@initializer)
end
@@ -39,19 +41,16 @@ def test_should_return_empty_array_if_configuration_plugins_is_empty
end
def test_should_find_all_availble_plugins_and_return_as_all_plugins
- failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
- assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @loader.all_plugins, failure_tip
+ assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @loader.all_plugins, @failure_tip
end
def test_should_return_all_plugins_as_plugins_when_registered_plugin_list_is_untouched
- failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
- assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @loader.plugins, failure_tip
+ assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip
end
def test_should_return_all_plugins_as_plugins_when_registered_plugin_list_is_nil
@configuration.plugins = nil
- failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
- assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @loader.plugins, failure_tip
+ assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip
end
def test_should_return_specific_plugins_named_in_config_plugins_array_if_set
@@ -68,26 +67,22 @@ def test_should_respect_the_order_of_plugins_given_in_configuration
def test_should_load_all_plugins_in_natural_order_when_all_is_used
only_load_the_following_plugins! [:all]
- failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
- assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @loader.plugins, failure_tip
+ assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip
end
def test_should_load_specified_plugins_in_order_and_then_all_remaining_plugins_when_all_is_used
only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon, :all]
- failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
- assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :plugin_with_no_lib_dir], @loader.plugins, failure_tip
+ assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :plugin_with_no_lib_dir], @loader.plugins, @failure_tip
end
def test_should_be_able_to_specify_loading_of_plugins_loaded_after_all
only_load_the_following_plugins! [:stubby, :all, :acts_as_chunky_bacon]
- failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
- assert_plugins [:stubby, :a, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @loader.plugins, failure_tip
+ assert_plugins [:stubby, :a, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @loader.plugins, @failure_tip
end
def test_should_accept_plugin_names_given_as_strings
only_load_the_following_plugins! ['stubby', 'acts_as_chunky_bacon', :a, :plugin_with_no_lib_dir]
- failure_tip = "It's likely someone has added a new plugin fixture without updating this list"
- assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :plugin_with_no_lib_dir], @loader.plugins, failure_tip
+ assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :plugin_with_no_lib_dir], @loader.plugins, @failure_tip
end
def test_should_add_plugin_load_paths_to_global_LOAD_PATH_array
@@ -130,9 +130,27 @@ def test_should_only_be_loaded_once
end
assert plugin.loaded?
end
+
+ def test_should_make_about_yml_available_as_about_method_on_plugin
+ plugin = plugin_for(@valid_plugin_path)
+ assert_equal "Plugin Author", plugin.about['author']
+ assert_equal "1.0.0", plugin.about['version']
+ end
+
+ def test_should_return_empty_hash_for_about_if_about_yml_is_missing
+ assert_equal({}, plugin_for(about_yml_plugin_path('plugin_without_about_yaml')).about)
+ end
+
+ def test_should_return_empty_hash_for_about_if_about_yml_is_malformed
+ assert_equal({}, plugin_for(about_yml_plugin_path('bad_about_yml')).about)
+ end
private
+ def about_yml_plugin_path(name)
+ File.join(File.dirname(__FILE__), 'fixtures', 'about_yml_plugins', name)
+ end
+
def plugin_for(path)
Rails::Plugin.new(path)
end

0 comments on commit f5b991d

Please sign in to comment.