tty-config 0.4.0 → 0.5.0

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: 0f0321ad959e00b63f653d7f27a89964cdc24b103aab2052615e0ed3fdf284ce
4
- data.tar.gz: 12df44d97821e84cb19620fadec565841d0de28abc1a4ba1cd8a05d9d1d6ec24
3
+ metadata.gz: '082839e3aab5e29ce83325a7c96c2c6ce636fde381f4dc4c39cf66309bdb10b7'
4
+ data.tar.gz: c0be15cc9d7e0f211fefe9a9e6317f3076b26bfe2bff4a13eb4c0b2ce68a6174
5
5
  SHA512:
6
- metadata.gz: ebe68702be360238664adf912b42342df8e3e3be266d7e52d2be515d8f48373db53850688517f7c569f2b9674a88371a3205bad707d48a209c01d147af27c07f
7
- data.tar.gz: af1c4f4586a6be640f65ebe01e0dcb88340860b4ae1645ba611135e618ca1b772c75eab2ab9744e0409dddac316154f2358d403e0950af20c25c1755ee7f16cc
6
+ metadata.gz: fb63f5a33972507ebdc97c4986117b93085314b2a66bd0ed455c2c7bda03ff520f69eee72b72350a868e9370ab287980e34ef60a008c0dbe4917cd6cce8a26bd
7
+ data.tar.gz: 88b31c1a9cc6023c9057b9ea8c7970e791d3de9c64118c8ddc0b753a48c643f5e62bd364c2089feccc40ca1977d821a5c0ab7ca6425ea7e5d69dcf04d03d43cd
data/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Change log
2
2
 
3
+ ## [v0.5.0] - 2021-05-23
4
+
5
+ ### Added
6
+ * Add :create option to the #write method to control creating any missing directories
7
+ * Add :path option to the #write method to specify a custom file path to write to
8
+ * Add ability to specify a default value for a missing key in the #delete method
9
+ * Add #env_separator for setting a string to separate parts in environment variable name
10
+
11
+ ### Changed
12
+ * Change #delete to allow removing any subkey of a deeply nested key
13
+ * Change #remove to require from keyword
14
+ * Change #set_if_empty to use nested key fetching to check for value presence
15
+
16
+ ### Fixed
17
+ * Fix Marshaller interface to copy extension names to a subclass
18
+
3
19
  ## [v0.4.0] - 2020-01-25
4
20
 
5
21
  ### Added
@@ -55,9 +71,10 @@
55
71
 
56
72
  * Initial implementation and release
57
73
 
74
+ [v0.5.0]: https://github.com/piotrmurach/tty-config/compare/v0.4.0...v0.5.0
58
75
  [v0.4.0]: https://github.com/piotrmurach/tty-config/compare/v0.3.2...v0.4.0
59
76
  [v0.3.2]: https://github.com/piotrmurach/tty-config/compare/v0.3.1...v0.3.2
60
77
  [v0.3.1]: https://github.com/piotrmurach/tty-config/compare/v0.3.0...v0.3.1
61
78
  [v0.3.0]: https://github.com/piotrmurach/tty-config/compare/v0.2.0...v0.3.0
62
79
  [v0.2.0]: https://github.com/piotrmurach/tty-config/compare/v0.1.0...v0.2.0
63
- [v0.1.0]: https://github.com/piotrmurach/tty-config/compare/v0.1.0
80
+ [v0.1.0]: https://github.com/piotrmurach/tty-config/compare/19cd277...v0.1.0
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2018 Piotr Murach
3
+ Copyright (c) 2018 Piotr Murach (piotrmurach.com)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,46 +1,46 @@
1
1
  <div align="center">
2
- <a href="https://piotrmurach.github.io/tty" target="_blank"><img width="130" src="https://github.com/piotrmurach/tty/raw/master/images/tty.png" alt="tty logo" /></a>
2
+ <a href="https://ttytoolkit.org"><img width="130" src="https://github.com/piotrmurach/tty/raw/master/images/tty.png" alt="TTY Toolkit logo" /></a>
3
3
  </div>
4
4
 
5
5
  # TTY::Config [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter]
6
6
 
