Skip to content
Browse files

Add alternative server-centric role definition method

git-svn-id: http://svn.rubyonrails.org/rails/tools/capistrano@8926 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 07d777b commit f8ac678ca037e5198e2b5dcba975a19b3bf1ec50 @jamis jamis committed Feb 23, 2008
Showing with 26 additions and 4 deletions.
  1. +2 −0 CHANGELOG
  2. +12 −0 lib/capistrano/configuration/roles.rb
  3. +12 −4 test/configuration/roles_test.rb
View
2 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 <rob@inversepath.com>]
View
12 lib/capistrano/configuration/roles.rb
@@ -48,6 +48,18 @@ def role(which, *args, &block)
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
View
16 test/configuration/roles_test.rb
@@ -17,10 +17,6 @@ def setup
@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 @@ def test_role_block_can_return_empty_array
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

0 comments on commit f8ac678

Please sign in to comment.
Something went wrong with that request. Please try again.