Class: CFRuntime::Mysql2Client

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

Class Method Summary collapse

Class Method Details

.create(options = {}) ⇒ Object

Creates and returns a Mysql2 Client instance connected to a single mysql service. Passes optional Hash of non-connection-related options to Mysql2::Client.new. Raises ArgumentError If zero or multiple mysql services are found.



9
10
11
12
13
14
15
16
17
# File 'lib/cf-runtime/mysql.rb', line 9

def self.create(options={})
  service_names = CloudApp.service_names_of_type('mysql')
  if service_names.length != 1
    raise ArgumentError.new("Expected 1 service of mysql 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 Mysql2 Client instance connected to a mysql service with the specified name. Passes optional Hash of non-connection-related options to Mysql2::Client.new. Raises ArgumentError If specified mysql service is not found.



23
24
25
# File 'lib/cf-runtime/mysql.rb', line 23

def self.create_from_svc(service_name, options={})
  Mysql2::Client.new(options_for_svc(service_name,options))
end

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

Merges provided options with connection options for specified mysql service. Returns merged Hash containing (:username, :password, :database, :host, :port). Raises ArgumentError If specified mysql service is not found.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cf-runtime/mysql.rb', line 30

def self.options_for_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
  cfoptions = options
  cfoptions[:host] = service_props[:host]
  cfoptions[:port] = service_props[:port]
  cfoptions[:username] = service_props[:username]
  cfoptions[:password] = service_props[:password]
  cfoptions[:database] = service_props[:database]
  cfoptions
end