Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f6b24f
commit b599da3
Showing
6 changed files
with
126 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
13
vendor/sequelinha/lib/sequelinha/connection_url_factory.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |