Module: Merb::Orms::MongoMapper

Defined in:
lib/merb/orms/mongomapper/connection.rb

Defined Under Namespace

Classes: Connect

Class Method Summary collapse

Class Method Details

.configObject



29
30
31
# File 'lib/merb/orms/mongomapper/connection.rb', line 29

def config
  @config ||= get_config
end

.config_fileObject



11
# File 'lib/merb/orms/mongomapper/connection.rb', line 11

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

.connectObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/merb/orms/mongomapper/connection.rb', line 37

def connect
  no_database_file unless File.exists?(config_file)

  # it is possible that we have been passed an array of hosts - attempt each in turn until one is found
  host = config[:host] || 'localhost'
  port = config[:port] || 27017
  begin
    Merb.logger.info!("Attempting connection to the '#{database}' database on '#{host}' ...")
    ::MongoMapper.connection = Mongo::Connection.new(host, port)
    Merb.logger.info!("Connected to '#{host}'")
    ::MongoMapper.database = database
    return true
  rescue Errno::ECONNREFUSED
    Merb.logger.info!("#{host} not available")
  rescue => e
    Merb.logger.info!("Connection Error: #{e}")
    Merb.print_colorized_backtrace(e)
    exit(1)
  end
end

.copy_sample_configObject



15
16
17
# File 'lib/merb/orms/mongomapper/connection.rb', line 15

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

.databaseObject



33
34
35
# File 'lib/merb/orms/mongomapper/connection.rb', line 33

def database
  (config[:database_prefix] || '') + (config[:database] || '') + (config[:database_suffix] || '')
end

.get_configObject



19
20
21
22
23
24
25
26
27
# File 'lib/merb/orms/mongomapper/connection.rb', line 19

def get_config
  # Convert string keys to symbols
  full_config = Erubis.load_yaml_file(config_file)
  config = (Merb::Plugins.config[:merb_mongomapper] = {})
  (full_config[Merb.environment.to_sym] || full_config[Merb.environment] || full_config[:development]).each do |key, value|
    config[key.to_sym] = value
  end
  config
end

.no_database_fileObject



58
59
60
61
62
63
64
# File 'lib/merb/orms/mongomapper/connection.rb', line 58

def no_database_file
  copy_sample_config
  Merb.logger.set_log(STDERR)
  Merb.logger.error! "No database.yml file found at #{config_file}."
  Merb.logger.error! "A sample file was created called #{sample_dest} for you to copy and edit."
  exit(1)
end

.sample_destObject



12
# File 'lib/merb/orms/mongomapper/connection.rb', line 12

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

.sample_sourceObject



13
# File 'lib/merb/orms/mongomapper/connection.rb', line 13

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