Skip to content

Instantly share code, notes, and snippets.

@danieroux
Created December 29, 2008 17:03
Show Gist options
  • Save danieroux/41310 to your computer and use it in GitHub Desktop.
Save danieroux/41310 to your computer and use it in GitHub Desktop.
# 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