bbk-utils 1.1.0.149110 → 1.1.0.273317

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b5a90e5fb2e9e12fcce7025e4db9358853b9b277c43660cb23d168dabd2957ed
4
- data.tar.gz: 9133ac9e0fca489c65abe5b6702429fbb750537b1f092d5a6b0481caaf8c746c
3
+ metadata.gz: 92bd4633caa38405fd9ec5f01f101842716c0808c3bc86776c4880d0120006c1
4
+ data.tar.gz: 3593415e0eec611b5ca305a4d5ba6f59c92591943b00c9c96415f5bfc6441b4a
5
5
  SHA512:
6
- metadata.gz: 760c5aedb896030e63fde2ba5ad280f0e2ecc5ad19834c6e72fd7a6eb9551549b7750d085253a72999a49633f08862dc6eb5af9053615d70cf07fa9c41a381f7
7
- data.tar.gz: f83185cf18a0658798b325ce78308d72e29585aea238340a90c33b9c56e654886d717b5bd1886617052f0fb4a5f646a2f176f1b177fafc2c4efba1c543b64cc3
6
+ metadata.gz: 90f54d3498b381d24f737147ce5eaf7f5ca717a44c2e830ff307fa41616f5866e5c2f3dba3fe1495904b15da1480d24691f90113c97ede2ea1d4e04c7c7d532c
7
+ data.tar.gz: 3306151c09eba11399f139134d65457d52d355fd7b23a59cb056ba2a6f901c4e7744d47acb0d56fe5d86ce3441ad1dde05589f46a3b2beb6d2fbdec0d113f399
data/Gemfile.lock CHANGED
@@ -1,17 +1,25 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bbk-utils (1.1.0.149110)
4
+ bbk-utils (1.1.0.273317)
5
5
  activesupport (>= 7.0)
6
6
  russian
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activesupport (7.0.5)
11
+ activesupport (7.1.5)
12
+ base64
13
+ benchmark (>= 0.3)
14
+ bigdecimal
12
15
  concurrent-ruby (~> 1.0, >= 1.0.2)
16
+ connection_pool (>= 2.2.5)
17
+ drb
13
18
  i18n (>= 1.6, < 2)
19
+ logger (>= 1.4.2)
14
20
  minitest (>= 5.1)
21
+ mutex_m
22
+ securerandom (>= 0.3)
15
23
  tzinfo (~> 2.0)
16
24
  addressable (2.8.0)
17
25
  public_suffix (>= 2.0.2, < 5.0)
@@ -21,14 +29,19 @@ GEM
21
29
  descendants_tracker (~> 0.0.4)
22
30
  ice_nine (~> 0.11.0)
23
31
  thread_safe (~> 0.3, >= 0.3.1)
32
+ base64 (0.2.0)
33
+ benchmark (0.4.0)
34
+ bigdecimal (3.1.8)
24
35
  byebug (11.1.3)
25
36
  coercible (1.0.0)
26
37
  descendants_tracker (~> 0.0.1)
27
38
  concurrent-ruby (1.2.2)
39
+ connection_pool (2.4.1)
28
40
  descendants_tracker (0.0.4)
29
41
  thread_safe (~> 0.3, >= 0.3.1)
30
42
  diff-lcs (1.4.4)
31
43
  docile (1.4.0)
44
+ drb (2.2.1)
32
45
  equalizer (0.0.11)
33
46
  erubis (2.7.0)
34
47
  faker (2.19.0)
@@ -48,7 +61,9 @@ GEM
48
61
  kwalify (0.7.2)
49
62
  launchy (2.5.0)
50
63
  addressable (~> 2.7)
51
- minitest (5.18.0)
64
+ logger (1.6.1)
65
+ minitest (5.25.1)
66
+ mutex_m (0.2.0)
52
67
  parallel (1.21.0)
53
68
  parser (3.0.3.1)
54
69
  ast (~> 2.4.1)
@@ -106,6 +121,7 @@ GEM
106
121
  virtus (~> 1.0)
107
122
  russian (0.6.0)
108
123
  i18n (>= 0.5.0)
124
+ securerandom (0.3.2)
109
125
  sexp_processor (4.16.0)
110
126
  simplecov (0.21.2)
111
127
  docile (~> 1.1)
@@ -4,7 +4,7 @@ module BBK
4
4
  module Utils
5
5
  class Config
6
6
 
7
- PREFIX_SEP = '_'.freeze
7
+ PREFIX_SEP = '_'
8
8
 
9
9
  attr_accessor :store, :name
10
10
  attr_reader :prefix, :env_prefix, :parent
@@ -60,8 +60,11 @@ module BBK
60
60
  @env_prefix = normalize_key(@prefixes.join(PREFIX_SEP))
61
61
  end
62
62
 
