super-smart-sys 0.0.1
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 +7 -0
- data/config-5.6.1/CHANGELOG.md +332 -0
- data/config-5.6.1/CONTRIBUTING.md +38 -0
- data/config-5.6.1/LICENSE.md +26 -0
- data/config-5.6.1/README.md +615 -0
- data/config-5.6.1/config.gemspec +62 -0
- data/config-5.6.1/lib/config/configuration.rb +36 -0
- data/config-5.6.1/lib/config/dry_validation_requirements.rb +25 -0
- data/config-5.6.1/lib/config/error.rb +4 -0
- data/config-5.6.1/lib/config/integrations/heroku.rb +59 -0
- data/config-5.6.1/lib/config/integrations/rails/railtie.rb +38 -0
- data/config-5.6.1/lib/config/integrations/sinatra.rb +26 -0
- data/config-5.6.1/lib/config/options.rb +195 -0
- data/config-5.6.1/lib/config/rack/reloader.rb +15 -0
- data/config-5.6.1/lib/config/sources/env_source.rb +94 -0
- data/config-5.6.1/lib/config/sources/hash_source.rb +16 -0
- data/config-5.6.1/lib/config/sources/yaml_source.rb +32 -0
- data/config-5.6.1/lib/config/tasks/heroku.rake +7 -0
- data/config-5.6.1/lib/config/validation/error.rb +15 -0
- data/config-5.6.1/lib/config/validation/schema.rb +23 -0
- data/config-5.6.1/lib/config/validation/validate.rb +29 -0
- data/config-5.6.1/lib/config/version.rb +3 -0
- data/config-5.6.1/lib/config.rb +93 -0
- data/config-5.6.1/lib/generators/config/install_generator.rb +32 -0
- data/config-5.6.1/lib/generators/config/templates/config.rb +77 -0
- data/config-5.6.1/lib/generators/config/templates/settings/development.yml +0 -0
- data/config-5.6.1/lib/generators/config/templates/settings/production.yml +0 -0
- data/config-5.6.1/lib/generators/config/templates/settings/test.yml +0 -0
- data/config-5.6.1/lib/generators/config/templates/settings.local.yml +0 -0
- data/config-5.6.1/lib/generators/config/templates/settings.yml +0 -0
- data/super-smart-sys.gemspec +11 -0
- metadata +70 -0
|
@@ -0,0 +1,615 @@
|
|
|
1
|
+
# Config
|
|
2
|
+
|
|
3
|
+
[](https://rubygems.org/gems/config)
|
|
4
|
+
[](https://rubygems.org/gems/config)
|
|
5
|
+
[](https://github.com/rubyconfig/config/actions?query=branch%3Amaster)
|
|
6
|
+
[](https://opencollective.com/rubyconfig)
|
|
7
|
+
|
|
8
|
+
## Summary
|
|
9
|
+
|
|
10
|
+
Config helps you easily manage environment specific settings in an easy and usable manner.
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
* simple YAML config files
|
|
15
|
+
* config files support ERB
|
|
16
|
+
* config files support inheritance and multiple environments
|
|
17
|
+
* access config information via convenient object member notation
|
|
18
|
+
* support for multi-level settings (`Settings.group.subgroup.setting`)
|
|
19
|
+
* local developer settings ignored when committing the code
|
|
20
|
+
|
|
21
|
+
## Compatibility
|
|
22
|
+
|
|
23
|
+
Current version supports and is [tested](.github/workflows/tests.yml#L19) for the following interpreters and frameworks:
|
|
24
|
+
|
|
25
|
+
* Interpreters
|
|
26
|
+
* [Ruby](https://www.ruby-lang.org) `>= 2.6`
|
|
27
|
+
* [JRuby](https://www.jruby.org) `>= 9.2`
|
|
28
|
+
* [TruffleRuby](https://github.com/oracle/truffleruby) `>= 19.3`
|
|
29
|
+
* Application frameworks
|
|
30
|
+
* Rails `>= 5.2`
|
|
31
|
+
* Padrino
|
|
32
|
+
* Sinatra
|
|
33
|
+
|
|
34
|
+
For Ruby `2.0` to `2.3` or Rails `3` to `4.1` use version `1.x` of this gem. For older versions of Rails or Ruby use [AppConfig](http://github.com/fredwu/app_config).
|
|
35
|
+
|
|
36
|
+
For Ruby `2.4` or `2.5` or Rails `4.2`, `5.0`, or `5.1` use version `3.x` of this gem.
|
|
37
|
+
|
|
38
|
+
## Installing
|
|
39
|
+
|
|
40
|
+
### Installing on Rails
|
|
41
|
+
|
|
42
|
+
Add `gem 'config'` to your `Gemfile` and run `bundle install` to install it. Then run
|
|
43
|
+
|
|
44
|
+
rails g config:install
|
|
45
|
+
|
|
46
|
+
which will generate customizable config file `config/initializers/config.rb` and set of default settings files:
|
|
47
|
+
|
|
48
|
+
config/settings.yml
|
|
49
|
+
config/settings.local.yml
|
|
50
|
+
config/settings/development.yml
|
|
51
|
+
config/settings/production.yml
|
|
52
|
+
config/settings/test.yml
|
|
53
|
+
|
|
54
|
+
You can now edit them to adjust to your needs.
|
|
55
|
+
|
|
56
|
+
> Note: By default, the config environment will match the Rails environment (`Rails.env`). This can be changed by setting `config.environment`.
|
|
57
|
+
|
|
58
|
+
### Installing on Padrino
|
|
59
|
+
|
|
60
|
+
Add the gem to your `Gemfile` and run `bundle install` to install it. Then edit `app.rb` and register `Config`
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
register Config
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### Installing on Sinatra
|
|
67
|
+
|
|
68
|
+
Add the gem to your `Gemfile` and run `bundle install` to install it. Afterwards in need to register `Config` in your app and give it a root so it can find the config files.
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
set :root, File.dirname(__FILE__)
|
|
72
|
+
register Config
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Installing on other ruby projects
|
|
76
|
+
|
|
77
|
+
Add the gem to your `Gemfile` and run `bundle install` to install it. Then initialize `Config` manually within your configure block.
|
|
78
|
+
|
|
79
|
+
```ruby
|
|
80
|
+
Config.load_and_set_settings(Config.setting_files("/path/to/config_root", "your_project_environment"))
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
It's also possible to initialize `Config` manually within your configure block if you want to just give it some yml paths to load from.
|
|
84
|
+
|
|
85
|
+
```ruby
|
|
86
|
+
Config.load_and_set_settings("/path/to/yaml1", "/path/to/yaml2", ...)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Accessing the Settings object
|
|
90
|
+
|
|
91
|
+
After installing the gem, `Settings` object will become available globally and by default will be compiled from the files listed below. Settings defined in files that are lower in the list override settings higher.
|
|
92
|
+
|
|
93
|
+
config/settings.yml
|
|
94
|
+
config/settings/#{environment}.yml
|
|
95
|
+
config/environments/#{environment}.yml
|
|
96
|
+
|
|
97
|
+
config/settings.local.yml
|
|
98
|
+
config/settings/#{environment}.local.yml
|
|
99
|
+
config/environments/#{environment}.local.yml
|
|
100
|
+
|
|
101
|
+
Entries can be accessed via object member notation:
|
|
102
|
+
|
|
103
|
+
```ruby
|
|
104
|
+
Settings.my_config_entry
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Nested entries are supported:
|
|
108
|
+
|
|
109
|
+
```ruby
|
|
110
|
+
Settings.my_section.some_entry
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Alternatively, you can also use the `[]` operator if you don't know which exact setting you need to access ahead of time.
|
|
114
|
+
|
|
115
|
+
```ruby
|
|
116
|
+
# All the following are equivalent to Settings.my_section.some_entry
|
|
117
|
+
Settings.my_section[:some_entry]
|
|
118
|
+
Settings.my_section['some_entry']
|
|
119
|
+
Settings[:my_section][:some_entry]
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Reloading settings
|
|
123
|
+
|
|
124
|
+
You can reload the Settings object at any time by running `Settings.reload!`.
|
|
125
|
+
|
|
126
|
+
### Reloading settings and config files
|
|
127
|
+
|
|
128
|
+
You can also reload the `Settings` object from different config files at runtime.
|
|
129
|
+
|
|
130
|
+
For example, in your tests if you want to test the production settings, you can:
|
|
131
|
+
|
|
132
|
+
```ruby
|
|
133
|
+
Rails.env = "production"
|
|
134
|
+
Settings.reload_from_files(
|
|
135
|
+
Rails.root.join("config", "settings.yml").to_s,
|
|
136
|
+
Rails.root.join("config", "settings", "#{Rails.env}.yml").to_s,
|
|
137
|
+
Rails.root.join("config", "environments", "#{Rails.env}.yml").to_s
|
|
138
|
+
)
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Environment specific config files
|
|
142
|
+
|
|
143
|
+
You can have environment specific config files. Environment specific config entries take precedence over common config entries.
|
|
144
|
+
|
|
145
|
+
Example development environment config file:
|
|
146
|
+
|
|
147
|
+
```ruby
|
|
148
|
+
#{Rails.root}/config/environments/development.yml
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Example production environment config file:
|
|
152
|
+
|
|
153
|
+
```ruby
|
|
154
|
+
#{Rails.root}/config/environments/production.yml
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Extra sources
|
|
158
|
+
|
|
159
|
+
You can load extra sources during initialization by setting the `extra_sources` configuration option.
|
|
160
|
+
|
|
161
|
+
```ruby
|
|
162
|
+
Config.setup do |config|
|
|
163
|
+
config.extra_sources = [
|
|
164
|
+
'path/to/extra_source.yml', # String: loads extra_source.yml
|
|
165
|
+
{ api_key: ENV['API_KEY'] }, # Hash: direct hash source
|
|
166
|
+
MyCustomSource.new, # Object: custom source object that responds to `load`
|
|
167
|
+
]
|
|
168
|
+
end
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
This will also overwrite the same config entries from the main file.
|
|
172
|
+
|
|
173
|
+
### Developer specific config files
|
|
174
|
+
|
|
175
|
+
If you want to have local settings, specific to your machine or development environment, you can use the following files, which are automatically `.gitignore` :
|
|
176
|
+
|
|
177
|
+
```ruby
|
|
178
|
+
Rails.root.join("config", "settings.local.yml").to_s,
|
|
179
|
+
Rails.root.join("config", "settings", "#{Rails.env}.local.yml").to_s,
|
|
180
|
+
Rails.root.join("config", "environments", "#{Rails.env}.local.yml").to_s
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
**NOTE:** The file `settings.local.yml` will not be loaded in tests to prevent local configuration from causing flaky or non-deterministic tests. Environment-specific files (e.g. `settings/test.local.yml`) will still be loaded to allow test-specific credentials.
|
|
184
|
+
|
|
185
|
+
### Adding sources at runtime
|
|
186
|
+
|
|
187
|
+
You can add new YAML config files at runtime. Just use:
|
|
188
|
+
|
|
189
|
+
```ruby
|
|
190
|
+
Settings.add_source!("/path/to/source.yml")
|
|
191
|
+
Settings.reload!
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
This will use the given source.yml file and use its settings to overwrite any previous ones.
|
|
195
|
+
|
|
196
|
+
On the other hand, you can prepend a YML file to the list of configuration files:
|
|
197
|
+
|
|
198
|
+
```ruby
|
|
199
|
+
Settings.prepend_source!("/path/to/source.yml")
|
|
200
|
+
Settings.reload!
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
This will do the same as `add_source`, but the given YML file will be loaded first (instead of last) and its settings will be overwritten by any other configuration file. This is especially useful if you want to define defaults.
|
|
204
|
+
|
|
205
|
+
One thing I like to do for my Rails projects is provide a local.yml config file that is .gitignored (so its independent per developer). Then I create a new initializer in `config/initializers/add_local_config.rb` with the contents
|
|
206
|
+
|
|
207
|
+
```ruby
|
|
208
|
+
Settings.add_source!("#{Rails.root}/config/settings/local.yml")
|
|
209
|
+
Settings.reload!
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
> Note: this is an example usage, it is easier to just use the default local
|
|
213
|
+
> files `settings.local.yml`, `settings/#{Rails.env}.local.yml` and
|
|
214
|
+
> `environments/#{Rails.env}.local.yml` for your developer specific settings.
|
|
215
|
+
|
|
216
|
+
You also have the option to add a raw hash as a source. One use case might be storing settings in the database or in environment variables that overwrite what is in the YML files.
|
|
217
|
+
|
|
218
|
+
```ruby
|
|
219
|
+
Settings.add_source!({some_secret: ENV['some_secret']})
|
|
220
|
+
Settings.reload!
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
You may pass a hash to `prepend_source!` as well.
|
|
224
|
+
|
|
225
|
+
## Embedded Ruby (ERB)
|
|
226
|
+
|
|
227
|
+
Embedded Ruby is allowed in the YAML configuration files. ERB will be evaluated at load time by default, and when the `evaluate_erb_in_yaml` configuration is set to `true`.
|
|
228
|
+
|
|
229
|
+
Consider the two following config files.
|
|
230
|
+
|
|
231
|
+
* ```#{Rails.root}/config/settings.yml```
|
|
232
|
+
|
|
233
|
+
```yaml
|
|
234
|
+
size: 1
|
|
235
|
+
server: google.com
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
* ```#{Rails.root}/config/environments/development.yml```
|
|
239
|
+
|
|
240
|
+
```yaml
|
|
241
|
+
size: 2
|
|
242
|
+
computed: <%= 1 + 2 + 3 %>
|
|
243
|
+
section:
|
|
244
|
+
size: 3
|
|
245
|
+
servers: [ {name: yahoo.com}, {name: amazon.com} ]
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
Notice that the environment specific config entries overwrite the common entries.
|
|
249
|
+
|
|
250
|
+
```ruby
|
|
251
|
+
Settings.size # => 2
|
|
252
|
+
Settings.server # => google.com
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
Notice the embedded Ruby.
|
|
256
|
+
|
|
257
|
+
```ruby
|
|
258
|
+
Settings.computed # => 6
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
Notice that object member notation is maintained even in nested entries.
|
|
262
|
+
|
|
263
|
+
```ruby
|
|
264
|
+
Settings.section.size # => 3
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
Notice array notation and object member notation is maintained.
|
|
268
|
+
|
|
269
|
+
```ruby
|
|
270
|
+
Settings.section.servers[0].name # => yahoo.com
|
|
271
|
+
Settings.section.servers[1].name # => amazon.com
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
## Configuration
|
|
275
|
+
|
|
276
|
+
There are multiple configuration options available, however you can customize `Config` only once, preferably during application initialization phase:
|
|
277
|
+
|
|
278
|
+
```ruby
|
|
279
|
+
Config.setup do |config|
|
|
280
|
+
config.const_name = 'Settings'
|
|
281
|
+
# ...
|
|
282
|
+
end
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
After installing `Config` in Rails, you will find automatically generated file that contains default configuration located at `config/initializers/config.rb`.
|
|
286
|
+
|
|
287
|
+
### General
|
|
288
|
+
|
|
289
|
+
* `const_name` - name of the object holding your settings. Default: `'Settings'`
|
|
290
|
+
* `evaluate_erb_in_yaml` - evaluate ERB in YAML config files. Set to false if the config file contains ERB that should not be evaluated at load time. Default: `true`
|
|
291
|
+
* `file_name` - name of the file to store general keys accessible in all environments. Default: `'settings'` - located at `config/settings.yml`
|
|
292
|
+
* `dir_name` - name of the directory to store environment-specific files. Default: `'settings'` - located at `config/settings/`
|
|
293
|
+
|
|
294
|
+
### Merge customization
|
|
295
|
+
|
|
296
|
+
* `overwrite_arrays` - overwrite arrays found in previously loaded settings file. Default: `true`
|
|
297
|
+
* `merge_hash_arrays` - merge hashes inside of arrays from previously loaded settings files. Makes sense only when `overwrite_arrays = false`. Default: `false`
|
|
298
|
+
* `knockout_prefix` - ability to remove elements of the array set in earlier loaded settings file. Makes sense only when `overwrite_arrays = false`, otherwise array settings would be overwritten by default. Default: `nil`
|
|
299
|
+
* `merge_nil_values` - `nil` values will overwrite an existing value when merging configs. Default: `true`.
|
|
300
|
+
|
|
301
|
+
```ruby
|
|
302
|
+
# merge_nil_values is true by default
|
|
303
|
+
c = Config.load_files("./spec/fixtures/development.yml") # => #<Config::Options size=2, ...>
|
|
304
|
+
c.size # => 2
|
|
305
|
+
c.merge!(size: nil) => #<Config::Options size=nil, ...>
|
|
306
|
+
c.size # => nil
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
```ruby
|
|
310
|
+
# To reject nil values when merging settings:
|
|
311
|
+
Config.setup do |config|
|
|
312
|
+
config.merge_nil_values = false
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
c = Config.load_files("./spec/fixtures/development.yml") # => #<Config::Options size=2, ...>
|
|
316
|
+
c.size # => 2
|
|
317
|
+
c.merge!(size: nil) => #<Config::Options size=nil, ...>
|
|
318
|
+
c.size # => 2
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
Check [Deep Merge](https://github.com/danielsdeleo/deep_merge) for more details.
|
|
322
|
+
|
|
323
|
+
### Validation
|
|
324
|
+
|
|
325
|
+
With Ruby 2.1 or newer, you can optionally define a [schema](https://github.com/dry-rb/dry-schema) or [contract](https://github.com/dry-rb/dry-validation) (added in `config-2.1`) using [dry-rb](https://github.com/dry-rb) to validate presence (and type) of specific config values. Generally speaking contracts allow to describe more complex validations with depencecies between fields.
|
|
326
|
+
|
|
327
|
+
If you provide either validation option (or both) it will automatically be used to validate your config. If validation fails it will raise a `Config::Validation::Error` containing information about all the mismatches between the schema and your config.
|
|
328
|
+
|
|
329
|
+
Both examples below demonstrates how to ensure that the configuration has an optional `email` and the `youtube` structure with the `api_key` field filled. The contract adds an additional rule.
|
|
330
|
+
|
|
331
|
+
#### Contract
|
|
332
|
+
|
|
333
|
+
Leverage dry-validation, you can create a contract with a params schema and rules:
|
|
334
|
+
|
|
335
|
+
```ruby
|
|
336
|
+
class ConfigContract < Dry::Validation::Contract
|
|
337
|
+
params do
|
|
338
|
+
optional(:email).maybe(:str?)
|
|
339
|
+
|
|
340
|
+
required(:youtube).schema do
|
|
341
|
+
required(:api_key).filled
|
|
342
|
+
end
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
rule(:email) do
|
|
346
|
+
unless /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i.match?(value)
|
|
347
|
+
key.failure('has invalid format')
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
Config.setup do |config|
|
|
353
|
+
config.validation_contract = ConfigContract.new
|
|
354
|
+
end
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
The above example adds a rule to ensure the `email` is valid by matching it against the provided regular expression.
|
|
358
|
+
|
|
359
|
+
Check [dry-validation](https://github.com/dry-rb/dry-validation) for more details.
|
|
360
|
+
|
|
361
|
+
#### Schema
|
|
362
|
+
|
|
363
|
+
You may also specify a schema using [dry-schema](https://github.com/dry-rb/dry-schema):
|
|
364
|
+
|
|
365
|
+
```ruby
|
|
366
|
+
Config.setup do |config|
|
|
367
|
+
# ...
|
|
368
|
+
config.schema do
|
|
369
|
+
optional(:email).maybe(:str?)
|
|
370
|
+
|
|
371
|
+
required(:youtube).schema do
|
|
372
|
+
required(:api_key).filled
|
|
373
|
+
end
|
|
374
|
+
end
|
|
375
|
+
end
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
Check [dry-schema](https://github.com/dry-rb/dry-schema) for more details.
|
|
379
|
+
|
|
380
|
+
### Missing keys
|
|
381
|
+
|
|
382
|
+
For an example settings file:
|
|
383
|
+
|
|
384
|
+
```yaml
|
|
385
|
+
size: 1
|
|
386
|
+
server: google.com
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
You can test if a value was set for a given key using `key?` and its alias `has_key?`:
|
|
390
|
+
|
|
391
|
+
```ruby
|
|
392
|
+
Settings.key?(:path)
|
|
393
|
+
# => false
|
|
394
|
+
Settings.key?(:server)
|
|
395
|
+
# => true
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
By default, accessing to a missing key returns `nil`:
|
|
399
|
+
|
|
400
|
+
```ruby
|
|
401
|
+
Settings.key?(:path)
|
|
402
|
+
# => false
|
|
403
|
+
Settings.path
|
|
404
|
+
# => nil
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
This is not "typo-safe". To solve this problem, you can configure the `fail_on_missing` option:
|
|
408
|
+
|
|
409
|
+
```ruby
|
|
410
|
+
Config.setup do |config|
|
|
411
|
+
config.fail_on_missing = true
|
|
412
|
+
# ...
|
|
413
|
+
end
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
So it will raise a `KeyError` when accessing a non-existing key (similar to `Hash#fetch` behaviour):
|
|
417
|
+
|
|
418
|
+
```ruby
|
|
419
|
+
Settings.path
|
|
420
|
+
# => raises KeyError: key not found: :path
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
### Environment variables
|
|
424
|
+
|
|
425
|
+
See section below for more details.
|
|
426
|
+
|
|
427
|
+
## Working with environment variables
|
|
428
|
+
|
|
429
|
+
To load environment variables from the `ENV` object, that will override any settings defined in files, set the `use_env` to true in your `config/initializers/config.rb` file:
|
|
430
|
+
|
|
431
|
+
```ruby
|
|
432
|
+
Config.setup do |config|
|
|
433
|
+
config.const_name = 'Settings'
|
|
434
|
+
config.use_env = true
|
|
435
|
+
end
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
Now config would read values from the ENV object to the settings. For the example above it would look for keys starting with `Settings`:
|
|
439
|
+
|
|
440
|
+
```ruby
|
|
441
|
+
ENV['Settings.section.size'] = 1
|
|
442
|
+
ENV['Settings.section.server'] = 'google.com'
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
It won't work with arrays, though.
|
|
446
|
+
|
|
447
|
+
It is considered an error to use environment variables to simultaneously assign a "flat" value and a multi-level value to a key.
|
|
448
|
+
|
|
449
|
+
```ruby
|
|
450
|
+
# Raises an error when settings are loaded
|
|
451
|
+
ENV['BACKEND_DATABASE'] = 'development'
|
|
452
|
+
ENV['BACKEND_DATABASE_USER'] = 'postgres'
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
Instead, specify keys of equal depth in the environment variable names:
|
|
456
|
+
|
|
457
|
+
```ruby
|
|
458
|
+
ENV['BACKEND_DATABASE_NAME'] = 'development'
|
|
459
|
+
ENV['BACKEND_DATABASE_USER'] = 'postgres'
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
### Working with Heroku
|
|
463
|
+
|
|
464
|
+
Heroku uses ENV object to store sensitive settings. You cannot upload such files to Heroku because it's ephemeral filesystem gets recreated from the git sources on each instance refresh. To use config with Heroku just set the `use_env` var to `true` as mentioned above.
|
|
465
|
+
|
|
466
|
+
To upload your local values to Heroku you could ran `bundle exec rake config:heroku`.
|
|
467
|
+
|
|
468
|
+
### Fine-tuning
|
|
469
|
+
|
|
470
|
+
You can customize how environment variables are processed:
|
|
471
|
+
|
|
472
|
+
* `env_prefix` (default: `const_name`) - load only ENV variables starting with this prefix (case-sensitive)
|
|
473
|
+
* `env_separator` (default: `'.'`) - what string to use as level separator - default value of `.` works well with Heroku, but you might want to change it for example for `__` to easy override settings from command line, where using dots in variable names might not be allowed (eg. Bash)
|
|
474
|
+
* `env_converter` (default: `:downcase`) - how to process variables names:
|
|
475
|
+
* `nil` - no change
|
|
476
|
+
* `:downcase` - convert to lower case
|
|
477
|
+
* `env_parse_values` (default: `true`) - try to parse values to a correct type (`Boolean`, `Integer`, `Float`, `String`)
|
|
478
|
+
|
|
479
|
+
For instance, given the following environment:
|
|
480
|
+
|
|
481
|
+
```bash
|
|
482
|
+
SETTINGS__SECTION__SERVER_SIZE=1
|
|
483
|
+
SETTINGS__SECTION__SERVER=google.com
|
|
484
|
+
SETTINGS__SECTION__SSL_ENABLED=false
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
And the following configuration:
|
|
488
|
+
|
|
489
|
+
```ruby
|
|
490
|
+
Config.setup do |config|
|
|
491
|
+
config.use_env = true
|
|
492
|
+
config.env_prefix = 'SETTINGS'
|
|
493
|
+
config.env_separator = '__'
|
|
494
|
+
config.env_converter = :downcase
|
|
495
|
+
config.env_parse_values = true
|
|
496
|
+
end
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
The following settings will be available:
|
|
500
|
+
|
|
501
|
+
```ruby
|
|
502
|
+
Settings.section.server_size # => 1
|
|
503
|
+
Settings.section.server # => 'google.com'
|
|
504
|
+
Settings.section.ssl_enabled # => false
|
|
505
|
+
```
|
|
506
|
+
|
|
507
|
+
### Working with AWS Secrets Manager
|
|
508
|
+
|
|
509
|
+
It is possible to parse variables stored in an AWS Secrets Manager Secret as if they were environment variables by using `Config::Sources::EnvSource`.
|
|
510
|
+
|
|
511
|
+
For example, the plaintext secret might look like this:
|
|
512
|
+
|
|
513
|
+
```json
|
|
514
|
+
{
|
|
515
|
+
"Settings.foo": "hello",
|
|
516
|
+
"Settings.bar": "world",
|
|
517
|
+
}
|
|
518
|
+
```
|
|
519
|
+
|
|
520
|
+
In order to load those settings, fetch the settings from AWS Secrets Manager, parse the plaintext as JSON, pass the resulting `Hash` into a new `EnvSource`, load the new source, and reload.
|
|
521
|
+
|
|
522
|
+
```ruby
|
|
523
|
+
# fetch secrets from AWS
|
|
524
|
+
client = Aws::SecretsManager::Client.new
|
|
525
|
+
response = client.get_secret_value(secret_id: "#{ENV['ENVIRONMENT']}/my_application")
|
|
526
|
+
secrets = JSON.parse(response.secret_string)
|
|
527
|
+
|
|
528
|
+
# load secrets into config
|
|
529
|
+
secret_source = Config::Sources::EnvSource.new(secrets)
|
|
530
|
+
Settings.add_source!(secret_source)
|
|
531
|
+
Settings.reload!
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
In this case, the following settings will be available:
|
|
535
|
+
|
|
536
|
+
```ruby
|
|
537
|
+
Settings.foo # => "hello"
|
|
538
|
+
Settings.bar # => "world"
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
By default, `EnvSource` will use configuration for `env_prefix`, `env_separator`, `env_converter`, and `env_parse_values`, but any of these can be overridden in the constructor.
|
|
542
|
+
|
|
543
|
+
```ruby
|
|
544
|
+
secret_source = Config::Sources::EnvSource.new(secrets,
|
|
545
|
+
prefix: 'MyConfig',
|
|
546
|
+
separator: '__',
|
|
547
|
+
converter: nil,
|
|
548
|
+
parse_values: false)
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
## Contributing
|
|
552
|
+
|
|
553
|
+
You are very warmly welcome to help. Please follow our [contribution guidelines](CONTRIBUTING.md)
|
|
554
|
+
|
|
555
|
+
Any and all contributions offered in any form, past present or future are understood to be in complete agreement and acceptance with [MIT](LICENSE) license.
|
|
556
|
+
|
|
557
|
+
### Running specs
|
|
558
|
+
|
|
559
|
+
Setup
|
|
560
|
+
|
|
561
|
+
```sh
|
|
562
|
+
bundle install
|
|
563
|
+
bundle exec appraisal install
|
|
564
|
+
```
|
|
565
|
+
|
|
566
|
+
List defined appraisals:
|
|
567
|
+
|
|
568
|
+
```sh
|
|
569
|
+
bundle exec appraisal list
|
|
570
|
+
```
|
|
571
|
+
|
|
572
|
+
Run specs for specific appraisal:
|
|
573
|
+
|
|
574
|
+
```sh
|
|
575
|
+
bundle exec appraisal rails-6.1 rspec
|
|
576
|
+
```
|
|
577
|
+
|
|
578
|
+
Run specs for all appraisals:
|
|
579
|
+
|
|
580
|
+
```sh
|
|
581
|
+
bundle exec appraisal rspec
|
|
582
|
+
```
|
|
583
|
+
|
|
584
|
+
## Authors
|
|
585
|
+
|
|
586
|
+
* [Piotr Kuczynski](http://github.com/pkuczynski)
|
|
587
|
+
* [Fred Wu](http://github.com/fredwu)
|
|
588
|
+
* [Jacques Crocker](http://github.com/railsjedi)
|
|
589
|
+
* Inherited from [AppConfig](http://github.com/cjbottaro/app_config) by [Christopher J. Bottaro](http://github.com/cjbottaro)
|
|
590
|
+
|
|
591
|
+
## Contributors
|
|
592
|
+
|
|
593
|
+
### Code Contributors
|
|
594
|
+
|
|
595
|
+
This project exists thanks to all the people who contribute and you are very warmly welcome to help. Please follow our [contribution guidelines](CONTRIBUTING.md).
|
|
596
|
+
|
|
597
|
+
Any and all contributions offered in any form, past present or future are understood to be in complete agreement and acceptance with the [MIT](LICENSE) license.
|
|
598
|
+
|
|
599
|
+
[](https://github.com/rubyconfig/config/graphs/contributors)
|
|
600
|
+
|
|
601
|
+
### Financial Contributors
|
|
602
|
+
|
|
603
|
+
[Become a backer](https://opencollective.com/rubyconfig#backer) and support us with a small monthly donation to help us continue our activities. Thank you if you already one! 🙏
|
|
604
|
+
|
|
605
|
+
[](https://opencollective.com/rubyconfig)
|
|
606
|
+
|
|
607
|
+
#### Sponsors
|
|
608
|
+
|
|
609
|
+
Support this project by becoming a [sponsor](https://opencollective.com/rubyconfig#sponsor). Your logo will show up here with a link to your website.
|
|
610
|
+
|
|
611
|
+
[](https://opencollective.com/rubyconfig)
|
|
612
|
+
|
|
613
|
+
## License
|
|
614
|
+
|
|
615
|
+
Copyright (C) Piotr Kuczynski. Released under the [MIT License](LICENSE.md).
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
require_relative 'lib/config/version'
|
|
2
|
+
require_relative 'lib/config/dry_validation_requirements'
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |s|
|
|
5
|
+
s.name = 'config'
|
|
6
|
+
s.version = Config::VERSION
|
|
7
|
+
s.date = Time.now.strftime '%F'
|
|
8
|
+
s.authors = ['Piotr Kuczynski', 'Fred Wu', 'Jacques Crocker']
|
|
9
|
+
s.email = %w[piotr.kuczynski@gmail.com ifredwu@gmail.com railsjedi@gmail.com]
|
|
10
|
+
s.summary = 'Effortless multi-environment settings in Rails, Sinatra, Padrino and others'
|
|
11
|
+
s.description = 'Easiest way to manage multi-environment settings in any ruby project or framework: ' +
|
|
12
|
+
'Rails, Sinatra, Padrino and others'
|
|
13
|
+
s.homepage = 'https://github.com/rubyconfig/config'
|
|
14
|
+
s.license = 'MIT'
|
|
15
|
+
s.extra_rdoc_files = %w[README.md CHANGELOG.md CONTRIBUTING.md LICENSE.md]
|
|
16
|
+
s.rdoc_options = ['--charset=UTF-8']
|
|
17
|
+
|
|
18
|
+
s.metadata = {
|
|
19
|
+
'changelog_uri' => "https://github.com/rubyconfig/config/blob/master/CHANGELOG.md",
|
|
20
|
+
'funding_uri' => 'https://opencollective.com/rubyconfig/donate',
|
|
21
|
+
'source_code_uri' => 'https://github.com/rubyconfig/config',
|
|
22
|
+
'bug_tracker_uri' => 'https://github.com/rubyconfig/config/issues'
|
|
23
|
+
}
|
|
24
|
+
s.files = `git ls-files`.split($/)
|
|
25
|
+
s.files.select! { |file| /(^lib\/|^\w+.md$|\.gemspec$)/ =~ file }
|
|
26
|
+
|
|
27
|
+
s.require_paths = ['lib']
|
|
28
|
+
s.required_ruby_version = '>= 2.6.0'
|
|
29
|
+
|
|
30
|
+
s.add_dependency 'deep_merge', '~> 1.2', '>= 1.2.1'
|
|
31
|
+
s.add_dependency 'ostruct'
|
|
32
|
+
|
|
33
|
+
s.add_development_dependency 'rake', '~> 12.0', '>= 12.0.0'
|
|
34
|
+
|
|
35
|
+
# Testing
|
|
36
|
+
s.add_development_dependency 'appraisal', '~> 2.5', '>= 2.5.0'
|
|
37
|
+
s.add_development_dependency 'dry-validation', *Config::DryValidationRequirements::VERSIONS
|
|
38
|
+
s.add_development_dependency 'rspec', '~> 3.9', '>= 3.9.0'
|
|
39
|
+
|
|
40
|
+
# Default RSpec run will test against latest Rails app
|
|
41
|
+
unless ENV['APPRAISAL_INITIALIZED'] || ENV['GITHUB_ACTIONS']
|
|
42
|
+
gems_to_install = /gem "(.*?)", "(.*?)"(?!, platform: (?!\[:ruby\]))/
|
|
43
|
+
|
|
44
|
+
if Dir.exist?('gemfiles')
|
|
45
|
+
rails_gemfiles = Dir['gemfiles/rails*.gemfile'].sort
|
|
46
|
+
unless rails_gemfiles.empty?
|
|
47
|
+
File.read(rails_gemfiles.last).scan(gems_to_install) do |name, version|
|
|
48
|
+
s.add_development_dependency name, version
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
if ENV['GITHUB_ACTIONS']
|
|
55
|
+
# Code coverage is needed only in CI
|
|
56
|
+
s.add_development_dependency 'simplecov', '~> 0.18.5' if RUBY_ENGINE == 'ruby'
|
|
57
|
+
else
|
|
58
|
+
# Static code analysis to be used locally
|
|
59
|
+
s.add_development_dependency 'mdl', '~> 0.9', '>= 0.9.0'
|
|
60
|
+
s.add_development_dependency 'rubocop', '~> 0.85.0'
|
|
61
|
+
end
|
|
62
|
+
end
|