From f8ac678ca037e5198e2b5dcba975a19b3bf1ec50 Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Sat, 23 Feb 2008 04:15:16 +0000 Subject: [PATCH] Add alternative server-centric role definition method git-svn-id: http://svn.rubyonrails.org/rails/tools/capistrano@8926 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- CHANGELOG | 2 ++ lib/capistrano/configuration/roles.rb | 12 ++++++++++++ test/configuration/roles_test.rb | 16 ++++++++++++---- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index dff66929..f46284ec 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Add alternative server-centric role definition method [James Duncan Davidson] + * Add support for password prompts from the Mercurial SCM [ches] * Add support for :max_hosts option in task definition or run() [Rob Holland ] diff --git a/lib/capistrano/configuration/roles.rb b/lib/capistrano/configuration/roles.rb index 83d08b56..ffcb495d 100644 --- a/lib/capistrano/configuration/roles.rb +++ b/lib/capistrano/configuration/roles.rb @@ -48,6 +48,18 @@ module Capistrano roles[which].push(block, options) if block_given? args.each { |host| roles[which] << ServerDefinition.new(host, options) } end + + # An alternative way to associate servers with roles. If you have a server + # that participates in multiple roles, this can be a DRYer way to describe + # the relationships. Pass the host definition as the first parameter, and + # the roles as the remaining parameters: + # + # server "master.example.com", :web, :app + def server(host, *roles) + options = roles.last.is_a?(Hash) ? roles.pop : {} + raise ArgumentError, "you must associate a server with at least one role" if roles.empty? + roles.each { |name| role(name, host, options) } + end end end end diff --git a/test/configuration/roles_test.rb b/test/configuration/roles_test.rb index 8b0ecfd3..fe6c05ae 100644 --- a/test/configuration/roles_test.rb +++ b/test/configuration/roles_test.rb @@ -17,10 +17,6 @@ class ConfigurationRolesTest < Test::Unit::TestCase @config = MockConfig.new end - def assert_role_equals(list) - assert_equal list, @config.roles[:app].map { |s| s.host } - end - def test_initialize_should_initialize_roles_collection assert @config.original_initialize_called assert @config.roles.empty? @@ -132,4 +128,16 @@ class ConfigurationRolesTest < Test::Unit::TestCase end assert_role_equals ([]) end + + def test_role_definitions_via_server_should_associate_server_with_roles + @config.server "www.capistrano.test", :web, :app + assert_equal %w(www.capistrano.test), @config.roles[:app].map { |s| s.host } + assert_equal %w(www.capistrano.test), @config.roles[:web].map { |s| s.host } + end + + private + + def assert_role_equals(list) + assert_equal list, @config.roles[:app].map { |s| s.host } + end end