Class: Cubicle::MongoEnvironment

Inherits:
Object
  • Object
show all
Defined in:
lib/cubicle/mongo_environment.rb

Class Method Summary collapse

Class Method Details

.configObject



38
39
40
41
# File 'lib/cubicle/mongo_environment.rb', line 38

def self.config
  raise 'Set config before connecting. Cubicle.mongo.config = {...}' unless defined?(@@config)
  @@config
end

.config=(hash) ⇒ Object



34
35
36
# File 'lib/cubicle/mongo_environment.rb', line 34

def self.config=(hash)
  @@config = hash
end

.config_for_environment(environment) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (InvalidScheme)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cubicle/mongo_environment.rb', line 44

def self.config_for_environment(environment)
  env = config[environment]
  return env if env['uri'].blank?

  uri = URI.parse(env['uri'])
  raise InvalidScheme.new('must be mongodb') unless uri.scheme == 'mongodb'
  {
          'host' => uri.host,
          'port' => uri.port,
          'database' => uri.path.gsub(/^\//, ''),
          'username' => uri.user,
          'password' => uri.password,
          }
end

.connect(environment, options = {}) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/cubicle/mongo_environment.rb', line 59

def self.connect(environment, options={})
  raise 'Set config before connecting. Cubicle.mongo.config = {...}' if config.blank?
  env = config_for_environment(environment)
  self.connection = Mongo::Connection.new(env['host'], env['port'], options)
  self.database = env['database']
  self.database.authenticate(env['username'], env['password']) if env['username'] && env['password']
end

.connectionObject



5
6
7
# File 'lib/cubicle/mongo_environment.rb', line 5

def self.connection
  @@connection ||= Mongo::Connection.new
end

.connection=(new_connection) ⇒ Object



10
11
12
# File 'lib/cubicle/mongo_environment.rb', line 10

def self.connection=(new_connection)
  @@connection = new_connection
end

.databaseObject



26
27
28
29
30
31
32
# File 'lib/cubicle/mongo_environment.rb', line 26

def self.database
  if @@database_name.blank?
    raise 'You forgot to set the default database name: MongoMapper.database = "foobar"'
  end

  @@database ||= connection.db(@@database_name)
end

.database=(name) ⇒ Object



20
21
22
23
# File 'lib/cubicle/mongo_environment.rb', line 20

def self.database=(name)
  @@database = nil
  @@database_name = name
end

.handle_passenger_forkingObject



74
75
76
77
78
79
80
# File 'lib/cubicle/mongo_environment.rb', line 74

def self.handle_passenger_forking
  if defined?(PhusionPassenger)
    PhusionPassenger.on_event(:starting_worker_process) do |forked|
      connection.connect_to_master if forked
    end
  end
end

.loggerObject



15
16
17
# File 'lib/cubicle/mongo_environment.rb', line 15

def self.logger
  connection.logger
end

.normalize_object_id(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



93
94
95
# File 'lib/cubicle/mongo_environment.rb', line 93

def self.normalize_object_id(value)
  value.is_a?(String) ? Mongo::ObjectID.from_string(value) : value
end

.setup(config, environment, options = {}) ⇒ Object



67
68
69
70
71
72
# File 'lib/cubicle/mongo_environment.rb', line 67

def self.setup(config, environment, options={})
  using_passenger = options.delete(:passenger)
  handle_passenger_forking if using_passenger
  self.config = config
  connect(environment, options)
end

.time_classObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



88
89
90
# File 'lib/cubicle/mongo_environment.rb', line 88

def self.time_class
  use_time_zone? ? Time.zone : Time
end

.use_time_zone?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


83
84
85
# File 'lib/cubicle/mongo_environment.rb', line 83

def self.use_time_zone?
  Time.respond_to?(:zone) && Time.zone ? true : false
end