config_reader 1.0.3 → 1.0.5

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
  SHA1:
3
- metadata.gz: 02627114450472254b226d2ffc7092a276ab4179
4
- data.tar.gz: 053954bea858dcd8b10895371f791130b72dbc07
3
+ metadata.gz: 3fa20c036bed02f5864bb5fdaebbd2f0ecce3315
4
+ data.tar.gz: cea79cb4e8a5b14456f64d3988fe2f9274f36099
5
5
  SHA512:
6
- metadata.gz: 6baee2f52c7b2c2eede17cffc14c4981aa886e555195e48a77dd42ba2ce582f00f93ae789ea76cbf259e792f8576881a4b4d73317d3866392c095ebd98dbb79a
7
- data.tar.gz: 2c5bbc7636e67d247d57883a4f8e377d9d7087d00763bf5457dc949db1b2d0bf6dfca1cbdce38d2751f0f47caa4e2aa15db604fe8e614ce2a625a8e03aa6e03e
6
+ metadata.gz: 69f491dc61461f6d7415b65f3cf1d3ec22b64f950bd9ed996ffcbcb173c2de78e61afa87c6bdd1af1021f39bec8c0b792cd27f483c85c936ab00fd7c57055e10
7
+ data.tar.gz: 9bd67ba76fc575145621f4f0f274384e3fa7fef06f913876b11b3176400a9d043ace8559f55aae5b30222042ac86f30708dcf25be334955184b744963ed0f183
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- config_reader (1.0.3)
4
+ config_reader (1.0.5)
5
5
  deep_merge
6
6
 
7
7
  GEM
@@ -47,4 +47,4 @@ DEPENDENCIES
47
47
  sekrets (~> 1.7.0)
48
48
 
49
49
  BUNDLED WITH
50
- 1.15.1
50
+ 1.16.1
@@ -1,3 +1,9 @@
1
+ == 1.0.5 2018-02-01
2
+ * pass ignore_missing_keys to nested hashes during conversion
3
+
4
+ == 1.0.4 2018-02-01
5
+ * add a config option for Key Error behavior
6
+
1
7
  == 1.0.3 2017-06-20
2
8
  * add the deep_merge gem so all keys don't need to be duplicated across env sections
3
9
 
@@ -27,6 +27,13 @@ Example class:
27
27
  self.config_file = 'config/my_config.yml'
28
28
  end
29
29
 
30
+ don't throw errors on missing keys
31
+
32
+ class MyConfig < ConfigReader
33
+ self.config_file = 'config/my_config.yml'
34
+ self.ignore_missing_keys = true
35
+ end
36
+
30
37
  == Sekrets
31
38
  Now also includes Sekrets integration. https://github.com/ahoward/sekrets
32
39
 
@@ -11,7 +11,7 @@ end
11
11
 
12
12
  class ConfigReader
13
13
  class << self
14
- attr_accessor :config_file, :config, :sekrets_file
14
+ attr_accessor :config_file, :config, :sekrets_file, :ignore_missing_keys
15
15
 
16
16
  def config
17
17
  @config = nil unless defined?(@config)
@@ -22,6 +22,10 @@ class ConfigReader
22
22
  @config_file = file
23
23
  end
24
24
 
25
+ def ignore_missing_keys=(bool)
26
+ @ignore_missing_keys = bool
27
+ end
28
+
25
29
  def reload
26
30
  merge_configs(find_env, load_config, load_sekrets)
27
31
  end
@@ -106,7 +110,7 @@ class ConfigReader
106
110
  _conf.deep_merge!(sekrets['defaults']) if sekrets && sekrets['defaults']
107
111
  _conf.deep_merge!(conf[env]) if conf[env]
108
112
  _conf.deep_merge!(sekrets[env]) if sekrets && sekrets[env]
109
- ConfigReader::MagicHash.convert_hash(_conf)
113
+ ConfigReader::MagicHash.convert_hash(_conf, @ignore_missing_keys)
110
114
  end
111
115
  end
112
116
  end
@@ -1,12 +1,14 @@
1
1
  class ConfigReader
2
2
  class MagicHash < Hash
3
+ attr_accessor :ignore_missing_keys
3
4
 
4
- def self.convert_hash(hash)
5
+ def self.convert_hash(hash, ignore_missing_keys=false)
5
6
  magic_hash = new
7
+ magic_hash.ignore_missing_keys = ignore_missing_keys
6
8
 
7
9
  hash.each_pair do |key, value|
8
10
  if value.is_a?(Hash)
9
- magic_hash[key.to_sym] = convert_hash(value)
11
+ magic_hash[key.to_sym] = convert_hash(value, ignore_missing_keys)
10
12
  else
11
13
  magic_hash[key.to_sym] = value
12
14
  end
@@ -16,13 +18,21 @@ class ConfigReader
16
18
  end
17
19
 
18
20
  def [](key)
