Skip to content

Commit

Permalink
cleaning the room
Browse files Browse the repository at this point in the history
  • Loading branch information
mistersourcerer committed Jul 11, 2012
1 parent 5f6b24f commit b599da3
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 121 deletions.
131 changes: 10 additions & 121 deletions vendor/sequelinha/lib/sequelinha.rb
@@ -1,75 +1,8 @@
module Sequelinha
class ConnectionURL
def initialize(config)
@config = config
end

attr_accessor :config
def self.=~(adapter)
nil
end

def username
@config["username"]
end

def password
@config["password"]
end

def adapter
@config["adapter"]
end

def database
@config["database"]
end

def host
@config["host"] || "localhost"
end

def string
final_url = "#{self.adapter}://"
final_url << "#{self.username}:" if self.username
final_url << "#{self.password}" if self.password
final_url << "@#{self.host}" if self.host
final_url << "/#{self.database}" if self.database
end
end
end

module Sequelinha
class SqliteURL < ConnectionURL
def host
nil
end

def adapter
super == "sqlite3" ? "sqlite" : super
end

def database
"#{Sequelinha.config.project_root}/#{super}"
end

def self.=~(adapter)
adapter =~ /sqlite/
end
end
end

module Sequelinha
class PostgresURL < ConnectionURL
def adapter
super == "postgresql" ? "postgres" : super
end

def self.=~(adapter)
adapter =~ /postgre/
end
end
end
require "sequelinha/config"
require "sequelinha/connection_url"
require "sequelinha/connection_url_factory"
require "sequelinha/adapters/sqlite"
require "sequelinha/adapters/postgres"

module Sequelinha
def self.implementations
Expand All @@ -80,60 +13,13 @@ def self.register(klass, &block)
self.implementations << klass
self
end
end

Sequelinha.register Sequelinha::PostgresURL
Sequelinha.register Sequelinha::SqliteURL

module Sequelinha
class ConnectionURLFactory
def self.get_instance config
implementation(config).string
end

private
def self.implementation config
implementation = Sequelinha.implementations.find { |klass| klass =~ config["adapter"] }
implementation.new(config)
end
end
end

module Sequelinha
class Config
def initialize(data={})
@data = data
end

def [](key)
@data[key.to_sym]
end

def []=(key, value)
if value.class == Hash
@data[key.to_sym] = Config.new(value)
else
@data[key.to_sym] = value
end
end

def method_missing(sym, *args)
if sym.to_s =~ /(.+)=$/
self[$1] = args.first
else
self[sym]
end
end
end
end

module Sequelinha
def self.config
@config
end

def self.configure
@config ||= Config.new()
@config ||= Config.new
yield(@config) if block_given?

@config.database_yml ||= "#{@config.project_root}/config/database.yml"
Expand All @@ -146,6 +32,9 @@ def self.database_url
database_yml = config["database_yml"]
database_config = YAML.load(ERB.new(File.read(database_yml)).result(binding))
config = database_config[ENV["RACK_ENV"] || "development"]
ConnectionURLFactory.get_instance config
ConnectionURLFactory.url_for config
end
end

Sequelinha.register Sequelinha::Adapters::Postgres
Sequelinha.register Sequelinha::Adapters::Sqlite
14 changes: 14 additions & 0 deletions vendor/sequelinha/lib/sequelinha/adapters/postgres.rb
@@ -0,0 +1,14 @@
module Sequelinha
module Adapters
class Postgres < ConnectionURL
def adapter
super == "postgresql" ? "postgres" : super
end

def self.=~(adapter)
adapter =~ /postgre/
end
end
end
end

22 changes: 22 additions & 0 deletions vendor/sequelinha/lib/sequelinha/adapters/sqlite.rb
@@ -0,0 +1,22 @@
module Sequelinha
module Adapters
class Sqlite < ConnectionURL
def host
nil
end

def adapter
super == "sqlite3" ? "sqlite" : super
end

def database
"#{Sequelinha.config.project_root}/#{super}"
end

def self.=~(adapter)
adapter =~ /sqlite/
end
end
end
end

27 changes: 27 additions & 0 deletions vendor/sequelinha/lib/sequelinha/config.rb
@@ -0,0 +1,27 @@
module Sequelinha
class Config
def initialize(data={})
@data = data
end

def [](key)
@data[key.to_sym]
end

def []=(key, value)
if value.class == Hash
@data[key.to_sym] = Config.new(value)
else
@data[key.to_sym] = value
end
end

def method_missing(sym, *args)
if sym.to_s =~ /(.+)=$/
self[$1] = args.first
else
self[sym]
end
end
end
end
40 changes: 40 additions & 0 deletions vendor/sequelinha/lib/sequelinha/connection_url.rb
@@ -0,0 +1,40 @@
module Sequelinha
class ConnectionURL
def initialize(config)
@config = config
end

attr_accessor :config
def self.=~(adapter)
nil
end

def username
@config["username"]
end

def password
@config["password"]
end

def adapter
@config["adapter"]
end

def database
@config["database"]
end

def host
@config["host"] || "localhost"
end

def string
final_url = "#{self.adapter}://"
final_url << "#{self.username}:" if self.username
final_url << "#{self.password}" if self.password
final_url << "@#{self.host}" if self.host
final_url << "/#{self.database}" if self.database
end
end
end
13 changes: 13 additions & 0 deletions vendor/sequelinha/lib/sequelinha/connection_url_factory.rb
@@ -0,0 +1,13 @@
module Sequelinha
class ConnectionURLFactory
def self.url_for config
implementation(config).string
end

private
def self.implementation config
implementation = Sequelinha.implementations.find { |klass| klass =~ config["adapter"] }
implementation.new(config)
end
end
end

0 comments on commit b599da3

Please sign in to comment.