Top Level Namespace

Defined Under Namespace

Modules: ActionController, DataMapper, DatamapperStore, ModelLoader Classes: Datamapper4rails

Constant Summary collapse

MODELS =

load all models before each request so relations in datamapper find their classes

[]

Instance Method Summary collapse

Instance Method Details

#configObject



32
33
34
# File 'lib/datamapper4rails/database_config.rb', line 32

def config
  @config ||= get_config_for_environment
end

#config_fileObject



3
4
5
# File 'lib/datamapper4rails/database_config.rb', line 3

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

#create_connectionObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/datamapper4rails/database_config.rb', line 7

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

#full_configObject



28
29
30
# File 'lib/datamapper4rails/database_config.rb', line 28

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

#get_config_for_environmentObject



18
19
20
21
22
23
24
25
26
# File 'lib/datamapper4rails/database_config.rb', line 18

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

#symbolize_keys(h) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/datamapper4rails/database_config.rb', line 36

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