19
- fetch(key.to_sym)
21
+ begin
22
+ fetch(key.to_sym)
23
+ rescue KeyError => e
24
+ raise e unless ignore_missing_keys
25
+ end
20
26
  end
21
27
 
22
28
  def method_missing(key, *args, &block)
23
- has_key?(key) ?
24
- fetch(key) :
25
- super
29
+ begin
30
+ has_key?(key) ?
31
+ fetch(key) :
32
+ super
33
+ rescue KeyError, NoMethodError => e
34
+ raise e unless ignore_missing_keys
35
+ end
26
36
  end
27
37
 
28
38
  end
@@ -1,3 +1,3 @@
1
1
  class ConfigReader
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.5"
3
3
  end
@@ -16,6 +16,58 @@ describe "ConfigReader" do
16
16
  end
17
17
  end
18
18
 
19
+ describe "default KeyNotFound behavior" do
20
+ it "should raise on missing key with [] accessor" do
21
+ expect {
22
+ TestConfig[:no_key]
23
+ }.to raise_error(KeyError)
24
+ end
25
+
26
+ it "should raise on missing key with #key accessor" do
27
+ expect {
28
+ TestConfig.no_key
29
+ }.to raise_error(KeyError)
30
+ end
31
+
32
+ it "should raise on missing nested key with [] accessor" do
33
+ expect {
34
+ TestConfig[:nested_key][:missing]
35
+ }.to raise_error(KeyError)
36
+ end
37
+
38
+ it "should raise on missing nested key with #key accessor" do
39
+ expect {
40
+ TestConfig.nested_key.missing
41
+ }.to raise_error(NoMethodError)
42
+ end
43
+ end
44
+
45
+ describe "ignoring KeyNotFound" do
46
+ it "should not raise on missing key with [] accessor" do
47
+ expect {
48
+ NoKeyNoErrorConfig[:no_key]
49
+ }.to_not raise_error
50
+ end
51
+
52
+ it "should not raise on missing key with #key accessor" do
53
+ expect {
54
+ NoKeyNoErrorConfig.no_key
55
+ }.to_not raise_error
56
+ end
57
+
58
+ it "should not raise on missing nested key with [] accessor" do
59
+ expect {
60
+ NoKeyNoErrorConfig[:nested_key][:missing]
61
+ }.to_not raise_error
62
+ end
63
+
64
+ it "should not raise on missing nested key with #key accessor" do
65
+ expect {
66
+ NoKeyNoErrorConfig.nested_key.missing
67
+ }.to_not raise_error
68
+ end
69
+ end
70
+
19
71
  describe "parsing a YAML file" do
20
72
  it "should find values with method_missing" do
21
73
  TestConfig.app_name.should == 'test_app'
@@ -56,8 +108,6 @@ describe "ConfigReader" do
56
108
  end
57
109
 
58
110
  context 'using sekrets' do
59
- ENV['SEKRETS_KEY'] = 'shhh'
60
-
61
111
  describe "parsing a YAML file" do
62
112
  it "should find values with method_missing" do
63
113
  SekretsConfig.app_name.should == 'test_app_sekret'
@@ -0,0 +1,4 @@
1
+ class NoKeyNoErrorConfig < ConfigReader
2
+ self.config_file = 'spec/test_config.yml'
3
+ self.ignore_missing_keys = true
4
+ end
Binary file
@@ -1,10 +1,10 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
  require 'spec'
4
- # require 'spec/autorun'
5
4
  require 'config_reader'
6
5
  require 'test_config'
7
6
  require 'sekrets_config'
7
+ require 'no_key_no_error_config'
8
8
 
9
9
  ENV['RACK_ENV'] = 'test'
10
- ENV['SEKRETS_KEY'] = 'shhh'
10
+ ENV['SEKRETS_KEY'] = 'ruby2.4_needs_longer_keys'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_reader
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Moen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-20 00:00:00.000000000 Z
11
+ date: 2018-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: deep_merge
@@ -100,6 +100,7 @@ files:
100
100
  - lib/config_reader/magic_hash.rb
101
101
  - lib/config_reader/version.rb
102
102
  - spec/config_reader_spec.rb
103
+ - spec/no_key_no_error_config.rb
103
104
  - spec/sekrets_config.rb
104
105
  - spec/sekrets_config.yml.enc
105
106
  - spec/spec_helper.rb
@@ -124,12 +125,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
125
  version: '0'
125
126
  requirements: []
126
127
  rubyforge_project: config_reader
127
- rubygems_version: 2.5.1
128
+ rubygems_version: 2.4.5.1
128
129
  signing_key:
129
130
  specification_version: 4
130
131
  summary: Provides a way to manage environment specific configuration settings.
131
132
  test_files:
132
133
  - spec/config_reader_spec.rb
134
+ - spec/no_key_no_error_config.rb
133
135
  - spec/sekrets_config.rb
134
136
  - spec/sekrets_config.yml.enc
135
137
  - spec/spec_helper.rb