Class: DatabaseSpecification
- Inherits:
-
Object
- Object
- DatabaseSpecification
- Defined in:
- lib/database_specification.rb,
lib/database_specification/version.rb
Overview
Translate beween different ways of describing a database connection Currently supports
-
ActiveRecord hash
-
Sequel hash
-
URL (Sequel/Heroku)
Construct an object with the appropriate from-database constructor, and then use the appopriate to-database accessor.
Constant Summary collapse
- STANDARD =
How we store the information internally
[ :adapter, :database, :user, :username, :password, :host, :port, ]
- VERSION =
version number
"0.0.1"
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
The database driver type: postgres, sqlite, etc.
-
#database ⇒ Object
readonly
Database name within the server.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#options ⇒ Object
readonly
Misc extra options, ex: pool, timeout.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
-
.active_record(config) ⇒ Object
ActiveRecord constructor.
-
.sequel(config) ⇒ Object
Sequel hash constructor.
-
.url(url) ⇒ Object
URL constructor (Sequel/Heroku).
Instance Method Summary collapse
-
#active_record ⇒ Hash
ActiveRecord style hash.
-
#from_active_record(config) ⇒ DatabaseSpecification
Performs the work of setting up from an ActiveRecord style hash.
-
#from_sequel(config) ⇒ DatabaseSpecification
Performs the work of setting up from a Sequel style hash.
-
#from_url(url) ⇒ DatabaseSpecification
Performs the work of setging up from a url.
-
#sequel ⇒ Hash
Sequel style hash.
-
#url ⇒ String
DB url with query paramters.
-
#url_bare ⇒ String
DB url without query parameters.
Instance Attribute Details
#adapter ⇒ Object (readonly)
The database driver type: postgres, sqlite, etc.
16 17 18 |
# File 'lib/database_specification.rb', line 16 def adapter @adapter end |
#database ⇒ Object (readonly)
Database name within the server
18 19 20 |
# File 'lib/database_specification.rb', line 18 def database @database end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
21 22 23 |
# File 'lib/database_specification.rb', line 21 def host @host end |
#options ⇒ Object (readonly)
Misc extra options, ex: pool, timeout
24 25 26 |
# File 'lib/database_specification.rb', line 24 def @options end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
20 21 22 |
# File 'lib/database_specification.rb', line 20 def password @password end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
22 23 24 |
# File 'lib/database_specification.rb', line 22 def port @port end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
19 20 21 |
# File 'lib/database_specification.rb', line 19 def user @user end |
Class Method Details
.active_record(config) ⇒ Object
ActiveRecord constructor
39 40 41 |
# File 'lib/database_specification.rb', line 39 def self.active_record(config) new.from_active_record(config) end |
.sequel(config) ⇒ Object
Sequel hash constructor
45 46 47 |
# File 'lib/database_specification.rb', line 45 def self.sequel(config) new.from_sequel(config) end |
.url(url) ⇒ Object
URL constructor (Sequel/Heroku)
51 52 53 |
# File 'lib/database_specification.rb', line 51 def self.url(url) new.from_url(url) end |
Instance Method Details
#active_record ⇒ Hash
Returns ActiveRecord style hash.
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/database_specification.rb', line 108 def active_record Hash[[ [:adapter, sequel_to_ar(adapter)], [:database, database], [:username, user], [:password, password], [:host, host], [:port, port], ].select {|x| x.last} + ] end |
#from_active_record(config) ⇒ DatabaseSpecification
Performs the work of setting up from an ActiveRecord style hash
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/database_specification.rb', line 58 def from_active_record(config) @adapter = ar_to_sequel(config[:adapter]) @database = config[:database] @user = config[:username] @password = config[:password] @host = config[:host] @port = config[:port] @options = (config.keys - STANDARD).map {|k| [k, config[k]]} self end |
#from_sequel(config) ⇒ DatabaseSpecification
Performs the work of setting up from a Sequel style hash
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/database_specification.rb', line 72 def from_sequel(config) @adapter = config[:adapter] @database = config[:database] @user = config[:user] @password = config[:password] @host = config[:host] @port = config[:port] @options = (config.keys - STANDARD).map {|k| [k, config[k]]} self end |
#from_url(url) ⇒ DatabaseSpecification
Performs the work of setging up from a url
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/database_specification.rb', line 86 def from_url(url) begin uri = URI.parse(url) rescue URI::InvalidURIError raise "Invalid DATABASE_URL" end @adapter = uri.scheme @database = uri.path.split('/')[1] @user = uri.user @password = uri.password @host = uri.host @port = uri.port @options = parse_query(uri.query) if @adapter == 'sqlite' @database = [@host, @database].join('/') @host = nil end self end |
#sequel ⇒ Hash
Returns Sequel style hash.
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/database_specification.rb', line 120 def sequel Hash[[ [:adapter, adapter], [:database, database], [:user, user], [:password, password], [:host, host], [:port, port], ].select {|x| x.last} + ] end |
#url ⇒ String
Returns DB url with query paramters.
132 133 134 |
# File 'lib/database_specification.rb', line 132 def url [, query].compact.join('?') end |
#url_bare ⇒ String
Returns DB url without query parameters.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/database_specification.rb', line 137 def if adapter == 'postgres' h = host || 'localhost' else h = host end uri = URI::Generic.build({ :scheme => adapter, :userinfo => userinfo, :host => h, :port => port, :path => '/' + database, # wants an absolute component }).to_s # URI is overly agressive in removing leading / if adapter == 'sqlite' uri.gsub!(':/', '://') end uri end |