Sha256: c569473a3f0deed5f1270b7aa2ae0b594b68c20537862d32fc322ef72f50b673

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require 'dm-core'

def config_file()
  RAILS_ROOT + "/config/database.yml"
end

def create_connection()
  conf = config.dup
  if repositories = conf.delete(:repositories)
    repositories.each do |repo, conf|
      ::DataMapper.setup(repo, conf) unless conf.empty?
    end
  else
    ::DataMapper.setup(:default, conf) unless conf.empty?
  end
end

def get_config_for_environment
  if hash = full_config[RAILS_ENV]
    symbolize_keys(hash)
  elsif hash = full_config[RAILS_ENV.to_sym]
    hash
  else
    raise ArgumentError, "missing environment '#{RAILS_ENV}' in config file #{config_file}"
  end
end

def full_config
  @full_config ||= YAML::load(ERB.new(IO.read(config_file)).result)
end

def config
  @config ||= get_config_for_environment
end

def symbolize_keys(h)
  config = {}

  h.each do |k, v|
    if k == 'port'
      config[k.to_sym] = v.to_i
    elsif k == 'adapter' && v == 'postgresql'
      config[k.to_sym] = 'postgres'
    elsif v.is_a?(Hash)
      config[k.to_sym] = symbolize_keys(v)
    else
      config[k.to_sym] = v
    end
  end

  config
end

create_connection()

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
datamapper4rail-0.2.0 lib/datamapper4rails/database_config.rb