Created
June 30, 2009 14:18
-
-
Save mikehale/138174 to your computer and use it in GitHub Desktop.
Generate your database.yml file with thor
This file contains hidden or 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
| 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