config_reader 3.0.1 → 3.0.2

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: cae8d3f402a58472016b70dd56fc8e5f97f099c77809edf8a6145cfe80425139
4
- data.tar.gz: 87b86f1f97bc4586aa20768f7216b235fc42ed463bc568f5775c87e756b32c55
3
+ metadata.gz: ff6af0d4ec72275cf3d8186e9f889a8a0dee9c94e9ca16552d8a7da3e4f057a0
4
+ data.tar.gz: 6be5d22c4fb82c0fb60927a6f63d5abe287f2dbc7bedb87e67fb0bfa39dd7bb7
5
5
  SHA512:
6
- metadata.gz: a64b08e7e67f9313d2c44b179769b84594c3dc5e48f22b0551787d352274724f38ca8db544ba16904fad67d0c6358026c65edda44fef181981031cf952391226
7
- data.tar.gz: 38d8e881518b32c0e997d2f859127deb40bafc5dc3478c34fb6927f46cf63d80dc2fd4240cf9dc4b66fb02f950ebe6329d67625842792bec7f44020878525452
6
+ metadata.gz: 600ea0a78d494acecc50f787fa8ab4d8594592f5ccd70dfbc892b3e4068f31c12dc79c56d7c4801089dc47834af26da65bfbacceef09e8d3ee3962b352720ff6
7
+ data.tar.gz: 2d39e35dbcd26edd3b9a50e364b0d4ec1dd091691ba43989baaf386d28212c04250fdc04946f636b728fb3754546d65c3ac74be2ff74e5d5ea52c24a0867c570
data/CHANGELOG.md CHANGED
@@ -1,10 +1,14 @@
1
1
  # Changelog
2
2
 
3
- ## 3.0.1 2024-12-13
3
+ ## 3.0.2 2024-12-13
4
+
5
+ Fix an issue when merging multiple envs.
6
+
7
+ ## 3.0.1 2024-12-13 (yanked)
4
8
 
5
9
  Don't merge! in deep_merge
6
10
 
7
- ## 3.0.0 2024-12-12
11
+ ## 3.0.0 2024-12-12 (yanked)
8
12
 
9
13
  Update to latest Sekrets (1.14.0)
10
14
  Explicitly require abbrev gem
@@ -24,9 +24,7 @@ class ConfigReader
24
24
  end
25
25
 
26
26
  def method_missing(key, *args, &block)
27
- key?(key) ? fetch(key) : super
28
- rescue KeyError, NoMethodError => e # skipcq: RB-P1001
29
- raise e unless ignore_missing_keys
27
+ ignore_missing_keys ? self[key] : fetch(key)
30
28
  end
31
29
 
32
30
  def respond_to_missing?(m, *)
@@ -1,3 +1,3 @@
1
1
  class ConfigReader
2
- VERSION = "3.0.1"
2
+ VERSION = "3.0.2"
3
3
  end
data/lib/config_reader.rb CHANGED
@@ -2,6 +2,11 @@ require "config_reader/version"
2
2
  require "config_reader/config_hash"
3
3
  require "psych"
4
4
 
5
+ begin
6
+ require "sekrets"
7
+ rescue LoadError
8
+ end
9
+
5
10
  begin
6
11
  require "erb"
7
12
  rescue LoadError
@@ -31,11 +36,11 @@ class ConfigReader
31
36
  end
32
37
 
33
38
  def deep_merge(hash, other_hash)
34
- hash.merge(other_hash) do |_key, this_val, other_val|
39
+ hash.merge(other_hash) do |key, this_val, other_val|
35
40
  if this_val.is_a?(Hash) && other_val.is_a?(Hash)
36
41
  deep_merge(this_val, other_val)
37
42
  else
38
- other_val
43
+ hash[key] = other_val
39
44
  end
40
45
  end
41
46
  end
@@ -58,26 +63,13 @@ class ConfigReader
58
63
  end
59
64
 
60
65
  def inspect
61
- puts config.inspect
66
+ config.inspect
62
67
  end
63
68
 
64
69
  def load_config
65
70
  raise "No config file set" unless find_config
66
71
 
