Class: CFRuntime::MongoClient

Inherits:
Object
  • Object
show all
Defined in:
lib/cf-runtime/mongodb.rb

Class Method Summary collapse

Class Method Details

.create(options = {}) ⇒ Object

Creates and returns a Mongo Connection to a single mongodb service. Passes optional Hash of non-connection-related options to Mongo::Connection.new. The connection is wrapped in a proxy that adds a no-argument db method to gain access to the database created by CloudFoundry without having to specify the name. Raises ArgumentError If zero or multiple mongodb services are found.



11
12
13
14
15
16
17
18
19
# File 'lib/cf-runtime/mongodb.rb', line 11

def self.create(options={})
  service_names = CloudApp.service_names_of_type('mongodb')
  if service_names.length != 1
    raise ArgumentError.new("Expected 1 service of mongodb type, " +
      "but found #{service_names.length}.  " +
      "Consider using create_from_svc(service_name) instead.")
  end
  create_from_svc(service_names[0],options)
end

.create_from_svc(service_name, options = {}) ⇒ Object

Creates and returns a Mongo Connection to a mongodb service with the specified name. Passes optional Hash of non-connection-related options to Mongo::Connection.new. The connection is wrapped in a proxy that adds a no-argument db method to gain access to the database created by CloudFoundry without having to specify the name. Raises ArgumentError If specified mongodb service is not found.



27
28
29
30
31
32
33
34
35
# File 'lib/cf-runtime/mongodb.rb', line 27

def self.create_from_svc(service_name,options={})
  service_props = CFRuntime::CloudApp.service_props(service_name)
  if service_props.nil?
    raise ArgumentError.new("Service with name #{service_name} not found")
  end
  uri = "mongodb://#{service_props[:username]}:#{service_props[:password]}@#{service_props[:host]}:#{service_props[:port]}/#{service_props[:db]}"
  conn = Mongo::Connection.from_uri(uri, options)
  MongoConnection.new(conn, service_props[:db])
end

.db_nameObject

Returns the db_name for a single mongodb service. Raises ArgumentError If zero or multiple mongodb services are found.



39
40
41
42
43
44
45
46
47
# File 'lib/cf-runtime/mongodb.rb', line 39

def self.db_name()
  service_names = CloudApp.service_names_of_type('mongodb')
  if service_names.length != 1
    raise ArgumentError.new("Expected 1 service of mongodb type, " +
      "but found #{service_names.length}.  " +
      "Consider using db_name_from_svc(service_name) instead.")
  end
  db_name_from_svc(service_names[0])
end

.db_name_from_svc(service_name) ⇒ Object

Returns the db_name for the mongodb service with the specified name. Raises ArgumentError If specified mongodb service is not found.



51
52
53
54
55
56
57
# File 'lib/cf-runtime/mongodb.rb', line 51

def self.db_name_from_svc(service_name)
  service_props = CFRuntime::CloudApp.service_props(service_name)
  if service_props.nil?
    raise ArgumentError.new("Service with name #{service_name} not found")
  end
  service_props[:db]
end