Created
December 29, 2008 17:03
-
-
Save danieroux/41310 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Supporting class for demo purposes | |
class DatabaseStub | |
attr_reader :name | |
def self.instances | |
["development", "test", "ci"] | |
end | |
def initialize(databaseName) | |
@name = databaseName | |
end | |
def running? | |
@name == "development" | |
end | |
def method_missing(sym, *args, &block) | |
puts "Action '#{sym}' would now be performed on database '#@name'" | |
end | |
end | |
# Define our own task type | |
def task_database(database, action) | |
desc "#{action.to_s} - #{database.name}" | |
task action do | |
# Use message sending to just pass on the action to the | |
# instance. | |
database.send(action) | |
end | |
end | |
# Define the tasks | |
DatabaseStub.instances.each do |databaseName| | |
namespace databaseName do | |
database = DatabaseStub.new(databaseName) | |
task_database(database, :status) | |
task_database(database, :restart) | |
if database.running? | |
task_database(database, :stop) | |
else | |
task_database(database, :start) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment