Module: Low::Mongo::Util

Defined in:
lib/low/mongo/util.rb

Overview

The ‘Util` module provides some handy static Mongo helper methods.

Class Method Summary collapse

Class Method Details

.extract_mongodb_uris(string) ⇒ Object

Given a ‘string`, return all the mongdb URIs contained therein.



10
11
12
# File 'lib/low/mongo/util.rb', line 10

def self.extract_mongodb_uris(string)
  URI.extract(string, 'mongodb')
end

.sync_from_remote(local_database_or_mongo, remote) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/low/mongo/util.rb', line 14

def self.sync_from_remote(local_database_or_mongo, remote)
  # If a Mongo::Local is specified,
  local = local_database_or_mongo.is_a?(Mongo::Local) ?

    # use it,
    local_database_or_mongo :

    # otherwise, assume that it is a database name and build one.
    Mongo::Local.new(local_database_or_mongo)

  # Extract host and port from the remote URI,
  match = remote.uri.match(URI.regexp('mongodb'))
  remote_host = match[4]
  remote_port = match[5]

  # and copy the database.
  local.connection.copy_database(
    remote.database,
    local.database,
    "#{remote_host}:#{remote_port}",
    remote.username,
    remote.password
  )
end