Sha256: 8455b0264cdb1e59efaa8b2e9e1d6b0ba2857a1c242a8df97cf3e0619589d09e

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

require 'yaml'

class ActiveRecordConnect
  attr_accessor :database

  def validate
    %w[adapter database].each do |key|
      if @database[key].nil? or @database[key].empty?
        error_message "If you would like to connect to the database, please populate #{File.expand_path(File.dirname(__FILE__))}/database.yml ."
        return false
      end
    end
  end  
  
  def get_database_information
    @database = YAML.load_file('database.yml')['database']
  end
  
  def connect
    get_database_information
    validate
    connect!
  end
  
  def error_message(message)
    puts "=" * 50
    puts message
    puts "=" * 50    
  end
  
  def connection_data
    connection = {}
    connection[:adapter]   = @database["adapter"]   if @database["adapter"]
    connection[:host]       = @database["host"]     if @database["host"]
    connection[:username]   = @database["username"] if @database["username"]
    connection[:password]   = @database["password"] if @database["password"]
    connection[:datababase] = @database["database"] if @database["database"]
    connection
  end

  def connect!
    puts "Connect with #{connection_data.inspect}"
    begin    
      ActiveRecord::Base.establish_connection( connection_data )
    rescue Exception => e
      error_message("It appears that your database information is invalid.
      \rPlease check your configuration in #{File.expand_path(File.dirname(__FILE__))}/database.yml .
      \rSee the error message below for more information.\r
      \r\r\r#{e.message}")
    
    end
  end
end

ActiveRecordConnect.new.connect

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
roadkill-0.0.1 files/generate/ready/db_connect.rb