Class: Mongoid::Sessions::MongoUri

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/sessions/mongo_uri.rb

Constant Summary collapse

SCHEME =
/(mongodb:\/\/)/
USER =
/([-.\w:]+)/
PASS =
/([^@,]+)/
NODES =
/((([-.\w]+)(?::(\w+))?,?)+)/
DATABASE =
/(?:\/([-\w]+))?/
URI =
/#{SCHEME}(#{USER}:#{PASS}@)?#{NODES}#{DATABASE}/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ MongoUri

Create the new uri from the provided string.

Examples:

Create the new uri.

MongoUri.new(uri)

Parameters:

  • string (String)

    The uri string.

Since:

  • 3.0.0



48
49
50
# File 'lib/mongoid/sessions/mongo_uri.rb', line 48

def initialize(string)
  @match = string.match(URI)
end

Instance Attribute Details

#matchObject (readonly)

Returns the value of attribute match.



14
15
16
# File 'lib/mongoid/sessions/mongo_uri.rb', line 14

def match
  @match
end

Instance Method Details

#databaseString

Get the database provided in the URI.

Examples:

Get the database.

uri.database

Returns:

  • (String)

    The database.

Since:

  • 3.0.0



24
25
26
# File 'lib/mongoid/sessions/mongo_uri.rb', line 24

def database
  @database ||= match[9]
end

#hostsArray<String>

Get the hosts provided in the URI.

Examples:

Get the hosts.

uri.hosts

Returns:

  • (Array<String>)

    The hosts.

Since:

  • 3.0.0



36
37
38
# File 'lib/mongoid/sessions/mongo_uri.rb', line 36

def hosts
  @hosts ||= match[5].split(",")
end

#passwordString

Get the password provided in the URI.

Examples:

Get the password.

uri.password

Returns:

  • (String)

    The password.

Since:

  • 3.0.0



60
61
62
# File 'lib/mongoid/sessions/mongo_uri.rb', line 60

def password
  @password ||= match[4]
end

#to_hashHash

Get the uri as a Mongoid friendly configuration hash.

Examples:

Get the uri as a hash.

uri.to_hash

Returns:

  • (Hash)

    The uri as options.

Since:

  • 3.0.0



72
73
74
75
76
77
78
# File 'lib/mongoid/sessions/mongo_uri.rb', line 72

def to_hash
  config = { database: database, hosts: hosts }
  if username && password
    config.merge!(username: username, password: password)
  end
  config
end

#usernameString

Get the username provided in the URI.

Examples:

Get the username.

uri.username

Returns:

  • (String)

    The username.

Since:

  • 3.0.0



88
89
90
# File 'lib/mongoid/sessions/mongo_uri.rb', line 88

def username
  @username ||= match[3]
end