Class: Cubicle::MongoEnvironment

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

Class Method Summary collapse

Class Method Details

.configObject



44
45
46
47
# File 'lib/cubicle/mongo_environment.rb', line 44

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

.config=(hash) ⇒ Object



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

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)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cubicle/mongo_environment.rb', line 50

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



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

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



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

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

.connection=(new_connection) ⇒ Object



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

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

.databaseObject



32
33
34
35
36
37
38
# File 'lib/cubicle/mongo_environment.rb', line 32

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

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

.database=(name) ⇒ Object



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

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

.handle_passenger_forkingObject



81
82
83
84
85
86
87
# File 'lib/cubicle/mongo_environment.rb', line 81

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



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

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.



100
101
102
# File 'lib/cubicle/mongo_environment.rb', line 100

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

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



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

def self.setup(config, environment, options={})
  using_passenger = options.delete(:passenger)
  handle_passenger_forking if using_passenger
  self.config = config
  connect(environment, options)
  self
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.



95
96
97
# File 'lib/cubicle/mongo_environment.rb', line 95

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)


90
91
92
# File 'lib/cubicle/mongo_environment.rb', line 90

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