Class: RailsDbConfigResolver::DatabaseConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_db_config_resolver/database_config.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabaseConfig

Returns a new instance of DatabaseConfig.



30
31
32
33
34
# File 'lib/rails_db_config_resolver/database_config.rb', line 30

def initialize(*)
  super

  @pool = cast_to_integer_or_nil(@pool)
end

Class Method Details

.from_url(url_string) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rails_db_config_resolver/database_config.rb', line 9

def self.from_url(url_string)
  url = URI.parse(url_string)

  pool = (url.query.match(/pool=(\d+)/)[1] if url.query)
  database = url.path[1..-1]

  new(
    adapter: url.scheme,
    host: url.host,
    port: url.port,
    username: url.user,
    password: url.password,
    database: database,
    pool: pool
  )
end

Instance Method Details

#merge(other) ⇒ Object



52
53
54
55
56
# File 'lib/rails_db_config_resolver/database_config.rb', line 52

def merge(other)
  my_hash = to_hash
  other_hash = other.to_hash.reject { |_, value| value.nil? }
  self.class.from_hash(my_hash.merge(other_hash))
end

#to_hashObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rails_db_config_resolver/database_config.rb', line 36

def to_hash
  {
    adapter: @adapter,
    host: @host,
    port: @port,
    username: @username,
    password: @password,
    database: @database,
    pool: @pool
  }
end

#to_urlObject



48
49
50
# File 'lib/rails_db_config_resolver/database_config.rb', line 48

def to_url
  URI::Generic.build(build_url_hash).to_s
end