Skip to content

Instantly share code, notes, and snippets.

@mikehale
Created June 30, 2009 14:18
Show Gist options
  • Select an option

  • Save mikehale/138174 to your computer and use it in GitHub Desktop.

Select an option

Save mikehale/138174 to your computer and use it in GitHub Desktop.
Generate your database.yml file with thor
class DatabaseYml < Thor
FILE = 'config/database.yml'
desc "mysql db_name", "create a database.yml for the named mysql database"
method_options :force => :boolean
def mysql(name)
yml = %(
env: &env
adapter: mysql
encoding: utf8
username: root
password:
host: localhost
development:
<<: *env
database: #{name}_development
test:
<<: *env
database: #{name}_test
production:
<<: *env
database: #{name}_production
).lstrip!
create_file(yml, options)
end
desc "sqlite", "create a database.yml for sqlite3"
method_options :force => :boolean
def sqlite
yml = %(
env: &env
adapter: sqlite3
timeout: 5000
development:
<<: *env
database: db/development.sqlite3
test:
<<: *env
database: db/test.sqlite3
production:
<<: *env
database: db/production.sqlite3
).lstrip!
create_file(yml, options)
end
def create_file(yml, options)
if (!File.exists?(FILE) || File.exists?(FILE) && options.force?)
File.open(FILE, 'w+') do |f|
f.print yml
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment