Sha256: a485017280c82d20fa1dc0668f990bd466e9835403af88d22b03bcfe5fe0862e

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 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
    begin
      @database = YAML.load_file('database.yml')['database']
    rescue
      @database = {}
    end
  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

3 entries across 3 versions & 1 rubygems

Version Path
roadkill-0.0.4 files/generate/ready/db_connect.rb
roadkill-0.0.3 files/generate/ready/db_connect.rb
roadkill-0.0.2 files/generate/ready/db_connect.rb