Module: WdSinatraActiveRecord::DBConnector
- Defined in:
- lib/wd_sinatra_active_record.rb
Overview
DB Connection ########
Constant Summary collapse
- DB_CONFIG =
YAML.load_file(File.join(WDSinatra::AppLoader.root_path, "config", "database.yml"))
Class Method Summary collapse
- .connect_to_db ⇒ Object
- .db_configuration(env = RACK_ENV) ⇒ Object
- .db_configurations ⇒ Object
- .set_db_connection(env = RACK_ENV) ⇒ Object
Class Method Details
.connect_to_db ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/wd_sinatra_active_record.rb', line 65 def connect_to_db if @db_configuration connection = ActiveRecord::Base.establish_connection(@db_configuration) else raise "Can't connect without the config previously set" end end |
.db_configuration(env = RACK_ENV) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/wd_sinatra_active_record.rb', line 53 def db_configuration(env=RACK_ENV) old_connect_status = ENV['DONT_CONNECT'] set_db_connection(env) unless @db_configuration ENV['DONT_CONNECT'] = old_connect_status @db_configuration end |
.db_configurations ⇒ Object
60 61 62 63 |
# File 'lib/wd_sinatra_active_record.rb', line 60 def db_configurations db_configuration unless @db_configurations @db_configurations end |
.set_db_connection(env = RACK_ENV) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/wd_sinatra_active_record.rb', line 25 def set_db_connection(env=RACK_ENV) # Set the AR logger if Object.const_defined?(:LOGGER) ActiveRecord::Base.logger = LOGGER else ActiveRecord::Base.logger = Logger.new($stdout) end # Establish the DB connection db_file = File.join(WDSinatra::AppLoader.root_path, "config", "database.yml") if File.exist?(db_file) hash_settings = YAML.load_file(db_file) if hash_settings && hash_settings[env] @db_configurations = hash_settings @db_configuration = @db_configurations[env] # overwrite DB name by using an ENV variable if ENV['FORCED_DB_NAME'] print "Database name overwritten to be #{ENV['FORCED_DB_NAME']}\n" @db_configurations[env]['database'] = @db_configuration['database'] = ENV['FORCED_DB_NAME'] end connect_to_db unless ENV['DONT_CONNECT'] else raise "#{db_file} doesn't have an entry for the #{env} environment" end else raise "#{db_file} file missing, can't connect to the DB" end end |