Class: ActivePostgres::Rails::DatabaseConfig

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

Defined Under Namespace

Classes: Builder

Constant Summary collapse

DEFAULT_ROLE =
'primary'.freeze

Class Method Summary collapse

Class Method Details

.generate(environment = 'production', app_name: nil, config: nil) ⇒ Object



8
9
10
11
# File 'lib/active_postgres/rails/database_config.rb', line 8

def self.generate(environment = 'production', app_name: nil, config: nil)
  builder = Builder.new(environment, app_name: app_name, config: config)
  builder.build_connections
end

.render(environment = 'production', app_name: nil, config: nil) ⇒ Object



13
14
15
16
# File 'lib/active_postgres/rails/database_config.rb', line 13

def self.render(environment = 'production', app_name: nil, config: nil)
  connections = generate(environment, app_name: app_name, config: config)
  render_yaml(environment, connections)
end

.render_hash(hash, indent: 0) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/active_postgres/rails/database_config.rb', line 27

def self.render_hash(hash, indent: 0)
  output = ''
  hash.each do |key, value|
    spaces = ' ' * indent
    if value.is_a?(Hash)
      output += "#{spaces}#{key}:\n"
      output += render_hash(value, indent: indent + 2)
    elsif value.to_s.include?('<%=')
      # Don't quote ERB tags
      output += "#{spaces}#{key}: #{value}\n"
    elsif value.is_a?(String)
      output += "#{spaces}#{key}: #{value}\n"
    elsif [true, false].include?(value)
      output += "#{spaces}#{key}: #{value}\n"
    elsif value.is_a?(Integer)
      output += "#{spaces}#{key}: #{value}\n"
    else
      output += "#{spaces}#{key}: #{value}\n"
    end
  end
  output
end

.render_partial(environment = 'production', app_name: nil, config: nil) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/active_postgres/rails/database_config.rb', line 50

def self.render_partial(environment = 'production', app_name: nil, config: nil)
  <<~YAML
    # Generated by active_postgres. This file is evaluated via ERB from config/database.yml.
    # Update config/postgres.yml or the POSTGRES_* environment variables to change values.

    #{render(environment, app_name: app_name, config: config)}
  YAML
end

.render_yaml(environment, connections) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/active_postgres/rails/database_config.rb', line 18

def self.render_yaml(environment, connections)
  output = "#{environment}:\n"
  connections.each do |role, config|
    output += "  #{role}:\n"
    output += render_hash(config, indent: 4)
  end
  output
end