Module: MongoMapper

Defined in:
lib/mongo_mapper/plugins/callbacks.rb,
lib/mongo_mapper.rb,
lib/mongo_mapper/query.rb,
lib/mongo_mapper/plugins.rb,
lib/mongo_mapper/version.rb,
lib/mongo_mapper/document.rb,
lib/mongo_mapper/plugins/keys.rb,
lib/mongo_mapper/support/find.rb,
lib/mongo_mapper/plugins/clone.rb,
lib/mongo_mapper/plugins/dirty.rb,
lib/mongo_mapper/plugins/rails.rb,
lib/mongo_mapper/plugins/logger.rb,
lib/mongo_mapper/plugins/inspect.rb,
lib/mongo_mapper/plugins/equality.rb,
lib/mongo_mapper/plugins/keys/key.rb,
lib/mongo_mapper/embedded_document.rb,
lib/mongo_mapper/plugins/modifiers.rb,
lib/mongo_mapper/plugins/protected.rb,
lib/mongo_mapper/plugins/pagination.rb,
lib/mongo_mapper/plugins/timestamps.rb,
lib/mongo_mapper/plugins/userstamps.rb,
lib/mongo_mapper/plugins/descendants.rb,
lib/mongo_mapper/plugins/persistence.rb,
lib/mongo_mapper/plugins/validations.rb,
lib/mongo_mapper/plugins/associations.rb,
lib/mongo_mapper/plugins/identity_map.rb,
lib/mongo_mapper/plugins/serialization.rb,
lib/mongo_mapper/plugins/pagination/proxy.rb,
lib/mongo_mapper/plugins/associations/base.rb,
lib/mongo_mapper/plugins/associations/proxy.rb,
lib/mongo_mapper/support/descendant_appends.rb,
lib/mongo_mapper/plugins/associations/one_proxy.rb,
lib/mongo_mapper/plugins/associations/collection.rb,
lib/mongo_mapper/plugins/associations/in_array_proxy.rb,
lib/mongo_mapper/plugins/serialization/xml_serializer.rb,
lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb,
lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb,
lib/mongo_mapper/plugins/associations/embedded_collection.rb,
lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb,
lib/mongo_mapper/plugins/associations/many_documents_proxy.rb,
lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb,
lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb,
lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb,
lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb,
lib/mongo_mapper/support.rb

Overview

Almost all of this callback stuff is pulled directly from ActiveSupport in the interest of support rails 2 and 3 at the same time and is the same copyright as rails.

Defined Under Namespace

Modules: Document, EmbeddedDocument, Plugins, Support Classes: DocumentNotFound, DocumentNotValid, InvalidScheme, KeyNotFound, MongoMapperError, Query

Constant Summary collapse

Version =
'0.7.8'

Class Method Summary collapse

Class Method Details

.configObject



73
74
75
76
# File 'lib/mongo_mapper.rb', line 73

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

.config=(hash) ⇒ Object



69
70
71
# File 'lib/mongo_mapper.rb', line 69

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:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mongo_mapper.rb', line 79

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



94
95
96
97
98
99
100
# File 'lib/mongo_mapper.rb', line 94

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

.connectionObject



30
31
32
# File 'lib/mongo_mapper.rb', line 30

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

.connection=(new_connection) ⇒ Object



35
36
37
# File 'lib/mongo_mapper.rb', line 35

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

.databaseObject



51
52
53
54
55
56
57
# File 'lib/mongo_mapper.rb', line 51

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

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

.database=(name) ⇒ Object



45
46
47
48
# File 'lib/mongo_mapper.rb', line 45

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

.handle_passenger_forkingObject



108
109
110
111
112
113
114
# File 'lib/mongo_mapper.rb', line 108

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



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

def self.logger
  connection.logger
end

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



102
103
104
105
106
# File 'lib/mongo_mapper.rb', line 102

def self.setup(config, environment, options={})
  handle_passenger_forking
  self.config = config
  connect(environment, options)
end

.slave_connectionsObject



60
61
62
# File 'lib/mongo_mapper.rb', line 60

def self.slave_connections
  @@slave_connections ||= []
end

.slave_connections=(slaves = []) ⇒ Object



65
66
67
# File 'lib/mongo_mapper.rb', line 65

def self.slave_connections=(slaves = [])
  @@slave_connections = slaves
end