Class: GschoolDatabaseConnection::DatabaseConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/gschool_database_connection/database_connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, config_file_path) ⇒ DatabaseConnection

Returns a new instance of DatabaseConnection.



17
18
19
20
21
22
23
24
25
# File 'lib/gschool_database_connection/database_connection.rb', line 17

def initialize(environment, config_file_path)
  if ENV["DATABASE_URL"]
    @connection = establish_from_uri(ENV["DATABASE_URL"])
  else
    file = File.read(config_file_path)
    config = YAML.load(file)[environment]
    @connection = establish_from_config(config)
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



6
7
8
# File 'lib/gschool_database_connection/database_connection.rb', line 6

def connection
  @connection
end

Class Method Details

.clear!Object



12
13
14
15
# File 'lib/gschool_database_connection/database_connection.rb', line 12

def self.clear!
  @@_connection and @@_connection.close
  @@_connection = nil
end

.establish(environment = "development", config_file_path = "config/database.yml") ⇒ Object



8
9
10
# File 'lib/gschool_database_connection/database_connection.rb', line 8

def self.establish(environment = "development", config_file_path="config/database.yml")
  @@_connection ||= new(environment, config_file_path)
end

Instance Method Details

#closeObject



31
32
33
# File 'lib/gschool_database_connection/database_connection.rb', line 31

def close
  connection.close
end

#sql(sql_string) ⇒ Object



27
28
29
# File 'lib/gschool_database_connection/database_connection.rb', line 27

def sql(sql_string)
  connection.exec(sql_string).to_a
end