67
- conf =
68
- if defined?(ERB)
69
- Psych.safe_load(
70
- ERB.new(File.read(find_config)).result,
71
- aliases: true,
72
- permitted_classes: configuration.permitted_classes.to_a + [Symbol]
73
- )
74
- else
75
- Psych.safe_load_file(
76
- File.read(find_config),
77
- aliases: true,
78
- permitted_classes: configuration.permitted_classes.to_a + [Symbol]
79
- )
80
- end
72
+ conf = load_yaml
81
73
 
82
74
  raise "No config found" unless conf
83
75
 
@@ -85,43 +77,59 @@ class ConfigReader
85
77
  end
86
78
 
87
79
  def load_sekrets
88
- sekrets = {}
89
-
90
80
  if configuration.sekrets_file
91
- begin
92
- require "sekrets"
93
- sekrets = ::Sekrets.settings_for(configuration.sekrets_file)
94
- raise "No sekrets found" unless sekrets
95
- rescue LoadError
96
- warn "You specified a sekrets_file, but the sekrets gem isn't available."
81
+ if !defined?(::Sekrets)
82
+ raise ArgumentError,
83
+ "You specified a sekrets_file, but the sekrets gem isn't available."
84
+ else
85
+ ::Sekrets.settings_for(configuration.sekrets_file) ||
86
+ raise("No sekrets found")
97
87
  end
98
88
  end
99
-
100
- sekrets
101
89
  end
102
90
 
103
- def merge_configs(conf, sekrets)
104
- @envs = {}
105
- env_keys = conf.keys - ["defaults"]
106
- env = configuration.environment
91
+ def load_yaml
92
+ permitted_classes = configuration.permitted_classes.to_a + [Symbol]
107
93
 
108
- defaults = conf["defaults"]
109
-
110
- if sekrets && sekrets["defaults"]
111
- defaults = deep_merge(defaults, sekrets["defaults"])
94
+ if defined?(ERB)
95
+ Psych.safe_load(
96
+ ERB.new(File.read(find_config)).result,
97
+ aliases: true,
98
+ permitted_classes: permitted_classes
99
+ )
100
+ else
101
+ Psych.safe_load_file(
102
+ File.read(find_config),
103
+ aliases: true,
104
+ permitted_classes: permitted_classes
105
+ )
112
106
  end
107
+ end
113
108
 
114
- env_keys.each do |key|
115
- key_hash = deep_merge(defaults, conf[key]) if conf[key]
116
- key_hash = deep_merge(defaults, sekrets[key]) if sekrets && sekrets[key]
109
+ def merge_all_configs(conf, defaults, sekrets)
110
+ @envs = {}
117
111
 
118
- @envs[key] = ConfigHash.convert_hash(
112
+ (conf.keys - ["defaults"]).each do |env|
113
+ key_hash = deep_merge(defaults.dup, conf[env]) if conf[env]
114
+ key_hash = deep_merge(key_hash, sekrets[env]) if sekrets&.[](env)
115
+
116
+ @envs[env] = ConfigHash.convert_hash(
119
117
  key_hash,
120
118
  configuration.ignore_missing_keys
121
119
  )
122
120
  end
121
+ end
122
+
123
+ def merge_configs(conf, sekrets)
124
+ defaults = conf["defaults"]
125
+
126
+ if sekrets&.[]("defaults")
127
+ defaults = deep_merge(defaults, sekrets["defaults"])
128
+ end
129
+
130
+ merge_all_configs(conf, defaults, sekrets)
123
131
 
124
- @envs[env]
132
+ @envs[configuration.environment]
125
133
  end
126
134
 
127
135
  def method_missing(key, *_args, &_block)
@@ -27,7 +27,7 @@ describe "ConfigReader" do
27
27
  end
28
28
 
29
29
  it "should raise on missing nested key with #key accessor" do
30
- expect { TestConfig.nested_key.missing }.to raise_error(NoMethodError)
30
+ expect { TestConfig.nested_key.missing }.to raise_error(KeyError)
31
31
  end
32
32
  end
33
33
 
@@ -1,2 +1,3 @@
1
1
 
2
- �Y"������,K� m4�9-�T�it}juv�
2
+ �Y"������,K� m4�9-�T�i6 �dT~�7��wkp��Coy6Ōݕ
3
+ ��7
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_reader
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Moen