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.

Revisions

  1. mikehale renamed this gist Jun 30, 2009. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. mikehale created this gist Jun 30, 2009.
    62 changes: 62 additions & 0 deletions database.yml.thor
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    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