Module: Merb::Orms::Mongoid

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

Defined Under Namespace

Classes: Connect

Class Method Summary collapse

Class Method Details

.configObject



28
29
30
# File 'lib/merb/orms/mongoid/connection.rb', line 28

def config
  @config ||= get_config
end

.config_fileObject



10
# File 'lib/merb/orms/mongoid/connection.rb', line 10

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

.connectObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/merb/orms/mongoid/connection.rb', line 32

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 '#{config[:database]}' database on '#{host}' ...")
    connection = ::Mongo::Connection.new(host, port)
    ::Mongoid.database = connection.db(config[:database])
    if config[:username]
      ::Mongoid.database.authenticate(config[:username], config[:password])
    end
    Merb.logger.info!("Connected to '#{host}'")
    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



14
15
16
# File 'lib/merb/orms/mongoid/connection.rb', line 14

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

.get_configObject



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

def get_config
  # Convert string keys to symbols
  full_config = Erubis.load_yaml_file(config_file)
  config = (Merb::Plugins.config[:merb_mongoid] = {})
  (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



56
57
58
59
60
61
62
# File 'lib/merb/orms/mongoid/connection.rb', line 56

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



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

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

.sample_sourceObject



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

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