From f5b991d76dc5d21f1870da067fb3101440256fba Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 27 Mar 2008 17:47:51 +0000 Subject: 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 --- railties/CHANGELOG | 6 ++++++ railties/lib/rails/plugin.rb | 22 +++++++++++++++++++--- railties/test/plugin_loader_test.rb | 23 +++++++++-------------- railties/test/plugin_test.rb | 18 ++++++++++++++++++ 4 files changed, 52 insertions(+), 17 deletions(-) diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 048ee103a3..ac17d7a0ad 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -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] diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index be392195d4..03fa309ca3 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -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,6 +10,12 @@ module Rails # These methods are expected by the Rails::Plugin::Locator and Rails::Plugin::Loader classes. # The default implementation returns the lib directory as its load_paths, # and evaluates init.rb when load 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 @@ -18,8 +23,8 @@ module Rails 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 @@ module Rails 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}" diff --git a/railties/test/plugin_loader_test.rb b/railties/test/plugin_loader_test.rb index ec420784a3..30eeaadf12 100644 --- a/railties/test/plugin_loader_test.rb +++ b/railties/test/plugin_loader_test.rb @@ -14,6 +14,8 @@ uses_mocha "Plugin Loader Tests" do @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 @@ uses_mocha "Plugin Loader Tests" do 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 @@ uses_mocha "Plugin Loader Tests" do 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 diff --git a/railties/test/plugin_test.rb b/railties/test/plugin_test.rb index 32c72f1b31..1445338f14 100644 --- a/railties/test/plugin_test.rb +++ b/railties/test/plugin_test.rb @@ -130,9 +130,27 @@ uses_mocha "Plugin Tests" do 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 -- cgit v1.2.3