Class: DatabaseSpecification

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adapterObject (readonly)

The database driver type: postgres, sqlite, etc.



16
17
18
# File 'lib/database_specification.rb', line 16

def adapter
  @adapter
end

#databaseObject (readonly)

Database name within the server



18
19
20
# File 'lib/database_specification.rb', line 18

def database
  @database
end

#hostObject (readonly)

Returns the value of attribute host.



21
22
23
# File 'lib/database_specification.rb', line 21

def host
  @host
end

#optionsObject (readonly)

Misc extra options, ex: pool, timeout



24
25
26
# File 'lib/database_specification.rb', line 24

def options
  @options
end

#passwordObject (readonly)

Returns the value of attribute password.



20
21
22
# File 'lib/database_specification.rb', line 20

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



22
23
24
# File 'lib/database_specification.rb', line 22

def port
  @port
end

#userObject (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

Parameters:

  • config (Hash)

    ActiveRecord style db hash



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

Parameters:

  • config (Hash)

    Sequel style db hash



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)

Parameters:

  • url (String)


51
52
53
# File 'lib/database_specification.rb', line 51

def self.url(url)
  new.from_url(url)
end

Instance Method Details

#active_recordHash

Returns ActiveRecord style hash.

Returns:

  • (Hash)

    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} + options]
end

#from_active_record(config) ⇒ DatabaseSpecification

Performs the work of setting up from an ActiveRecord style hash

Parameters:

  • config (Hash)

    ActiveRecord style db hash

Returns:



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

Parameters:

  • config (Hash)

    Sequel style db hash

Returns:



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

Parameters:

  • url (String)

Returns:



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

#sequelHash

Returns Sequel style hash.

Returns:

  • (Hash)

    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} + options]
end

#urlString

Returns DB url with query paramters.

Returns:

  • (String)

    DB url with query paramters



132
133
134
# File 'lib/database_specification.rb', line 132

def url
  [url_bare, query].compact.join('?')
end

#url_bareString

Returns DB url without query parameters.

Returns:

  • (String)

    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 url_bare
  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