63
- def map(env, file, required: true, desc: nil, bool: false, key: nil)
64
- @store[full_prefixed_key(env)] = {
63
+ def map(env, file, required: true, desc: nil, bool: false, key: nil, rewrite: true)
64
+ conf_key = full_prefixed_key(env)
65
+ return if @store.key?(conf_key) && !rewrite
66
+
67
+ @store[conf_key] = {
65
68
  env: full_prefixed_key(key || env),
66
69
  file: file,
67
70
  required: required,
@@ -71,11 +74,14 @@ module BBK
71
74
  }
72
75
  end
73
76
 
74
- def require(env, desc: nil, bool: false, type: nil, key: nil)
77
+ def require(env, desc: nil, bool: false, type: nil, key: nil, rewrite: true)
75
78
  raise ArgumentError.new('Specified type and bool') if bool && type.present?
76
79
 
77
80
  type = BBK::Config::BooleanCaster.singleton_method(:cast) if bool
78
- @store[full_prefixed_key(env)] = {
81
+ conf_key = full_prefixed_key(env)
82
+ return if @store.key?(conf_key) && !rewrite
83
+
84
+ @store[conf_key] = {
79
85
  env: full_prefixed_key(key || env),
80
86
  file: nil,
81
87
  required: true,
@@ -85,11 +91,14 @@ module BBK
85
91
  }
86
92
  end
87
93
 
88
- def optional(env, default: nil, desc: nil, bool: false, type: nil, key: nil)
94
+ def optional(env, default: nil, desc: nil, bool: false, type: nil, key: nil, rewrite: true)
89
95
  raise ArgumentError.new('Specified type and bool') if bool && type.present?
90
96
 
91
97
  type = BBK::Utils::Config::BooleanCaster.singleton_method(:cast) if bool
92
- @store[full_prefixed_key(env)] = {
98
+ conf_key = full_prefixed_key(env)
99
+ return if @store.key?(conf_key) && !rewrite
100
+
101
+ @store[conf_key] = {
93
102
  env: full_prefixed_key(key || env),
94
103
  file: nil,
95
104
  required: false,
@@ -104,17 +113,16 @@ module BBK
104
113
  @store.each_value do |item|
105
114
  process(source, item)
106
115
  end
107
- @subconfigs.each {|sub| sub.run!(source)}
116
+ @subconfigs.each {|sub| sub.run!(source) }
108
117
  nil
109
118
  end
110
119
 
111
- def subconfig(prefix: , name: nil)
120
+ def subconfig(prefix:, name: nil)
112
121
  raise ArgumentError.new("Subconfig with prefix #{prefix} already exists") if @subconfigs.any? {|sub| sub.prefix == prefix.to_s }
122
+
113
123
  sub = self.class.new(name: name, prefix: prefix, parent: self)
114
124
  @subconfigs << sub
115
- if block_given?
116
- yield sub
117
- end
125
+ yield sub if block_given?
118
126
  sub
119
127
  end
120
128
 
@@ -187,48 +195,43 @@ module BBK
187
195
 
188
196
  protected
189
197
 
190
- def prefixes
191
- @prefixes
192
- end
198
+ attr_reader :prefixes
193
199
 
194
- def get(key, search_up: false, search_down: false)
195
- normalized_key = normalize_key(key)
196
- if @store.key?(normalized_key)
197
- return @store[normalized_key]
198
- end
199
- prefix_key = full_prefixed_key(key)
200
- if @store.key?(prefix_key)
201
- return @store[prefix_key]
202
- end
203
- if search_down
204
- sub_prefixed_keys(key).each do |pref_key|
205
- if @store.key?(pref_key)
206
- return @store[pref_key]
207
- end
200
+ def get(key, search_up: false, search_down: false)
201
+ normalized_key = normalize_key(key)
202
+ return @store[normalized_key] if @store.key?(normalized_key)
203
+
204
+ prefix_key = full_prefixed_key(key)
205
+ return @store[prefix_key] if @store.key?(prefix_key)
208
206
 
209
- subconf = @subconfigs.find {|sub| pref_key.starts_with?(sub.env_prefix)}
210
- next if subconf.nil?
211
- return subconf.get(pref_key, search_up: false, search_down: true)
207
+ if search_down
208
+ sub_prefixed_keys(key).each do |pref_key|
209
+ return @store[pref_key] if @store.key?(pref_key)
210
+
211
+ subconf = @subconfigs.find {|sub| pref_key.starts_with?(sub.env_prefix) }
212
+ next if subconf.nil?
213
+
214
+ return subconf.get(pref_key, search_up: false, search_down: true)
215
+ end
212
216
  end
217
+ return @parent.get(key, search_up: true, search_down: false) if search_up && @parent
218
+
219
+ raise KeyError.new("There is no such key: #{key} in config!")
213
220
  end
214
- if search_up && @parent
215
- return @parent.get(key, search_up: true, search_down: false)
216
- end
217
- raise KeyError.new("There is no such key: #{key} in config!")
218
- end
219
221
 
220
- def store_with_subconfigs
221
- res = @store.dup
222
- @subconfigs.each do |sub|
223
- res = res.merge(sub.store_with_subconfigs)
222
+ def store_with_subconfigs
223
+ res = @store.dup
224
+ @subconfigs.each do |sub|
225
+ res = res.merge(sub.store_with_subconfigs)
226
+ end
227
+ res
224
228
  end
225
- res
226
- end
227
229
 
228
230
  private
229
231
 
230
232
  def normalize_key(key)
231
233
  return nil if key.nil?
234
+
232
235
  key.to_s.upcase.gsub('-', '_')
233
236
  end
234
237
 
@@ -6,9 +6,11 @@ module BBK
6
6
  module Utils
7
7
  module EnvHelper
8
8
 
9
- def self.prepare_database_envs(env)
10
- uri = build_uri_with_defaults(env)
11
- apply_env_from_uri(env, uri)
9
+ DEFAULT_DATABASE_PREFIX = 'DATABASE'
10
+
11
+ def self.prepare_database_envs(env, prefix: DEFAULT_DATABASE_PREFIX)
12
+ uri = build_uri_with_defaults(env, prefix: prefix)
13
+ apply_env_from_uri(env, uri, prefix: prefix)
12
14
  env
13
15
  end
14
16
 
@@ -30,38 +32,38 @@ module BBK
30
32
  env
31
33
  end
32
34
 
33
- def self.build_uri_with_defaults(env)
34
- ::URI.parse(env['DATABASE_URL'] || '').tap do |uri|
35
- uri.scheme = env.fetch('DATABASE_ADAPTER', uri.scheme) || 'postgresql'
36
- uri.user = env.fetch('DATABASE_USER', uri.user) || 'postgres'
37
- uri.password = env.fetch('DATABASE_PASS', uri.password)
38
- uri.hostname = env.fetch('DATABASE_HOST', uri.hostname) || 'db'
39
- uri.port = env.fetch('DATABASE_PORT', uri.port) || 5432
35
+ def self.build_uri_with_defaults(env, prefix: DEFAULT_DATABASE_PREFIX)
36
+ ::URI.parse(env[prefixed_key(prefix, 'URL')] || '').tap do |uri|
37
+ uri.scheme = env.fetch(prefixed_key(prefix, 'ADAPTER'), uri.scheme) || 'postgresql'
38
+ uri.user = env.fetch(prefixed_key(prefix, 'USER'), uri.user) || 'postgres'
39
+ uri.password = env.fetch(prefixed_key(prefix, 'PASS'), uri.password)
40
+ uri.hostname = env.fetch(prefixed_key(prefix, 'HOST'), uri.hostname) || 'db'
41
+ uri.port = env.fetch(prefixed_key(prefix, 'PORT'), uri.port) || 5432
40
42
 
41
- name = env.fetch('DATABASE_NAME', uri.path) || ''
43
+ name = env.fetch(prefixed_key(prefix, 'NAME'), uri.path) || ''
42
44
  name = "/#{name}" unless name.start_with?('/')
43
45
  uri.path = name
44
46
 
45
47
  if uri.query
46
48
  params = URI.decode_www_form(uri.query).to_h
47
- params['pool'] = env.fetch('DATABASE_POOL', params['pool'])
49
+ params['pool'] = env.fetch(prefixed_key(prefix, 'POOL'), params['pool'])
48
50
  uri.query = URI.encode_www_form(params)
49
51
  end
50
52
  end
51
53
  end
52
54
 
53
- def self.apply_env_from_uri(env, uri)
54
- env['DATABASE_URL'] = uri.to_s
55
- env['DATABASE_ADAPTER'] = uri.scheme
56
- env['DATABASE_USER'] = uri.user
57
- env['DATABASE_PASS'] = uri.password
58
- env['DATABASE_HOST'] = uri.hostname
59
- env['DATABASE_PORT'] = uri.port.to_s
60
- env['DATABASE_NAME'] = uri.path[1..-1]
55
+ def self.apply_env_from_uri(env, uri, prefix: DEFAULT_DATABASE_PREFIX)
56
+ env[prefixed_key(prefix, 'URL')] = uri.to_s
57
+ env[prefixed_key(prefix, 'ADAPTER')] = uri.scheme
58
+ env[prefixed_key(prefix, 'USER')] = uri.user
59
+ env[prefixed_key(prefix, 'PASS')] = uri.password
60
+ env[prefixed_key(prefix, 'HOST')] = uri.hostname
61
+ env[prefixed_key(prefix, 'PORT')] = uri.port.to_s
62
+ env[prefixed_key(prefix, 'NAME')] = uri.path[1..-1]
61
63
 
62
64
  if uri.query
63
65
  params = URI.decode_www_form(uri.query).to_h
64
- env['DATABASE_POOL'] = params['pool']
66
+ env[prefixed_key(prefix, 'POOL')] = params['pool']
65
67
  end
66
68
  end
67
69
 
@@ -105,6 +107,11 @@ module BBK
105
107
  env['MQ_VHOST'] = vhost
106
108
  end
107
109
 
110
+
111
+ def self.prefixed_key(prefix, name)
112
+ [prefix, name].select(&:present?).join('_')
113
+ end
114
+
108
115
  end
109
116
  end
110
117
  end
data/sig/bbk/config.rbs CHANGED
@@ -17,7 +17,7 @@ module BBK
17
17
 
18
18
  type typeCaster = _CallableCaster | _ClassCaster
19
19
  type configItem = {env: String, file: String?, required: bool, desc: String?, bool: bool, value: untyped, default: untyped, type: typeCaster?}
20
- type envSource = _ENV | ENVClass
20
+ type envSource = ENVClass | _ENV
21
21
 
22
22
  attr_accessor store: untyped
23
23
  attr_accessor name: String?
@@ -25,9 +25,9 @@ module BBK
25
25
  def self.instance: () -> instance
26
26
 
27
27
  def initialize: (?name: String?, ?prefix: String?, ?parent: BBK::Utils::Config?) -> void
28
- def map: (envSource env, String file, ?required: bool, ?desc: String, ?bool: bool, ?key: String) -> void
29
- def require: (envSource env, ?desc: String?, ?bool: bool, ?type: typeCaster, ?key: String?) -> void
30
- def optional: (envSource env, ?default: untyped, ?desc: String, ?bool: bool, ?type: typeCaster, ?key: String) -> void
28
+ def map: (envSource env, String file, ?required: bool, ?desc: String, ?bool: bool, ?key: String, ?rewrite: bool) -> void
29
+ def require: (envSource env, ?desc: String?, ?bool: bool, ?type: typeCaster, ?key: String?, ?rewrite: bool) -> void
30
+ def optional: (envSource env, ?default: untyped, ?desc: String, ?bool: bool, ?type: typeCaster, ?key: String, ?rewrite: bool) -> void
31
31
 
32
32
  def run!: (?envSource source) -> void
33
33
  def []: (String key) -> untyped
@@ -2,11 +2,11 @@ module BBK
2
2
  module Utils
3
3
  module EnvHelper
4
4
 
5
- def self.prepare_database_envs: (Hash[String, untyped]|::_ENV env ) -> (Hash[String,untyped]|::_ENV)
5
+ def self.prepare_database_envs: (Hash[String, untyped]|::_ENV env, ?prefix: String) -> (Hash[String,untyped]|::_ENV)
6
6
  def self.prepare_mq_envs: (Hash[String, untyped]|::_ENV env) -> (Hash[String, untyped]|::_ENV)
7
7
  def self.prepare_jaeger_envs: (Hash[String, untyped]|::_ENV env) -> (Hash[String, untyped]|::_ENV)
8
- def self.build_uri_with_defaults: (Hash[String, untyped]|::_ENV env) -> URI::Generic
9
- def self.apply_env_from_uri: (Hash[String, untyped]|::_ENV env, URI::Generic uri) -> void
8
+ def self.build_uri_with_defaults: (Hash[String, untyped]|::_ENV env, ?prefix: String) -> URI::Generic
9
+ def self.apply_env_from_uri: (Hash[String, untyped]|::_ENV env, URI::Generic uri, ?prefix: String) -> void
10
10
  def self.build_mq_uri_with_defaults: (Hash[String, untyped]|::_ENV env) -> Array[URI::Generic]
11
11
  def self.apply_mq_env_from_uri: (Hash[String, untyped]|::_ENV env, Array[URI::Generic] uris) -> void
12
12
  end
data/sig/env.rbs CHANGED
@@ -1,4 +1,4 @@
1
1
  interface _ENV
2
- def []: (String) -> String
2
+ def []: (String) -> String?
3
3
  def []=: (String) -> String
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bbk-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0.149110
4
+ version: 1.1.0.273317
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samoilenko Yuri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-06 00:00:00.000000000 Z
11
+ date: 2024-11-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -257,7 +257,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
257
257
  - !ruby/object:Gem::Version
258
258
  version: '0'
259
259
  requirements: []
260
- rubygems_version: 3.2.32
260
+ rubygems_version: 3.2.33
261
261
  signing_key:
262
262
  specification_version: 4
263
263
  summary: Support classes for BBK stack