config 1.5.1 → 1.6.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
  SHA1:
3
- metadata.gz: 0bfea09c1ef9b78b0942e487411324dc3af0772c
4
- data.tar.gz: ac44c082dd7fb235f7bd5345d15d8f581cecb528
3
+ metadata.gz: 208da22ed967b6e2d8122a591eb0b34cb0d7e574
4
+ data.tar.gz: 155481bf4b306be34a9e36641d73f3a3464af0b1
5
5
  SHA512:
6
- metadata.gz: 034d7088dc7729f0f849fd956620fe4fc25da2995b26f886c5e7bf7c14261aa329a7b74d94881162c93b316a8f840db694ae032cf16897746d1091a52b978b05
7
- data.tar.gz: 259b136866b0ed5f49add1bdd81f26e05b1caac25e9673eeaefdb18d907813649dfe688bfeb3238d9de3c5ac0bfd453cccd398ca363b50d8cd0ab32df5c158cf
6
+ metadata.gz: 89c487205d5b8ed2f29902aad3e0b1f57ffe9503328231d89fe5f8b3487aee97199fdb8b3eb5d00ad118025e3e33201cf5dc264344f8d249afe979a3b1eab272
7
+ data.tar.gz: a0a7faace3de2e72c84e51f940aebfa6a22fadaa7d257c081f5097993bcf98e2e6ccbaa6c049aee495b82579887b1775ae9658daf2215286f5357d7cd0eb3966
@@ -2,9 +2,14 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ...
6
+
7
+ ## 1.6.0
8
+
5
9
  ### New features
6
10
 
7
- ...
11
+ * `Config#fail_on_missing` option (default `false`) to raise a `KeyError` exception when accessing a non-existing key
12
+ * Add ability to test if a value was set for a given key with `key?` and `has_key?`
8
13
 
9
14
  ## 1.5.1
10
15
 
data/README.md CHANGED
@@ -299,6 +299,49 @@ between the schema and your config.
299
299
 
300
300
  Check [dry-validation](https://github.com/dry-rb/dry-validation) for more details.
301
301
 
302
+ ### Missing keys
303
+
304
+ For an example settings file:
305
+
306
+ ```yaml
307
+ size: 1
308
+ server: google.com
309
+ ```
310
+
311
+ You can test if a value was set for a given key using `key?` and its alias `has_key?`:
312
+
313
+ ```ruby
314
+ Settings.key?(:path)
315
+ # => false
316
+ Settings.key?(:server)
317
+ # => true
318
+ ```
319
+
320
+ By default, accessing to a missing key returns `nil`:
321
+
322
+ ```ruby
323
+ Settings.key?(:path)
324
+ # => false
325
+ Settings.path
326
+ # => nil
327
+ ```
328
+
329
+ This is not "typo-safe". To solve this problem, you can configure the `fail_on_missing` option:
330
+
331
+ ```ruby
332
+ Config.setup do |config|
333
+ config.fail_on_missing = true
334
+ # ...
335
+ end
336
+ ```
337
+
338
+ So it will raise a `KeyError` when accessing a non-existing key (similar to `Hash#fetch` behaviour):
339
+
340
+ ```ruby
341
+ Settings.path
342
+ # => raises KeyError: key not found: :path
343
+ ```
344
+
302
345
  ### Environment variables
303
346
 
304
347
  See section below for more details.
@@ -15,13 +15,15 @@ module Config
15
15
  # Ensures the setup only gets run once
16
16
  @@_ran_once = false
17
17
 
18
- mattr_accessor :const_name, :use_env, :env_prefix, :env_separator, :env_converter, :env_parse_values
18
+ mattr_accessor :const_name, :use_env, :env_prefix, :env_separator,
19
+ :env_converter, :env_parse_values, :fail_on_missing
19
20
  @@const_name = 'Settings'
20
21
  @@use_env = false
21
22
  @@env_prefix = @@const_name
22
23
  @@env_separator = '.'
23
24
  @@env_converter = :downcase
24
25
  @@env_parse_values = true
26
+ @@fail_on_missing = false
25
27
 
26
28
  # deep_merge options
27
29
  mattr_accessor :knockout_prefix, :overwrite_arrays
@@ -153,6 +153,19 @@ module Config
153
153
  end
154
154
  end
155
155
 
156
+ delegate :key?, :has_key?, to: :table
157
+
158
+ def method_missing(method_name, *args)
159
+ if Config.fail_on_missing && method_name !~ /.*(?==\z)/m
160
+ raise KeyError, "key not found: #{method_name.inspect}" unless key?(method_name)
161
+ end
162
+ super
163
+ end
164
+
165
+ def respond_to_missing?(*args)
166
+ super
167
+ end
168
+
156
169
  protected
157
170
 
158
171
  def descend_array(array)
@@ -1,3 +1,3 @@
1
1
  module Config
2
- VERSION = '1.5.1'
2
+ VERSION = '1.6.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Kuczynski
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-10-23 00:00:00.000000000 Z
13
+ date: 2017-11-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport