Class: Knockoff::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/knockoff/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



13
14
15
16
17
18
19
20
21
22
# File 'lib/knockoff/config.rb', line 13

def initialize
  @environment = 'development'
  @replicas_configurations = {}
  set_replica_configs

  if !properly_configured? && Knockoff.enabled
    puts "[Knockoff] WARNING: Detected enabled Knockoff without proper replica pool configuration. Setting Knockoff.enabled to false."
    Knockoff.enabled = false
  end
end

Instance Attribute Details

#environmentObject (readonly)

The current environment. Normally set to Rails.env, but will default to ‘development’ outside of Rails apps.



5
6
7
# File 'lib/knockoff/config.rb', line 5

def environment
  @environment
end

#replica_configsObject (readonly)

An array of configs to use for the replica pool.



8
9
10
# File 'lib/knockoff/config.rb', line 8

def replica_configs
  @replica_configs
end

#replicas_configurationsObject (readonly)

A hash of replica configs to their config hash.



11
12
13
# File 'lib/knockoff/config.rb', line 11

def replicas_configurations
  @replicas_configurations
end

Instance Method Details

#properly_configured?Boolean

If replica_configs actually containts some configuration information, then we know it was properly configured. Improper URI’s will be ignored during the initialization step.

Returns:

  • (Boolean)


46
47
48
# File 'lib/knockoff/config.rb', line 46

def properly_configured?
  !@replica_configs.empty?
end

#replica_database_keysObject



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

def replica_database_keys
  @replicas_configurations.keys
end

#replica_env_keysObject



28
29
30
31
32
33
34
# File 'lib/knockoff/config.rb', line 28

def replica_env_keys
  if ENV['KNOCKOFF_REPLICA_ENVS'].nil?
    []
  else
    ENV['KNOCKOFF_REPLICA_ENVS'].split(',').map(&:strip)
  end
end

#update_replica_configs(new_configs) ⇒ Object



36
37
38
39
40
41
# File 'lib/knockoff/config.rb', line 36

def update_replica_configs(new_configs)
  ActiveRecord::Base.configurations['knockoff_replicas'].merge(new_configs) if ActiveRecord::Base.configurations['knockoff_replicas'].present?
  @replicas_configurations.each do |key, _config|
    update_replica_config(key, new_configs)
  end
end