Class: MongoDbUtils::Model::Db

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo-db-utils/models/db.rb

Overview

A Db stored in the config

Direct Known Subclasses

ReplicaSetDb

Constant Summary collapse

URI_NO_USER =
/mongodb:\/\/(.*)\/(.*$)/
URI_USER =
/mongodb:\/\/(.*):(.*)@(.*)\/(.*$)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Db

Returns a new instance of Db.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mongo-db-utils/models/db.rb', line 12

def initialize(uri)
  user,pwd,host_port,db = nil

  if( uri.match(URI_USER))
    match, user, pwd, host_port, name = *uri.match(URI_USER)
  elsif(uri.match(URI_NO_USER))
    match, host_port, name = *uri.match(URI_NO_USER)
    user = ""
    pwd = ""
  end

  raise "can't parse uri" if( host_port.nil? || name.nil? )

  @host_port = host_port
  @name = name
  @username = user
  @password = pwd
  @uri = uri
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/mongo-db-utils/models/db.rb', line 10

def name
  @name
end

#passwordObject

Returns the value of attribute password.



10
11
12
# File 'lib/mongo-db-utils/models/db.rb', line 10

def password
  @password
end

#uriObject

Returns the value of attribute uri.



10
11
12
# File 'lib/mongo-db-utils/models/db.rb', line 10

def uri
  @uri
end

#usernameObject

Returns the value of attribute username.



10
11
12
# File 'lib/mongo-db-utils/models/db.rb', line 10

def username
  @username
end

Instance Method Details

#<=>(other) ⇒ Object



50
51
52
# File 'lib/mongo-db-utils/models/db.rb', line 50

def <=>(other)
  self.to_s <=> other.to_s
end

#authentication_required?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/mongo-db-utils/models/db.rb', line 32

def authentication_required?
  has?(self.username) && has?(self.password)
end

#to_host_sObject

Return the host string in a format that is compatable with mongo binary tools See: docs.mongodb.org/manual/reference/program/mongodump/#cmdoption-mongodump–host



38
39
40
# File 'lib/mongo-db-utils/models/db.rb', line 38

def to_host_s
  "#{@host_port}"
end

#to_sObject



46
47
48
# File 'lib/mongo-db-utils/models/db.rb', line 46

def to_s
  "[SingleDb-(#{to_host_s}/#{name})]"
end

#to_s_simpleObject



42
43
44
# File 'lib/mongo-db-utils/models/db.rb', line 42

def to_s_simple
  "#{@name} on #{@host_port} - (#{@username}:#{@password})"
end