rails-options_config 1.3.2 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a7689f5625604db17772a75d1fdcdc5e7b316a3adbd79e8d691e575f1ba663f8
4
- data.tar.gz: 47367ce04d3f612b6c627e18d5f481f104ece4d2579ce3bbda150ffef3826b84
3
+ metadata.gz: 3d0be5348ae96aee4ddcd3569581ea8f9a0b0671fa65d3465c95153a3d87ebfb
4
+ data.tar.gz: 94433d9ccd8d477e9cf98defce9a90ca513d3164c386c11a4a6ec662c03cd3c0
5
5
  SHA512:
6
- metadata.gz: 315b51d35d4db43a0957f53fd8a4f991a9f282ebfda4ec0c993855649af7e3e27c91a04603bcf7b1db09d7e306d3cf6687de5efe7f4e74686f27a079b9bf221e
7
- data.tar.gz: a2dd452cdce5b421b8dc3f8df1152cbbf777b039d249c28f1f2ac74df89c302c921a0cab8cf346cdd0ab66ed9c9a7d724ddacbe96bc13521ee3a2469e9c3bcf6
6
+ metadata.gz: 8bda52d73c5635fca923af2e5a4d282e00eff8db8e9ac02622fd831855a4943564569d5fba5c443b63c64decd89593c883749a2f7a0217d51a0df0a4e74c9757
7
+ data.tar.gz: c49b73a140ab6072640a77d6f24c80cb00546969f60aa2275b8d46eeca9bec238f592daf0f42f352e585fcd41df4cb3a09c1a053b3d94537adc3d05c5a8dc1cc
data/README.md CHANGED
@@ -60,6 +60,16 @@ will become:
60
60
  }
