Skip to content

Commit

Permalink
isolates a new lib the responsability to discover url string connection
Browse files Browse the repository at this point in the history
  • Loading branch information
mistersourcerer committed Jul 11, 2012
1 parent 35af6fd commit 621c0cf
Showing 1 changed file with 6 additions and 76 deletions.
82 changes: 6 additions & 76 deletions lib/interestie.rb
Expand Up @@ -3,84 +3,14 @@
$LOAD_PATH.unshift File.expand_path("../", __FILE__)
$LOAD_PATH.unshift File.expand_path("../../app/models", __FILE__)

class ConnectionString
def initialize(config)
@config = config
end

attr_accessor :config

def credentials
"#{@config["username"]}:#{@config["password"]}"
end

def database
"/#{@config["database"]}"
end

def host
@config["host"] ? "@#{@config["host"]}" : "@localhost"
end

def connection
"#{credentials}#{host}#{database}"
end

def string
"#{adapter}://#{connection}"
end
end

class SqliteString < ConnectionString
def adapter
@config["adapter"] == "sqlite3" ? "sqlite" : @config["adapter"]
end

def credentials
""
end

def database
""
end

def connection
"#{File.expand_path("../../", __FILE__)}/#{@config["database"]}"
end
end

class PostgresString < ConnectionString
def adapter
@config["adapter"] == "postgresql" ? "postgres" : @config["adapter"]
end
end

class ConnectionStringFactory
def self.string_for_config config
con_string_object(config).string
end

private
def self.con_string_object config
adapters = {
/postgre/ => proc{ PostgresString.new(config) },
/sqlite/ => proc{ SqliteString.new(config) }
}
proc_to_create = adapters.select{ |regex, obj| regex =~ config["adapter"] }.values[0]
proc_to_create.call
end
end

def database_url
require "yaml"
require "erb"
database_yml = File.expand_path("../../config/database.yml", __FILE__)
database_config = YAML.load(ERB.new(File.read(database_yml)).result(binding))
config = database_config[ENV["RACK_ENV"]]
ConnectionStringFactory.string_for_config config
$LOAD_PATH.unshift File.expand_path("../../vendor/sequelinha/lib", __FILE__)
require "sequelinha"
Sequelinha.configure do |config|
config.project_root = File.expand_path("../../", __FILE__)
end

ENV["DATABASE_URL"] ||= database_url
# prepares interestie to run like a boss on heroku
ENV["DATABASE_URL"] ||= Sequelinha.database_url

require "interestie/connection"
require "interestie/application"

0 comments on commit 621c0cf

Please sign in to comment.