7
7
  [![Gem Version](https://badge.fury.io/rb/tty-config.svg)][gem]
8
- [![Build Status](https://secure.travis-ci.org/piotrmurach/tty-config.svg?branch=master)][travis]
8
+ [![Actions CI](https://github.com/piotrmurach/tty-config/workflows/CI/badge.svg?branch=master)][gh_actions_ci]
9
9
  [![Build status](https://ci.appveyor.com/api/projects/status/2383i0dn3hlw9cnn?svg=true)][appveyor]
10
10
  [![Maintainability](https://api.codeclimate.com/v1/badges/dfac05073e1549e9dbb6/maintainability)][codeclimate]
11
11
  [![Coverage Status](https://coveralls.io/repos/github/piotrmurach/tty-config/badge.svg)][coverage]
12
- [![Inline docs](http://inch-ci.org/github/piotrmurach/tty-config.svg?branch=master)][inchpages]
12
+ [![Inline docs](https://inch-ci.org/github/piotrmurach/tty-config.svg?branch=master)][inchpages]
13
13
 
14
14
  [gitter]: https://gitter.im/piotrmurach/tty
15
- [gem]: http://badge.fury.io/rb/tty-config
16
- [travis]: http://travis-ci.org/piotrmurach/tty-config
15
+ [gem]: https://badge.fury.io/rb/tty-config
16
+ [gh_actions_ci]: https://github.com/piotrmurach/tty-config/actions?query=workflow%3ACI
17
17
  [appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-config
18
18
  [codeclimate]: https://codeclimate.com/github/piotrmurach/tty-config/maintainability
19
19
  [coverage]: https://coveralls.io/github/piotrmurach/tty-config
20
- [inchpages]: http://inch-ci.org/github/piotrmurach/tty-config
20
+ [inchpages]: https://inch-ci.org/github/piotrmurach/tty-config
21
21
 
22
- > Define, read and write any Ruby app configurations with a penchant for terminal clients.
22
+ > A highly customisable application configuration interface for building terminal tools.
23
23
 
24
- **TTY::Config** provides app configuration component for [TTY](https://github.com/piotrmurach/tty) toolkit.
24
+ **TTY::Config** provides application configuration component for [TTY](https://github.com/piotrmurach/tty) toolkit.
25
25
 
26
26
  ## Features
27
27
 
28
28
  This is a one-stop shop for all your configuration needs:
29
29
 
30
- * Read and write config files in YAML, JSON, TOML, INI, HCL and Java Properties formats
31
- * Add custom marshallers or override the built-in ones
32
- * Set and read settings for deeply nested keys
33
- * Set defaults for undefined settings
34
- * Read settings with indifferent access
35
- * Merge configuration settings from other hash objects
36
- * Read values from environment variables
30
+ * [Read](#216-read) and [write](#217-write) config files in YAML, JSON, TOML, INI, HCL and Java Properties formats
31
+ * Add [custom marshallers](#222-register_marshaller) or override the built-in ones
32
+ * [Set](#21-set) and [read](#24-fetch) settings for deeply nested keys
33
+ * [Set](#21-set) defaults for undefined settings
34
+ * [Read](#24-fetch) settings with indifferent access
35
+ * [Merge](#25-merge) configuration settings from other hash objects
36
+ * Read values from [environment variables](#23-set_from_env)
37
37
 
38
38
  ## Installation
39
39
 
40
40
  Add this line to your application's Gemfile:
41
41
 
42
42
  ```ruby
43
- gem 'tty-config'
43
+ gem "tty-config"
44
44
  ```
45
45
 
46
46
  And then execute:
@@ -67,17 +67,18 @@ Or install it yourself as:
67
67
  * [2.9 delete](#29-delete)
68
68
  * [2.10 alias_setting](#210-alias_setting)
69
69
  * [2.11 validate](#211-validate)
70
- * [2.12 env_prefix=](#212-env_prefix)
71
- * [2.13 filename=](#213-filename)
72
- * [2.14 extname=](#214-extname)
73
- * [2.15 append_path](#215-append_path)
74
- * [2.16 prepend_path](#216-prepend_path)
75
- * [2.17 read](#217-read)
76
- * [2.18 write](#218-write)
77
- * [2.19 exist?](#219-exist)
78
- * [2.20 autoload_env](#220-autoload_env)
79
- * [2.21 register_marshaller](#221-register_marshaller)
80
- * [2.22 unregister_marshaller](#222-register_marshaller)
70
+ * [2.12 filename=](#212-filename)
71
+ * [2.13 extname=](#213-extname)
72
+ * [2.14 append_path](#214-append_path)
73
+ * [2.15 prepend_path](#215-prepend_path)
74
+ * [2.16 read](#216-read)
75
+ * [2.17 write](#217-write)
76
+ * [2.18 exist?](#218-exist)
77
+ * [2.19 env_prefix=](#219-env_prefix)
78
+ * [2.20 env_separator=](#220-env_separator)
79
+ * [2.21 autoload_env](#221-autoload_env)
80
+ * [2.22 register_marshaller](#222-register_marshaller)
81
+ * [2.23 unregister_marshaller](#223-unregister_marshaller)
81
82
  * [3. Examples](#3-examples)
82
83
  * [3.1 Working with env vars](#31-working-with-env-vars)
83
84
  * [3.2 Working with optparse](#32-working-with-optparse)
@@ -88,27 +89,27 @@ Initialize the configuration and provide the name:
88
89
 
89
90
  ```ruby
90
91
  config = TTY::Config.new
91
- config.filename = 'investments'
92
+ config.filename = "investments"
92
93
  ```
93
94
 
94
95
  Then configure values for different nested keys with `set` and `append`:
95
96
 
96
97
  ```ruby
97
- config.set(:settings, :base, value: 'USD')
98
+ config.set(:settings, :base, value: "USD")
98
99
  config.set(:settings, :color, value: true)
99
- config.set(:coins, value: ['BTC'])
100
+ config.set(:coins, value: ["BTC"])
100
101
 
101
- config.append('ETH', 'TRX', 'DASH', to: :coins)
102
+ config.append("ETH", "TRX", "DASH", to: :coins)
102
103
  ```
103
104
 
104
105
  You can get any value by using `fetch`:
105
106
 
106
107
  ```ruby
107
108
  config.fetch(:settings, :base)
108
- # => 'USD'
109
+ # => "USD"
109
110
 
110
111
  config.fetch(:coins)
111
- # => ['BTC', 'ETH', 'TRX', 'DASH']
112
+ # => ["BTC", "ETH", "TRX", "DASH"]
112
113
  ```
113
114
 
114
115
  And call `write` to persist the configuration to `investments.yml` file:
@@ -150,8 +151,8 @@ class App
150
151
 
151
152
  def initialize
152
153
  @config = TTY::Config.new
153
- @config.filename = 'investments'
154
- @config.extname = '.toml'
154
+ @config.filename = "investments"
155
+ @config.extname = ".toml"
155
156
  @config.append_path Dir.pwd
156
157
  @config.append_path Dir.home
157
158
  end
@@ -169,22 +170,22 @@ end
169
170
  To set configuration setting use `set` method. It accepts any number of keys and value by either using `:value` keyword argument or passing a block:
170
171
 
171
172
  ```ruby
172
- config.set(:base, value: 'USD')
173
- config.set(:base) { 'USD' }
173
+ config.set(:base, value: "USD")
174
+ config.set(:base) { "USD" }
174
175
  ```
175
176
 
176
- The block version of specifying a value will mean that the value is evaluated every time its being read.
177
+ The block version of specifying a value will mean that the value is evaluated every time it's being read.
177
178
 
178
179
  You can also specify deeply nested configuration settings by passing sequence of keys:
179
180
 
180
181
  ```ruby
181
- config.set :settings, :base, value: 'USD'
182
+ config.set(:settings, :base, value: "USD")
182
183
  ```
183
184
 
184
185
  Which is equivalent to:
185
186
 
186
187
  ```ruby
187
- config.set 'settings.base', value: 'USD'
188
+ config.set("settings.base", value: "USD")
188
189
  ```
189
190
 
190
191
  Internally all configuration settings are stored as string keys for ease of working with configuration files and command line application's inputs.
@@ -194,13 +195,13 @@ Internally all configuration settings are stored as string keys for ease of work
194
195
  To set a configuration setting only if it hasn't been set before use `set_if_empty`:
195
196
 
196
197
  ```ruby
197
- config.set_if_empty :base, value: 'USD'
198
+ config.set_if_empty(:base, value: "USD")
198
199
  ```
199
200
 
200
201
  Similar to `set` it allows you to specify arbitrary sequence of keys followed by a key value or block:
201
202
 
202
203
  ```ruby
203
- config.set_if_empty :settings, :base, value: 'USD'
204
+ config.set_if_empty(:settings, :base, value: "USD")
204
205
  ```
205
206
 
206
207
  ### 2.3 set_from_env
@@ -210,8 +211,8 @@ To read configuration options from environment variables use `set_from_env`. At
210
211
  Given the following environment variables:
211
212
 
212
213
  ```ruby
213
- ENV['HOST'] = '192.168.1.17'
214
- ENV['PORT'] = '7727'
214
+ ENV["HOST"] = "192.168.1.17"
215
+ ENV["PORT"] = "7727"
215
216
  ```
216
217
 
217
218
  You can make the config aware of the above env variables:
@@ -225,35 +226,35 @@ Then you can retrieve values like any other configuration option:
225
226
 
226
227
  ```ruby
227
228
  config.fetch(:host)
228
- # => '192.168.1.17'
229
+ # => "192.168.1.17"
229
230
  config.fetch(:port)
230
- # => '7727'
231
+ # => "7727"
231
232
  ```
232
233
 
233
234
  If you want the configuration key name to be different from `ENV` variable name use a block:
234
235
 
235
236
  ```ruby
236
- config.set_from_env(:host) { 'HOSTNAME' }
237
+ config.set_from_env(:host) { "HOSTNAME" }
237
238
  config.set_from_env(:host) { :hostname }
238
239
  ```
239
240
 
240
241
  You can also configure settings for deeply nested keys:
241
242
 
242
243
  ```ruby
243
- config.set_from_env(:settings, :base) { 'CURRENCY' }
244
+ config.set_from_env(:settings, :base) { "CURRENCY" }
244
245
  config.set_from_env(:settings, :base) { :currency }
245
- config.set_from_env('settings.base') { 'CURRENCY'}
246
- config.set_from_env('settings.base') { :currency}
246
+ config.set_from_env("settings.base") { "CURRENCY" }
247
+ config.set_from_env("settings.base") { :currency }
247
248
  ```
248
249
 
249
- And assuming `ENV['CURRENCY']=USD`:
250
+ And assuming `ENV["CURRENCY"]=USD`:
250
251
 
251
252
  ```ruby
252
253
  config.fetch(:settings, :base)
253
254
  # => USD
254
255
  ```
255
256
 
256
- You can also prefix your environment variables. See [env_prefix=](#212-env_prefix)
257
+ You can also prefix your environment variables with [env_prefix=](#219-env_prefix) or use a different separator with [env_separator](#220-env_separator).
257
258
 
258
259
  It's important to recognise that `set_from_env` doesn't record the value for the environment variables. They are read each time from the `ENV` when `fetch` is called.
259
260
 
@@ -262,8 +263,8 @@ It's important to recognise that `set_from_env` doesn't record the value for the
262
263
  To get a configuration setting use `fetch`, which can accept default value either with a `:default` keyword or a block that will be lazy evaluated:
263
264
 
264
265
  ```ruby
265
- config.fetch(:base, default: 'USD')
266
- config.fetch(:base) { 'USD' }
266
+ config.fetch(:base, default: "USD")
267
+ config.fetch(:base) { "USD" }
267
268
  ```
268
269
 
269
270
  Similar to `set` operation, `fetch` allows you to retrieve deeply nested values:
@@ -275,16 +276,16 @@ config.fetch(:settings, :base) # => USD
275
276
  Which is equivalent to:
276
277
 
277
278
  ```ruby
278
- config.fetch('settings.base')
279
+ config.fetch("settings.base")
279
280
  ```
280
281
 
281
282
  `fetch` has indifferent access so you can mix string and symbol keys, all the following examples retrieve the value:
282
283
 
283
284
  ```ruby
284
285
  config.fetch(:settings, :base)
285
- config.fetch('settings', 'base')
286
- config.fetch(:settings', 'base')
287
- config.fetch('settings', :base)
286
+ config.fetch("settings", "base")
287
+ config.fetch(:settings, "base")
288
+ config.fetch("settings", :base)
288
289
  ```
289
290
 
290
291
  ### 2.5 merge
@@ -295,7 +296,7 @@ To merge in other configuration settings as hash use `merge`:
295
296
  config.set(:a, :b, value: 1)
296
297
  config.set(:a, :c, value: 2)
297
298
 
298
- config.merge({'a' => {'c' => 3, 'd' => 4}})
299
+ config.merge({"a" => {"c" => 3, "d" => 4}})
299
300
 
300
301
  config.fetch(:a, :c) # => 3
301
302
  config.fetch(:a, :d) # => 4
@@ -379,6 +380,14 @@ config.delete(:settings, :base)
379
380
  # "USD"
380
381
  ```
381
382
 
383
+ You can provide an optional default value in a block that will be returned when a key is not set:
384
+
385
+ ```ruby
386
+ config.delete(:settings, :unknown) { |key| "#{key} isn't set" }
387
+ # =>
388
+ # "unknown isn't set"
389
+ ```
390
+
382
391
  ### 2.10 alias_setting
383
392
 
384
393
  In order to alias a configuration setting to another name use `alias_setting`.
@@ -386,7 +395,7 @@ In order to alias a configuration setting to another name use `alias_setting`.
386
395
  For example, given an already existing setting:
387
396
 
388
397
  ```ruby
389
- config.set(:base, value: 'baz')
398
+ config.set(:base, value: "baz")
390
399
  ```
391
400
 
392
401
  You can alias it to another name:
@@ -399,29 +408,29 @@ And then access like any other configuration setting:
399
408
 
400
409
  ```ruby
401
410
  config.fetch(:currency)
402
- # => 'USD'
411
+ # => "USD"
403
412
  ```
404
413
 
405
414
  Deep nested configuration options are also supported:
406
415
 
407
416
  ```ruby
408
- config.set(:settings, :base, value: 'USD')
417
+ config.set(:settings, :base, value: "USD")
409
418
  ```
410
419
 
411
420
  And then can be aliased like so:
412
421
 
413
422
  ```ruby
414
423
  config.alias_setting(:settings, :base, to: [:settings, :currency])
415
- config.alias_setting('settings.base', to [:settings, :currency])
424
+ config.alias_setting("settings.base", to [:settings, :currency])
416
425
  ```
417
426
 
418
427
  You can then access the deep nested settings:
419
428
 
420
429
  ```ruby
421
430
  config.fetch(:settings, :currency)
422
- # => 'USD'
423
- config.fetch('settings.currency')
424
- # => 'USD'
431
+ # => "USD"
432
+ config.fetch("settings.currency")
433
+ # => "USD"
425
434
  ```
426
435
 
427
436
  ### 2.11 validate
@@ -441,68 +450,37 @@ You can assign multiple validations for a given key and each of them will be run
441
450
  When setting value all the validations will be run:
442
451
 
443
452
  ```ruby
444
- config.set(:settings, :base, value: 'PL')
445
- # raises TTY::Config::ValidationError, 'Currency code needs to be 3 chars long.'
453
+ config.set(:settings, :base, value: "PL")
454
+ # raises TTY::Config::ValidationError, "Currency code needs to be 3 chars long."
446
455
  ```
447
456
 
448
- If the value s provided as a proc or a block then the validation will be delayed until the value is actually read:
457
+ If the value is provided as a proc or a block then the validation will be delayed until the value is actually read:
449
458
 
450
459
  ```ruby
451
- config.set(:settings, :base) { 'PL' }
460
+ config.set(:settings, :base) { "PL" }
452
461
  config.fetch(:settings, :base)
453
- # raises TTY::Config::ValidationError, 'Currency code needs to be 3 chars long.'
462
+ # raises TTY::Config::ValidationError, "Currency code needs to be 3 chars long."
454
463
  ```
455
464
 
456
- ### 2.12 env_prefix=
457
-
458
- Given the following variables:
459
-
460
- ```ruby
461
- ENV['MYTOOL_HOST'] = '192.168.1.17'
462
- ENV['MYTOOL_PORT'] = ' 7727'
463
- ```
464
-
465
- You can inform configuration about common prefix using `env_prefix`:
466
-
467
- ```ruby
468
- config.env_prefix = 'mytool'
469
- ```
470
-
471
- Then set configuration key name to environment variable name:
472
-
473
- ```ruby
474
- config.set_from_env(:host)
475
- config.set_from_env(:port)
476
- ```
477
-
478
- And finally retrieve the value:
479
-
480
- ```ruby
481
- config.fetch(:host)
482
- #=> '192.168.1.17'
483
- config.fetch(:port)
484
- # => '7727'
485
- ```
486
-
487
- ### 2.13 filename=
465
+ ### 2.12 filename=
488
466
 
489
467
  By default, **TTY::Config** searches for `config` named configuration file. To change this use `filename=` method without the extension name:
490
468
 
491
469
  ```ruby
492
- config.filename = 'investments'
470
+ config.filename = "investments"
493
471
  ```
494
472
 
495
- Then any supported extensions will be search for such as `.yml`, `.json` and `.toml`.
473
+ Then any supported extensions will be searched for such as `.yml`, `.json` and `.toml`.
496
474
 
497
- ### 2.14 extname=
475
+ ### 2.13 extname=
498
476
 
499
- By default '.yml' extension is used to write configuration out to a file but you can change that with `extname=`:
477
+ By default ".yml" extension is used to write configuration out to a file but you can change that with `extname=`:
500
478
 
501
479
  ```ruby
502
- config.extname = '.toml'
480
+ config.extname = ".toml"
503
481
  ```
504
482
 
505
- ### 2.15 append_path
483
+ ### 2.14 append_path
506
484
 
507
485
  You need to tell the **TTY::Config** where to search for configuration files. To search multiple paths for a configuration file use `append_path` or `prepend_path` methods.
508
486
 
@@ -514,9 +492,9 @@ config.append_path(Dir.home) # look in user's home directory
514
492
  config.append_path(Dir.pwd) # look in current working directory
515
493
  ```
516
494
 
517
- None of these paths are required, but you should provide at least one path if you wish to read configuration file.
495
+ None of these paths are required, but you should provide at least one path if you wish to read a configuration file.
518
496
 
519
- ### 2.16 prepend_path
497
+ ### 2.15 prepend_path
520
498
 
521
499
  The `prepend_path` allows you to add configuration search paths that should be searched first.
522
500
 
@@ -525,7 +503,7 @@ config.append_path(Dir.pwd) # look in current working directory second
525
503
  config.prepend_path(Dir.home) # look in user's home directory first
526
504
  ```
527
505
 
528
- ### 2.17 read
506
+ ### 2.16 read
529
507
 
530
508
  There are two ways for reading configuration files and both use the `read` method. One attempts to guess extension and format of your data, the other allows you to request specific extension and format.
531
509
 
@@ -544,7 +522,7 @@ For example, to find file called investments in the current directory do:
544
522
 
545
523
  ```ruby
546
524
  config.append_path(Dir.pwd) # look in current working directory
547
- config.filename = 'investments' # file to search for
525
+ config.filename = "investments" # file to search for
548
526
  ```
549
527
 
550
528
  Find and read the configuration file:
@@ -556,7 +534,7 @@ config.read
556
534
  You can also specify directly the file to read without setting up any search paths or filenames. If you specify a configuration with a known file extension, an appropriate format will be guessed, in this instance `TOML`:
557
535
 
558
536
  ```ruby
559
- config.read('./investments.toml')
537
+ config.read("./investments.toml")
560
538
  ```
561
539
 
562
540
  In cases where you wish to specify a custom file extension, you will need to also specify the file format to use.
@@ -564,40 +542,70 @@ In cases where you wish to specify a custom file extension, you will need to als
564
542
  For example, if you have a configuration file formatted using `YAML` notation with extension called `.config`, to read it do:
565
543
 
566
544
  ```ruby
567
- config.read('investments.config', format: :yaml)
545
+ config.read("investments.config", format: :yaml)
568
546
  ```
569
547
 
570
- ### 2.18 write
548
+ ### 2.17 write
571
549
 
572
- By default **TTY::Config**, persists configuration file in the current working directory with a `config.yml` name. However, you can change that by specifying the filename and extension type:
550
+ By default **TTY::Config**, persists configuration file in the current working directory with a `config.yml` name. However, you can change the default file name by specifying the `filename` and `extension` type:
573
551
 
574
552
  ```ruby
575
- config.filename = 'investments'
576
- config.extname = '.toml'
553
+ config.filename = "investments"
554
+ config.extname = ".toml"
577
555
  ```
578
556
 
579
- To write current configuration to a file, you can either specified direct location path and filename:
557
+ Now, by invoking `write` you will persist the current configuration to `investments.toml` file.
580
558
 
581
559
  ```ruby
582
- config.write('./investments.toml')
560
+ config.write # writes "investments.toml" in the current directory
583
561
  ```
584
562
 
585
- Or, specify location paths to be searched for already existing configuration to overwrite:
563
+ To write the current configuration to a file in a custom location, you can specify a direct location path and filename as an argument:
586
564
 
587
565
  ```ruby
588
- config.append_path(Dir.pwd) # search current working directory
566
+ config.write("/custom/path/to/investments.toml")
567
+ # may raise an error if any of the path directories are missing
568
+ ```
589
569
 
590
- config.write
570
+ Alternatively, if the filename doesn't need to change you can specify only a custom path using the `:path` keyword:
571
+
572
+ ```ruby
573
+ config.write(path: "/custom/path/to")
574
+ # may raise an error if any of the path directories are missing
575
+ ```
576
+
577
+ If the `/custom/path/to` doesn't exist an error will be raised. You can set the `:create` option to make any missing directories in the path:
578
+
579
+ ```ruby
580
+ config.write("/custom/path/to/investments.toml", create: true)
581
+ config.write(path: "/custom/path/to", create: true)
582
+ ```
583
+
584
+ When the `investments.toml` file already exists the `TTY::Config::WriteError` error will be raised.
585
+
586
+ To create a configuration file regardless of whether it exists or not, use `:force` flag:
587
+
588
+ ```ruby
589
+ config.write(force: true)
590
+ config.write("/custom/path/to/investments.toml", force: true)
591
+ config.write(path: "/custom/path/to", force: true)
591
592
  ```
592
593
 
593
- To create configuration file regardless whether it exists or not, use `:force` flag:
594
+ By default, only the current directory is searched. You can specify additional location paths to be searched for already existing configuration to overwrite:
594
595
 
595
596
  ```ruby
596
- config.write(force: true) # overwrite any found config file
597
- config.write('./investments.toml', force: true) # overwrite specific config file
597
+ config.append_path("/custom/path/to") # search in "/custom/path/to" for config file
598
598
  ```
599
599
 
600
- ### 2.19 exist?
600
+ By setting the `:create` option to `true`, you can ensure that even when no path is found that has a configuration file, the first location will be used and all missing directories created.
601
+
602
+ To ensure that a configuration file is written no matter what, use both `:create` and `:force`:
603
+
604
+ ```ruby
605
+ config.write(create: true, force: true)
606
+ ```
607
+
608
+ ### 2.18 exist?
601
609
 
602
610
  To check if a configuration file exists within the configured search paths use `exist?` method:
603
611
 
@@ -605,20 +613,79 @@ To check if a configuration file exists within the configured search paths use `
605
613
  config.exist? # => true
606
614
  ```
607
615
 
608
- ### 2.20 autoload_env
616
+ ### 2.19 env_prefix=
617
+
618
+ Given the following variables:
619
+
620
+ ```ruby
621
+ ENV["MYTOOL_HOST"] = "127.0.0.1"
622
+ ENV["MYTOOL_PORT"] = "7727"
623
+ ```
624
+
625
+ You can inform configuration about common prefix using `env_prefix`:
626
+
627
+ ```ruby
628
+ config.env_prefix = "mytool"
629
+ ```
630
+
631
+ Then set configuration key name to environment variable name:
632
+
633
+ ```ruby
634
+ config.set_from_env(:host)
635
+ config.set_from_env(:port)
636
+ ```
637
+
638
+ And retrieve the value:
639
+
640
+ ```ruby
641
+ config.fetch(:host)
642
+ # => "127.0.0.1"
643
+ config.fetch(:port)
644
+ # => "7727"
645
+ ```
646
+
647
+ ### 2.20 env_separator=
648
+
649
+ By default, the `_` character is used to separate parts in the environment variable name and it can be changed using the `env_separator=` like so:
650
+
651
+ ```ruby
652
+ config.env_separator = "___"
653
+ ```
654
+
655
+ Given the following environment variable:
656
+
657
+ ```ruby
658
+ ENV["SERVER__PORT"] = "123"
659
+ ```
660
+
661
+ Then we can make configuration aware of the above variable name in one of these ways:
662
+
663
+ ```ruby
664
+ config.set_from_env(:server, :port)
665
+ config.set_from_env("server.port")
666
+ ````
667
+
668
+ And retrieve the value:
669
+
670
+ ```ruby
671
+ config.fetch(:server, :port)
672
+ # => "123"
673
+ ```
674
+
675
+ ### 2.21 autoload_env
609
676
 
610
- The `autload_env` allows you to automatically read environment variables. In most cases you would combine it with [env_prefix=](#212-env_prefix) to only read a subset of variables. When using `autload_env`, anytime the `fetch` is called a corresponding environment variable will be checked.
677
+ The `autoload_env` method allows you to automatically read environment variables. In most cases you would combine it with [env_prefix=](#219-env_prefix) to only read a subset of variables. When using `autoload_env`, anytime the `fetch` is called a corresponding environment variable will be checked.
611
678
 
612
679
  For example, given an environment variable `MYTOOL_HOST` set to `localhost`:
613
680
 
614
681
  ```ruby
615
- ENV['MYTOOL_HOST']=localhost
682
+ ENV["MYTOOL_HOST"]=localhost
616
683
  ```
617
684
 
618
685
  And loading environment variables with a prefix of `MYTOOL`:
619
686
 
620
687
  ```ruby
621
- config.env_prefix = 'mytool'
688
+ config.env_prefix = "mytool"
622
689
  config.autoload_env
623
690
  ```
624
691
 
@@ -626,10 +693,10 @@ You can retrieve value with:
626
693
 
627
694
  ```ruby
628
695
  config.fetch(:host)
629
- # => 'localhost'
696
+ # => "localhost"
630
697
  ```
631
698
 
632
- ### 2.21 register_marshaller
699
+ ### 2.22 register_marshaller
633
700
 
634
701
  There are number of built-in marshallers that handle the process of serializing internal configuration from and back into a desired format, for example, a `JSON` string.
635
702
 
@@ -696,9 +763,9 @@ Bear in mind that you can also override the built-in implementation of a marshal
696
763
  config.register_marshaller(:toml, MyTOMLMarshaller)
697
764
  ```
698
765
 
699
- ### 2.22 unregister_marshaller
766
+ ### 2.23 unregister_marshaller
700
767
 
701
- By default, the **TTY::Config** is ready to recognize various extensions. See (2.17 read)[#217-read] section for more details. But, you're free to remove the default marshallers from the internal registry with `unregister_marshaller` method.
768
+ By default, the **TTY::Config** is ready to recognize various extensions. See [2.16 read](#216-read) section for more details. But, you're free to remove the default marshallers from the internal registry with `unregister_marshaller` method.
702
769
 
703
770
  For example, to remove all the built-in marshallers do:
704
771
 
@@ -713,22 +780,22 @@ config.unregister_marshaller :yaml, :json, :toml, :ini, :hcl
713
780
  *TTY::Config* fully supports working with environment variables. For example, there are couple of environment variables that your configuration is interested in, which normally would be set in terminal but for the sake of this example we assign them:
714
781
 
715
782
  ```ruby
716
- ENV['MYTOOL_HOST'] = '192.168.1.17'
717
- ENV['MYTOOL_PORT'] = '7727'
783
+ ENV["MYTOOL_HOST"] = "192.168.1.17"
784
+ ENV["MYTOOL_PORT"] = "7727"
718
785
  ```
719
786
 
720
- Then in order to make your configuration aware of the above, you would use [env_prefix=](#212-env_prefix) and [set_from_env](#23-set_from_env):
787
+ Then in order to make your configuration aware of the above, you would use [env_prefix=](#219-env_prefix) and [set_from_env](#23-set_from_env):
721
788
 
722
789
  ```ruby
723
- config.env_prefix = 'mytool'
790
+ config.env_prefix = "mytool"
724
791
  config.set_from_env(:host)
725
792
  config.set_from_env(:port)
726
793
  ```
727
794
 
728
- Or automatically load all prefixed environment variables with [autoload_env](#220-autoload-env):
795
+ Or automatically load all prefixed environment variables with [autoload_env](#221-autoload_env):
729
796
 
730
797
  ```ruby
731
- config.env_prefix = 'mytool'
798
+ config.env_prefix = "mytool"
732
799
  config.autoload_env
733
800
  ```
734
801
 
@@ -736,9 +803,9 @@ And then retrieve values with [fetch](#24-fetch):
736
803
 
737
804
  ```ruby
738
805
  config.fetch(:host)
739
- #=> '192.168.1.17'
806
+ #=> "192.168.1.17"
740
807
  config.fetch(:port)
741
- # => '7727'
808
+ # => "7727"
742
809
  ```
743
810
 
744
811
  ### 3.2 Working with optparse
@@ -750,7 +817,7 @@ Let's assume you want to create a command line tool that among many options acce
750
817
  First, you need to parse the flags and store results away in options hash:
751
818
 
752
819
  ```ruby
753
- require 'optparse'
820
+ require "optparse"
754
821
 
755
822
  options = {}
756
823
 
@@ -771,7 +838,7 @@ end
771
838
  option_parser.parse!
772
839
  ```
773
840
 
774
- Then, you craete a configuration instance:
841
+ Then, you create a configuration instance:
775
842
 
776
843
  ```ruby
777
844
  config = TTY::Config.new
@@ -780,7 +847,7 @@ config = TTY::Config.new
780
847
  And setup config filename:
781
848
 
782
849
  ```ruby
783
- config_filename = options[:config_file_path] || 'config.yml'
850
+ config_filename = options[:config_file_path] || "config.yml"
784
851
  ```
785
852
 
786
853
  As well as add configuration file locations to search in:
@@ -794,7 +861,7 @@ Once config is initialized, you can read the configuration from a config file:
794
861
 
795
862
  ```ruby
796
863
  begin
797
- config.read(config_filename) # by default the 'config.yml' is read
864
+ config.read(config_filename) # by default the "config.yml" is read
798
865
  rescue TTY::Config::ReadError => read_error
799
866
  STDERR.puts "\nNo configuration file found:"
800
867
  STDERR.puts read_error
@@ -824,7 +891,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
824
891
 
825
892
  ## Contributing
826
893
 
827
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tty-config. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
894
+ Bug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/tty-config. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
828
895
 
829
896
  ## License
830
897
 
@@ -832,7 +899,7 @@ The gem is available as open source under the terms of the [MIT License](https:/
832
899
 
833
900
  ## Code of Conduct
834
901
 
835
- Everyone interacting in the Tty::Config project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/piotrmurach/tty-config/blob/master/CODE_OF_CONDUCT.md).
902
+ Everyone interacting in the TTY::Config project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/piotrmurach/tty-config/blob/master/CODE_OF_CONDUCT.md).
836
903
 
837
904
  ## Copyright
838
905