61
61
  ```
62
62
 
63
+ If for any reason you need to load the options for a different environment from the current one (for example, Rails creates both the development and test databases running in the `development` environment, so if you want to fill in the `config/database.yml` file using Options, you need to force the test environment when filling the respective section), you just need to pass it to `Rails.application.options`:
64
+
65
+ ```ruby
66
+ # RAILS_ENV=staging rails c
67
+ Rails.application.options # => staging options
68
+ Rails.application.options(:production) # => production options
69
+ ```
70
+
71
+ Notice that only the options for the current environment are cached, the others are loaded on call every time.
72
+
63
73
  ### Options
64
74
 
65
75
  You can set these options in `application.rb` or in any of the `environments/*.rb` files.
@@ -1,82 +1,11 @@
1
- require_relative 'yaml/env_visitor'
2
- require_relative 'key_override_error'
3
- require_relative 'unusable_options_error'
4
-
5
1
  module Rails
6
2
  class Application
7
- def options
8
- @options ||= begin
9
- yaml_visitor = OptionsConfig::EnvVisitor.create symbolize_names: true
10
- Array(config.options.roots)
11
- .flat_map do |root|
12
- Dir
13
- .glob(Array(config.options.paths), base: root)
14
- .map do |path|
15
- path.match %r{^(?<filename>.*?)(?<env>\..*?)?(?<extension>\.ya?ml)(?<enc>\.enc)?$} do |md|
16
- full_path = Pathname(root).join(path)
17
- encrypted = md[:enc].present?
18
- content = if encrypted
19
- YAML.parse encrypted(full_path).read
20
- else
21
- YAML.parse_file full_path
22
- end
23
- .therefore { |yaml| yaml_visitor.accept yaml }
24
-
25
- unless content.is_a? Hash
26
- raise OptionsConfig::UnusableOptionsError,
27
- "The contents of options file `#{full_path}` are unsuitable. It must be a hash."
28
- end
29
-
30
- {
31
- path: full_path,
32
- filename: md[:filename],
33
- content: content,
34
- encrypted: encrypted
35
- }
36
- .tap do |hash|
37
- hash[:environment] = md[:env].delete_prefix('.').to_sym if md[:env].present?
38
- end
39
- end
40
- end
41
- end
42
- .group_by { |file| file[:filename] }
43
- .map do |_, files|
44
- # Specifics overwrite bases, and encrypted overwrite cleartexts for each.
45
- bases = files
46
- .reject { |file| file.key? :environment }
47
- .sort_by { |file| file[:encrypted] ? 1 : 0 }
48
- specifics = files
49
- .filter { |file| file[:environment] == Rails.env.to_sym }
50
- .sort_by { |file| file[:encrypted] ? 1 : 0 }
51
-
52
- (bases + specifics)
53
- .reduce({}) { |acc, override| acc.deep_merge override[:content] }
54
- end
55
- .then do |hashes|
56
- if config.options.raise_on_override
57
- hashes.reduce credentials.config do |acc, hash|
58
- acc.deep_merge hash do |key, value1, value2|
59
- raise OptionsConfig::KeyOverrideError,
60
- 'Key override while loading options: ' \
61
- "trying to set `#{key}' to #{value2.inspect}:#{value2.class}, " \
62
- "but it is already set to #{value1.inspect}:#{value1.class}"
63
- end
64
- end
65
- else
66
- hashes.reduce(credentials.config, &:deep_merge)
67
- end
68
- end
69
- .then do |hash|
70
- deep_transform = proc do |value|
71
- if value.is_a? Hash
72
- ActiveSupport::OrderedOptions[value.transform_values(&deep_transform)]
73
- else
74
- value
75
- end
76
- end
77
- deep_transform.call hash
78
- end
79
- end
3
+ def options env=nil
4
+ if env.nil? || env.to_sym == Rails.env.to_sym
5
+ @options ||= OptionsConfig.parse_options self, Rails.env.to_sym
6
+ else
7
+ OptionsConfig.parse_options self, env.to_sym
8
+ end
80
9
  end
81
10
  end
82
11
  end
@@ -1,5 +1,5 @@
1
1
  module Rails
2
2
  module OptionsConfig
3
- VERSION = '1.3.2'.freeze
3
+ VERSION = '1.4.0'.freeze
4
4
  end
5
5
  end
@@ -1,3 +1,86 @@
1
+ require_relative 'options_config/yaml/env_visitor'
2
+ require_relative 'options_config/key_override_error'
3
+ require_relative 'options_config/unusable_options_error'
4
+
5
+ module Rails
6
+ module OptionsConfig
7
+ def self.parse_options application, env
8
+ application.instance_exec do
9
+ yaml_visitor = EnvVisitor.create symbolize_names: true
10
+ Array(config.options.roots)
11
+ .flat_map do |root|
12
+ Dir
13
+ .glob(Array(config.options.paths), base: root)
14
+ .map do |path|
15
+ path.match %r{^(?<filename>.*?)(?<env>\..*?)?(?<extension>\.ya?ml)(?<enc>\.enc)?$} do |md|
16
+ full_path = Pathname(root).join(path)
17
+ encrypted = md[:enc].present?
18
+ content = if encrypted
19
+ YAML.parse encrypted(full_path).read
20
+ else
21
+ YAML.parse_file full_path
22
+ end
23
+ .therefore { |yaml| yaml_visitor.accept yaml }
24
+
25
+ unless content.is_a? Hash
26
+ raise UnusableOptionsError,
27
+ "The contents of options file `#{full_path}` are unsuitable. It must be a hash."
28
+ end
29
+
30
+ {
31
+ path: full_path,
32
+ filename: md[:filename],
33
+ content: content,
34
+ encrypted: encrypted
35
+ }
36
+ .tap do |hash|
37
+ hash[:environment] = md[:env].delete_prefix('.').to_sym if md[:env].present?
38
+ end
39
+ end
40
+ end
41
+ end
42
+ .group_by { |file| file[:filename] }
43
+ .map do |_, files|
44
+ # Specifics overwrite bases, and encrypted overwrite cleartexts for each.
45
+ bases = files
46
+ .reject { |file| file.key? :environment }
47
+ .sort_by { |file| file[:encrypted] ? 1 : 0 }
48
+ specifics = files
49
+ .filter { |file| file[:environment] == env }
50
+ .sort_by { |file| file[:encrypted] ? 1 : 0 }
51
+
52
+ (bases + specifics)
53
+ .reduce({}) { |acc, override| acc.deep_merge override[:content] }
54
+ end
55
+ .then do |hashes|
56
+ if config.options.raise_on_override
57
+ hashes.reduce credentials.config do |acc, hash|
58
+ acc.deep_merge hash do |key, value1, value2|
59
+ raise KeyOverrideError,
60
+ 'Key override while loading options: ' \
61
+ "trying to set `#{key}' to #{value2.inspect}:#{value2.class}, " \
62
+ "but it is already set to #{value1.inspect}:#{value1.class}"
63
+ end
64
+ end
65
+ else
66
+ hashes.reduce(credentials.config, &:deep_merge)
67
+ end
68
+ end
69
+ .then do |hash|
70
+ deep_transform = proc do |value|
71
+ if value.is_a? Hash
72
+ ActiveSupport::OrderedOptions[value.transform_values(&deep_transform)]
73
+ else
74
+ value
75
+ end
76
+ end
77
+ deep_transform.call hash
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+
1
84
  require 'rails/options_config/version'
2
85
  require 'rails/options_config/engine'
3
86
  require 'rails/options_config/application'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-options_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moku S.r.l.
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-11-27 00:00:00.000000000 Z
12
+ date: 2025-07-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties