Module: TinyMongo

Defined in:
lib/tinymongo.rb,
lib/tinymongo/model.rb,
lib/tinymongo/cursor.rb,
lib/tinymongo/errors.rb,
lib/tinymongo/helper.rb

Defined Under Namespace

Modules: Helper Classes: Cursor, DeserializationError, Error, Model, ModifierOperationError, NotConfiguredError, NotConnectedError

Class Method Summary collapse

Class Method Details

.configure(config = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/tinymongo.rb', line 5

def configure(config={})
  config = Helper.stringify_keys_in_hash(config)

  @host = config['host'] || 'localhost'
  @port = config['port']
  @options = config['options'] || {}
  @database = config['database'] || 'mongo'
  @username = config['username']
  @password = config['password']
  @configured = true
end

.connectObject

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tinymongo.rb', line 22

def connect
  raise NotConfiguredError unless @configured
  
  if defined?(PhusionPassenger) && @connection
    PhusionPassenger.on_event(:starting_worker_process) do |forked|
      @connection.connect_to_master if forked
    end
  end
    
  if(@connection.nil?)
    @connection = Mongo::Connection.new(@host, @port, @options)
    @db = @connection.db(@database)

    if(@username && @password) 
      auth = db.authenticate(@username, @password)
      return nil unless(auth)
    end
  end
  
  @connected = true
end

.connected?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/tinymongo.rb', line 44

def connected?
  @connected ? true : false
end

.dbObject

Raises:



17
18
19
20
# File 'lib/tinymongo.rb', line 17

def db
  raise NotConnectedError unless TinyMongo.connected?
  @db
end