config.rb 967 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
require 'yaml'
require 'erubis'
require 'fileutils'

module ARTest
  class << self
    def config
      @config ||= read_config
    end

    private

    def read_config
      unless File.exist?(TEST_ROOT + '/config.yml')
        FileUtils.cp TEST_ROOT + '/config.example.yml', TEST_ROOT + '/config.yml'
      end

      raw = File.read(TEST_ROOT + '/config.yml')
      erb = Erubis::Eruby.new(raw)
      expand_config(YAML.parse(erb.result(binding)).transform)
    end

    def expand_config(config)
      config['connections'].each do |adapter, connection|
J
Jon Leighton 已提交
25
        dbs = [['arunit', 'activerecord_unittest'], ['arunit2', 'activerecord_unittest2']]
26 27 28 29 30 31 32 33 34 35 36 37 38 39
        dbs.each do |name, dbname|
          unless connection[name].is_a?(Hash)
            connection[name] = { 'database' => connection[name] }
          end

          connection[name]['database'] ||= dbname
          connection[name]['adapter']  ||= adapter
        end
      end

      config
    end
  end
end