anyway_config 2.4.0 → 2.4.1

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: 44a860f50ae339b170da1a4ea99a7c6125b8ddcf2e25269c7341150bb8b8cf1f
4
- data.tar.gz: f39c3ff413a1f334ad85a8f3eb0dfabca16dfc0c18c7f8719236607a777f947d
3
+ metadata.gz: dd660b7594fd85e041d4f00a4abbadef72085256e250df638cd239a3512db58c
4
+ data.tar.gz: 94fa4463eaf4bf2d64cddba05ba1fac18e635f62ff89620f8a460c9cf0162535
5
5
  SHA512:
6
- metadata.gz: 6b903f239c176e8c031fdea2d81f9d6d46d0657c2b5525f7e36fed495ab2410dc017bbe676fdc8c633d6b303c619e5caeb109d8c172d172720bfb3417746c612
7
- data.tar.gz: 7bbb6db11016ce9c9473009c49aa676e3121ab921a2493ceb6d8e0e270cf75369935678bb1a2e5f69ebe387be1d5a44d159df819dcfab4a591aa67bab2e757fa
6
+ metadata.gz: a1585d343611b50d1ca021dfbd2dfb49f92b8aad835c7f3c48aabeab4d5cc905877db4ef57a86e51b59c53f9b1192f69c7b58af4ec6c1b182447b146e19b4a3b
7
+ data.tar.gz: 983a3079303037e6cda97057ad51ef2479000baa1e975d481a7c2f4cd2510605fd36227d3692b1a82f7a85db333a7909e5610515286e1c10adc0aeb0e11f3006
data/CHANGELOG.md CHANGED
@@ -2,9 +2,15 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 2.4.1 (2023-05-04)
6
+
7
+ - Add custom namespace support via `ejson_namespace` ([@bessey])
8
+
9
+ - Add arbitrary custom loader options support via `loader_options` ([@bessey])
10
+
5
11
  ## 2.4.0 (2023-04-04)
6
12
 
7
- - Add `Confi#as_env` to convert config into a ENV-like HASH. ([@tagirahmad][])
13
+ - Added `Confi#as_env` to convert config into a ENV-like Hash. ([@tagirahmad][])
8
14
 
9
15
  - Added experimental support for sub-configs via coercion. ([@palkan][])
10
16
 
@@ -517,3 +523,4 @@ No we're dependency-free!
517
523
  [@prog-supdex]: https://github.com/prog-supdex
518
524
  [@inner-whisper]: https://github.com/inner-whisper
519
525
  [@tagirahmad]: https://github.com/tagirahmad
526
+ [@bessey]: https://github.com/bessey
data/README.md CHANGED
@@ -544,6 +544,8 @@ Would you like to generate a heroku.yml file? (Y/n) n
544
544
  You can also specify the `--app` option to put the newly created class into `app/configs` folder.
545
545
  Alternatively, you can call `rails g anyway:app_config name param1 param2 ...`.
546
546
 
