Module: Merb::Orms::DataMapper

Defined in:
lib/merb_datamapper/connection.rb

Defined Under Namespace

Classes: Connect, Finalize, IdentityMapSupport

Class Method Summary collapse

Class Method Details

.configObject



19
20
21
# File 'lib/merb_datamapper/connection.rb', line 19

def config
  @config ||= Merb::Plugins.config[:merb_datamapper][:connection] ||= (get_config_for_environment)
end

.config_fileObject



7
# File 'lib/merb_datamapper/connection.rb', line 7

def config_file() Merb.dir_for(:config) / "database.yml" end

.connectObject

Database connects as soon as the gem is loaded



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/merb_datamapper/connection.rb', line 24

def connect
  if File.exists?(config_file)
    start_logging
    setup_connections
  else
    copy_sample_config
    Merb.logger.error! "No database.yml file found in #{Merb.root}/config."
    Merb.logger.error! "A sample file was created called database.sample.yml for you to copy and edit."
    exit(1)
  end
end

.copy_sample_configObject



11
12
13
# File 'lib/merb_datamapper/connection.rb', line 11

def copy_sample_config
  FileUtils.cp sample_source, sample_dest unless File.exists?(sample_dest)
end

.full_configObject



15
16
17
# File 'lib/merb_datamapper/connection.rb', line 15

def full_config
  @full_config ||= Erubis.load_yaml_file(config_file)
end

.register_session_typeObject

Registering this ORM lets the user choose DataMapper as a session store in merb.yml’s session_store: option.



50
51
52
53
54
# File 'lib/merb_datamapper/connection.rb', line 50

def register_session_type
  Merb.register_session_type("datamapper",
    "merb/session/data_mapper_session",
    "Using DataMapper database sessions")
end

.sample_destObject



8
# File 'lib/merb_datamapper/connection.rb', line 8

def sample_dest() Merb.dir_for(:config) / "database.yml.sample" end

.sample_sourceObject



9
# File 'lib/merb_datamapper/connection.rb', line 9

def sample_source() File.dirname(__FILE__) / "database.yml.sample" end

.setup_connectionsObject



41
42
43
44
45
46
# File 'lib/merb_datamapper/connection.rb', line 41

def setup_connections
  conf = config.dup
  repositories = conf.delete(:repositories)
  ::DataMapper.setup(:default, conf) unless conf.empty?
  repositories.each { |name, opts| ::DataMapper.setup(name, opts) } if repositories
end

.start_loggingObject



36
37
38
39
# File 'lib/merb_datamapper/connection.rb', line 36

def start_logging
  ::DataMapper.logger = Merb.logger
  ::DataMapper.logger.info("Connecting to database...")
end

.symbolize_keys(h) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/merb_datamapper/connection.rb', line 56

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