Class: Gorynich::Config
- Inherits:
-
Object
- Object
- Gorynich::Config
- Defined in:
- lib/gorynich/config.rb
Instance Attribute Summary collapse
-
#databases ⇒ Object
readonly
Returns the value of attribute databases.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#fetcher ⇒ Object
readonly
Returns the value of attribute fetcher.
-
#hosts ⇒ Object
readonly
Returns the value of attribute hosts.
-
#tenants ⇒ Object
readonly
Returns the value of attribute tenants.
-
#uris ⇒ Object
readonly
Returns the value of attribute uris.
Instance Method Summary collapse
-
#actualize ⇒ Object
Update configs from data source.
-
#config(tenant) ⇒ Hash
Full config from data source by tenant.
-
#connects_to_config ⇒ Hash
For connection to ActiveRecord.
-
#database(tenant) ⇒ Hash
Database config.
-
#database_config(env = nil, fail_ignore: false, dummy_value: nil) ⇒ String
Database config for database.yml If used <%= Gorynich.instance.database_config(your_enviroment) %> without gorynich config it will raise an error.
-
#initialize(**opts) ⇒ Config
constructor
Create instance of config.
-
#tenant_by_host(host) ⇒ String
Find tenant by host.
-
#tenant_by_uri(uri) ⇒ String
Find tenant by URI.
-
#uri_by_host(host, tenant = nil) ⇒ String
Find URI by host.
Constructor Details
Instance Attribute Details
#databases ⇒ Object (readonly)
Returns the value of attribute databases.
3 4 5 |
# File 'lib/gorynich/config.rb', line 3 def databases @databases end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
3 4 5 |
# File 'lib/gorynich/config.rb', line 3 def default @default end |
#fetcher ⇒ Object (readonly)
Returns the value of attribute fetcher.
3 4 5 |
# File 'lib/gorynich/config.rb', line 3 def fetcher @fetcher end |
#hosts ⇒ Object (readonly)
Returns the value of attribute hosts.
3 4 5 |
# File 'lib/gorynich/config.rb', line 3 def hosts @hosts end |
#tenants ⇒ Object (readonly)
Returns the value of attribute tenants.
3 4 5 |
# File 'lib/gorynich/config.rb', line 3 def tenants @tenants end |
#uris ⇒ Object (readonly)
Returns the value of attribute uris.
3 4 5 |
# File 'lib/gorynich/config.rb', line 3 def uris @uris end |
Instance Method Details
#actualize ⇒ Object
Update configs from data source
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/gorynich/config.rb', line 26 def actualize cfg = fetcher.fetch.fetch(Rails.env) @mx.synchronize do @tenants = tenants_from_config(cfg) @databases = databases_from_config(cfg) @secrets = secrets_from_config(cfg) @uris = uris_from_config(@secrets) @hosts = hosts_from_config(@secrets) end end |
#config(tenant) ⇒ Hash
Full config from data source by tenant
119 120 121 122 123 124 125 |
# File 'lib/gorynich/config.rb', line 119 def config(tenant) { tenant: tenant.to_s, database: database(tenant), secrets: secrets(tenant) } end |
#connects_to_config ⇒ Hash
For connection to ActiveRecord
169 170 171 172 173 174 |
# File 'lib/gorynich/config.rb', line 169 def connects_to_config actualize tenants.each_with_object({ default: :default }) do |tenant, cfg| cfg[tenant.to_sym] = tenant.to_sym end end |
#database(tenant) ⇒ Hash
Database config
45 46 47 48 49 |
# File 'lib/gorynich/config.rb', line 45 def database(tenant) databases.fetch(tenant.to_s) rescue StandardError raise TenantNotFound, tenant end |
#database_config(env = nil, fail_ignore: false, dummy_value: nil) ⇒ String
Database config for database.yml If used <%= Gorynich.instance.database_config(your_enviroment) %> without gorynich config it will raise an error. You can use <%= Gorynich.instance.database_config(your_enviroment, fail_ignore: true) %> if there is no configuration for the environment and dummy_value depending on whether you have a configuration in the database.yml under this environment (dummy_value: nil when there is and dummy_value: {} if there is not. Default dummy_value is nil)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/gorynich/config.rb', line 139 def database_config(env = nil, fail_ignore: false, dummy_value: nil) envs = Dir.glob(Rails.root.join('config/environments/*.rb').to_s).map { |f| File.basename(f, '.rb') } cfg = fetcher.fetch.extract!(*envs) result = if env.nil? cfg.to_h do |cfg_env, tenant_cfg| [ cfg_env, configs_sort(tenant_cfg).to_h { |t, c| [t, c.fetch('db_config')] } ] end else if fail_ignore && cfg[env].nil? { env => dummy_value } else { env => configs_sort(cfg.fetch(env)).to_h { |t, c| [t, c.fetch('db_config')] } } end end result.to_yaml.gsub(/^---/, '') end |
#tenant_by_host(host) ⇒ String
Find tenant by host
87 88 89 90 91 92 |
# File 'lib/gorynich/config.rb', line 87 def tenant_by_host(host) tenant = hosts.select { |t, h| t if h.include?(host) }.keys.first raise HostNotFound, host if tenant.nil? tenant end |
#tenant_by_uri(uri) ⇒ String
Find tenant by URI
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/gorynich/config.rb', line 69 def tenant_by_uri(uri) uri = URI(uri) search_tenant = uris.select do |tenant, tenant_uris| tenant if tenant_uris.map { |t_uri| URI(t_uri) }.include?(uri) end.keys.first raise UriNotFound, uri.host if search_tenant.nil? search_tenant end |
#uri_by_host(host, tenant = nil) ⇒ String
Find URI by host
102 103 104 105 106 107 108 109 110 |
# File 'lib/gorynich/config.rb', line 102 def uri_by_host(host, tenant = nil) tenant ||= tenant_by_host(host) tenant_uris = uris(tenant) search_uri = tenant_uris.select { |uri| uri.include?(host) }.first raise UriNotFound, search_uri.host if search_uri.nil? search_uri end |