547
+ **NOTE:** The generated `ApplicationConfig` class uses a singleton pattern along with `delegate_missing_to` to re-use the same instance across the application. However, the delegation can lead to unexpected behaviour and break Anyway Config internals if you have attributes named as `Anyway::Config` class methods. See [#120](https://github.com/palkan/anyway_config/issues/120).
548
+
547
549
  ## Using with Ruby
548
550
 
549
551
  The default data loading mechanism for non-Rails applications is the following (ordered by priority from low to high):
@@ -790,6 +792,18 @@ To debug any problems with loading configurations from `.ejson` files you can di
790
792
  ejson decrypt config/secrets.ejson
791
793
  ```
792
794
 
795
+ You can customize the JSON namespace under which a loader searches for configuration via `loader_options`:
796
+
797
+ ```ruby
798
+ class MyConfig < Anyway::Config
799
+ # To look under the key "foo" instead of the default key of "my"
800
+ loader_options ejson_namespace: "foo"
801
+
802
+ # Or to disable namespacing entirely, and instead search in the root object
803
+ loader_options ejson_namespace: false
804
+ end
805
+ ```
806
+
793
807
  ### Custom loaders
794
808
 
795
809
  You can provide your own data loaders or change the existing ones using the Loaders API (which is very similar to Rack middleware builder):
@@ -810,7 +824,8 @@ def call(
810
824
  name:, # config name
811
825
  env_prefix:, # prefix for env vars if any
812
826
  config_path:, # path to YML config
813
- local: # true|false, whether to load local configuration
827
+ local:, # true|false, whether to load local configuration
828
+ **options # custom options can be passed via Anyway::Config.loader_options example: "custom", option: "blah"
814
829
  )
815
830
  #=> must return Hash with configuration data
816
831
  end
@@ -199,6 +199,18 @@ module Anyway # :nodoc:
199
199
  end
200
200
  end
201
201
 
202
+ def loader_options(val = nil)
203
+ return (@loader_options = val) unless val.nil?
204
+
205
+ return @loader_options if instance_variable_defined?(:@loader_options)
206
+
207
+ @loader_options = if superclass < Anyway::Config
208
+ superclass.loader_options
209
+ else
210
+ {}
211
+ end
212
+ end
213
+
202
214
  def new_empty_config() ; {}; end
203
215
 
204
216
  def coerce_types(mapping)
@@ -343,7 +355,13 @@ module Anyway # :nodoc:
343
355
 
344
356
  config_path = resolve_config_path(config_name, env_prefix)
345
357
 
346
- load_from_sources(base_config, name: config_name, env_prefix: env_prefix, config_path: config_path)
358
+ load_from_sources(
359
+ base_config,
360
+ name: config_name,
361
+ env_prefix: env_prefix,
362
+ config_path: config_path,
363
+ **self.class.loader_options
364
+ )
347
365
 
348
366
  if overrides
349
367
  Tracing.trace!(:load) { overrides }
@@ -199,6 +199,18 @@ module Anyway # :nodoc:
199
199
  end
200
200
  end
201
201
 
202
+ def loader_options(val = nil)
203
+ return (@loader_options = val) unless val.nil?
204
+
205
+ return @loader_options if instance_variable_defined?(:@loader_options)
206
+
207
+ @loader_options = if superclass < Anyway::Config
208
+ superclass.loader_options
209
+ else
210
+ {}
211
+ end
212
+ end
213
+
202
214
  def new_empty_config() ; {}; end
203
215
 
204
216
  def coerce_types(mapping)
@@ -343,7 +355,13 @@ module Anyway # :nodoc:
343
355
 
344
356
  config_path = resolve_config_path(config_name, env_prefix)
345
357
 
346
- load_from_sources(base_config, name: config_name, env_prefix: env_prefix, config_path: config_path)
358
+ load_from_sources(
359
+ base_config,
360
+ name: config_name,
361
+ env_prefix: env_prefix,
362
+ config_path: config_path,
363
+ **self.class.loader_options
364
+ )
347
365
 
348
366
  if overrides
349
367
  Tracing.trace!(:load) { overrides }
@@ -199,6 +199,18 @@ module Anyway # :nodoc:
199
199
  end
200
200
  end
201
201
 
202
+ def loader_options(val = nil)
203
+ return (@loader_options = val) unless val.nil?
204
+
205
+ return @loader_options if instance_variable_defined?(:@loader_options)
206
+
207
+ @loader_options = if superclass < Anyway::Config
208
+ superclass.loader_options
209
+ else
210
+ {}
211
+ end
212
+ end
213
+
202
214
  def new_empty_config() = {}
203
215
 
204
216
  def coerce_types(mapping)
@@ -343,7 +355,13 @@ module Anyway # :nodoc:
343
355
 
344
356
  config_path = resolve_config_path(config_name, env_prefix)
345
357
 
346
- load_from_sources(base_config, name: config_name, env_prefix: env_prefix, config_path: config_path)
358
+ load_from_sources(
359
+ base_config,
360
+ name: config_name,
361
+ env_prefix: env_prefix,
362
+ config_path: config_path,
363
+ **self.class.loader_options
364
+ )
347
365
 
348
366
  if overrides
349
367
  Tracing.trace!(:load) { overrides }
data/lib/anyway/config.rb CHANGED
@@ -199,6 +199,18 @@ module Anyway # :nodoc:
199
199
  end
200
200
  end
201
201
 
202
+ def loader_options(val = nil)
203
+ return (@loader_options = val) unless val.nil?
204
+
205
+ return @loader_options if instance_variable_defined?(:@loader_options)
206
+
207
+ @loader_options = if superclass < Anyway::Config
208
+ superclass.loader_options
209
+ else
210
+ {}
211
+ end
212
+ end
213
+
202
214
  def new_empty_config() = {}
203
215
 
204
216
  def coerce_types(mapping)
@@ -343,7 +355,13 @@ module Anyway # :nodoc:
343
355
 
344
356
  config_path = resolve_config_path(config_name, env_prefix)
345
357
 
346
- load_from_sources(base_config, name: config_name, env_prefix:, config_path:)
358
+ load_from_sources(
359
+ base_config,
360
+ name: config_name,
361
+ env_prefix:,
362
+ config_path:,
363
+ **self.class.loader_options
364
+ )
347
365
 
348
366
  if overrides
349
367
  Tracing.trace!(:load) { overrides }
@@ -11,7 +11,7 @@ module Anyway
11
11
 
12
12
  self.bin_path = "ejson"
13
13
 
14
- def call(name:, ejson_parser: Anyway::EJSONParser.new(EJSON.bin_path), **_options)
14
+ def call(name:, ejson_namespace: name, ejson_parser: Anyway::EJSONParser.new(EJSON.bin_path), **_options)
15
15
  configs = []
16
16
 
17
17
  rel_config_paths.each do |rel_config_path|
@@ -23,7 +23,11 @@ module Anyway
23
23
 
24
24
  next unless secrets_hash
25
25
 
26
- config_hash = secrets_hash[name]
26
+ config_hash = if ejson_namespace
27
+ secrets_hash[ejson_namespace]
28
+ else
29
+ secrets_hash.except("_public_key")
30
+ end
27
31
 
28
32
  next unless config_hash.is_a?(Hash)
29
33
 
@@ -11,9 +11,10 @@ module Anyway
11
11
  class Settings
12
12
  class << self
13
13
  attr_reader :autoload_static_config_path, :autoloader
14
+ attr_writer :autoload_via_zeitwerk
14
15
 
15
- if defined?(::Zeitwerk)
16
- def autoload_static_config_path=(val)
16
+ def autoload_static_config_path=(val)
17
+ if autoload_via_zeitwerk
17
18
  raise "Cannot setup autoloader after application has been initialized" if ::Rails.application.initialized?
18
19
 
19
20
  return unless ::Rails.root.join(val).exist?
@@ -28,18 +29,16 @@ module Anyway
28
29
  # and Rails 7 https://github.com/rails/rails/blob/5462fbd5de1900c1b1ce1c9dc11c1a2d8cdcd809/railties/lib/rails/autoloaders.rb#L15
29
30
  @autoloader = Zeitwerk::Loader.new.tap do |loader|
30
31
  loader.tag = "anyway.config"
31
- loader.inflector = defined?(ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector) ? ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector : ::Rails::Autoloaders::Inflector
32
+
33
+ if defined?(ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector)
34
+ loader.inflector = ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector
35
+ elsif defined?(::Rails::Autoloaders::Inflector)
36
+ loader.inflector = ::Rails::Autoloaders::Inflector
37
+ end
32
38
  loader.push_dir(::Rails.root.join(val))
33
39
  loader.setup
34
40
  end
35
- end
36
-
37
- def cleanup_autoload_paths
38
- return unless autoload_static_config_path
39
- ActiveSupport::Dependencies.autoload_paths.delete(::Rails.root.join(autoload_static_config_path).to_s)
40
- end
41
- else
42
- def autoload_static_config_path=(val)
41
+ else
43
42
  if autoload_static_config_path
44
43
  old_path = ::Rails.root.join(autoload_static_config_path).to_s
45
44
  ActiveSupport::Dependencies.autoload_paths.delete(old_path)
@@ -51,10 +50,19 @@ module Anyway
51
50
  ActiveSupport::Dependencies.autoload_paths << new_path
52
51
  ::Rails.application.config.eager_load_paths << new_path
53
52
  end
53
+ end
54
54
 
55
- def cleanup_autoload_paths
56
- :no_op
57
- end
55
+ def cleanup_autoload_paths
56
+ return unless autoload_via_zeitwerk
57
+
58
+ return unless autoload_static_config_path
59
+ ActiveSupport::Dependencies.autoload_paths.delete(::Rails.root.join(autoload_static_config_path).to_s)
60
+ end
61
+
62
+ def autoload_via_zeitwerk
63
+ return @autoload_via_zeitwerk if instance_variable_defined?(:@autoload_via_zeitwerk)
64
+
65
+ @autoload_via_zeitwerk = defined?(::Zeitwerk)
58
66
  end
59
67
 
60
68
  def current_environment
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Anyway # :nodoc:
4
- VERSION = "2.4.0"
4
+ VERSION = "2.4.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anyway_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0
4
+ version: 2.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Dementyev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-05 00:00:00.000000000 Z
11
+ date: 2023-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-next-core