ef-config 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +83 -0
- data/LICENSE.md +26 -0
- data/README.md +404 -0
- data/config.gemspec +51 -0
- data/lib/config.rb +107 -0
- data/lib/config/compatibility.rb +3 -0
- data/lib/config/integrations/heroku.rb +59 -0
- data/lib/config/integrations/rails/engine.rb +9 -0
- data/lib/config/integrations/rails/railtie.rb +38 -0
- data/lib/config/integrations/sinatra.rb +26 -0
- data/lib/config/options.rb +191 -0
- data/lib/config/rack/reloader.rb +15 -0
- data/lib/config/sources/hash_source.rb +16 -0
- data/lib/config/sources/yaml_source.rb +31 -0
- data/lib/config/tasks/heroku.rake +7 -0
- data/lib/config/validation/error.rb +24 -0
- data/lib/config/validation/schema.rb +21 -0
- data/lib/config/validation/validate.rb +19 -0
- data/lib/config/version.rb +3 -0
- data/lib/generators/config/install_generator.rb +32 -0
- data/lib/generators/config/templates/config.rb +45 -0
- data/lib/generators/config/templates/settings.local.yml +0 -0
- data/lib/generators/config/templates/settings.yml +0 -0
- data/lib/generators/config/templates/settings/development.yml +0 -0
- data/lib/generators/config/templates/settings/production.yml +0 -0
- data/lib/generators/config/templates/settings/test.yml +0 -0
- data/spec/app/rails_5/README.md +24 -0
- metadata +334 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 49c965a2880e9334514098e3f84dc4d39d6ba6be677a003ec62ad7d226f36f54
|
4
|
+
data.tar.gz: 0f5ac59817fbb6b8b0111e0e11f6ae0732c1fc633d2603230a8c5869c60553ee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 27ec998d3dc1181a838de92a72f4d7799eb88f9db1796fa7d93a60f13bb547b6e173e3d0002d69aff7e2f8db079a6f80ab4f88e8fbd06f48b9f72d3ab12da736
|
7
|
+
data.tar.gz: d3a014563d58a84c73bc23dc6be36a36f4cc4da31d5b65da6cc4ebbbbe4ce22f29d594e773f2c22dfe8cc60bad005146cb6b1ead161682b0784842dc056e7201
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## Unreleased
|
4
|
+
|
5
|
+
### New features
|
6
|
+
* Add ability to validate config schema ([#155](https://github.com/railsconfig/config/pull/155) thanks to [@ok32](https://github.com/ok32))
|
7
|
+
|
8
|
+
## 1.4.0
|
9
|
+
|
10
|
+
### New features
|
11
|
+
* Added support for passing a raw ruby hash into to both `Settings.add_source!` and `Settings.prepend_source!` ([#108](https://github.com/railsconfig/config/pull/159) thanks to [@halloffame](https://github.com/halloffame))
|
12
|
+
|
13
|
+
### Bug fixes
|
14
|
+
* Added new reserved name `test` ([#158](https://github.com/railsconfig/config/pull/158) thanks to [@milushov](https://github.com/milushov))
|
15
|
+
* `to_hash` should not replace nested config objects with Hash ([#160](https://github.com/railsconfig/config/issues/160) thanks to [@seikichi](https://github.com/seikichi))
|
16
|
+
|
17
|
+
## 1.3.0
|
18
|
+
|
19
|
+
* **WARNING:** Overwrite arrays found in previously loaded settings file ([#137](https://github.com/railsconfig/config/pull/137) thanks to [@Fryguy](https://github.com/Fryguy) and [@dtaniwaki](https://github.com/dtaniwaki)) - this is a change breaking previous behaviour. If you want to keep Config to work as before, which is merging arrays found in following loaded settings file, please add `config.overwrite_arrays = false` to your Config initializer
|
20
|
+
* Changed default ENV variables loading settings to downcase variable names and parse values
|
21
|
+
* Added parsing ENV variables values to Float type
|
22
|
+
* Change method definition order in Rails integration module to prevent undefined method `preload` error (based on [@YaroSpace](https://github.com/YaroSpace) suggestion in [#111](https://github.com/railsconfig/config/issues/111)
|
23
|
+
|
24
|
+
## 1.2.1
|
25
|
+
|
26
|
+
* Fixed support for multilevel settings loaded from ENV variables (inspired by [@cbeer](https://github.com/cbeer) in [#144](https://github.com/railsconfig/config/pull/144))
|
27
|
+
|
28
|
+
## 1.2.0
|
29
|
+
|
30
|
+
* Add ability to load settings from ENV variables ([#108](https://github.com/railsconfig/config/issues/108) thanks to [@vinceve](https://github.com/vinceve) and [@spalladino](https://github.com/spalladino))
|
31
|
+
* Removed Rails 5 deprecation warnings for prepend_before_filter ([#141](https://github.com/railsconfig/config/pull/141))
|
32
|
+
|
33
|
+
## 1.1.1
|
34
|
+
|
35
|
+
* Downgrade minimum ruby version to 2.0.0 ([#136](https://github.com/railsconfig/config/issues/136))
|
36
|
+
|
37
|
+
## 1.1.0
|
38
|
+
|
39
|
+
* Add ability to specify knockout_prefix option for deep_merge
|
40
|
+
* Minor code and documentation refactoring and cleanup
|
41
|
+
|
42
|
+
## 1.0.0
|
43
|
+
|
44
|
+
* `RailsConfig` is now officially renamed to `Config`
|
45
|
+
* Fixed array descent when converting to hash ([#89](https://github.com/railsconfig/config/pull/89))
|
46
|
+
* Catch OpenStruct reserved keywords ([#95](https://github.com/railsconfig/config/pull/95) by [@dudo](https://github.com/dudo))
|
47
|
+
* Allows loading before app configuration process ([#107](https://github.com/railsconfig/config/pull/107) by [@Antiarchitect](https://github.com/Antiarchitect))
|
48
|
+
* `deep_merge` is now properly managed via gemspec ([#110](https://github.com/railsconfig/config/pull/110))
|
49
|
+
* Added `prepend_source!` ([#102](https://github.com/railsconfig/config/pull/102))
|
50
|
+
|
51
|
+
## 0.99
|
52
|
+
|
53
|
+
* Released deprecated gem migrating to the new name
|
54
|
+
|
55
|
+
## 0.5.0.beta1
|
56
|
+
|
57
|
+
* Ability to use in Settings file keywords reserved for OpenStruct: select, collect ([#95](https://github.com/railsjedi/config/issues/95))
|
58
|
+
* Made config work without Rails as a hard dependency ([#86](https://github.com/railsjedi/config/issues/86), [#88](https://github.com/railsjedi/config/issues/88))
|
59
|
+
* Fix generate error when .gitignore is missing ([#85](https://github.com/railsjedi/config/issues/85))
|
60
|
+
* Fix deprecation warning on File.exists? ([#81](https://github.com/railsjedi/config/issues/81))
|
61
|
+
* Add a shortcut method for setting files ([#67](https://github.com/railsjedi/config/issues/67))
|
62
|
+
* Improve YAMLSource load error message by outputting offending file path ([#88](https://github.com/railsjedi/config/issues/88))
|
63
|
+
|
64
|
+
## 0.4.2
|
65
|
+
|
66
|
+
* Ability to specify the app name when calling the Heroku rake task ([#75](https://github.com/railsjedi/config/issues/75))
|
67
|
+
|
68
|
+
## 0.4.1
|
69
|
+
|
70
|
+
* Fixed compatibility with Rails 4.1 ([#72](https://github.com/railsjedi/config/issues/72))
|
71
|
+
* Testing suite verifies compatibility with Rails 3.2, 4.0 and 4.1
|
72
|
+
|
73
|
+
## 0.4.0
|
74
|
+
|
75
|
+
* Compatibility with Heroku ([#64](https://github.com/railsjedi/config/issues/64))
|
76
|
+
|
77
|
+
## 0.3.4
|
78
|
+
|
79
|
+
* Expose Settings in application.rb, so you don't have to duplicate configuration for each environment file ([#59](https://github.com/railsjedi/config/issues/59))
|
80
|
+
* Adding support for Rails 4.1.0.rc ([#70](https://github.com/railsjedi/config/issues/70))
|
81
|
+
|
82
|
+
|
83
|
+
|
data/LICENSE.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2010-2014 Jacques Crocker, Fred Wu, Piotr Kuczynski
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
22
|
+
|
23
|
+
Third-party materials and licenses:
|
24
|
+
|
25
|
+
* Config contains Deep Merge (deep_merge.rb) which is governed by the MIT license
|
26
|
+
Copyright (C) 2008 Steve Midgley
|
data/README.md
ADDED
@@ -0,0 +1,404 @@
|
|
1
|
+
# Config
|
2
|
+
|
3
|
+
[![Build Status](https://api.travis-ci.org/railsconfig/config.svg?branch=master)](http://travis-ci.org/railsconfig/config)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/config.svg)](http://badge.fury.io/rb/config)
|
5
|
+
[![Dependency Status](https://gemnasium.com/railsconfig/config.svg)](https://gemnasium.com/railsconfig/config)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/railsconfig/config/badges/gpa.svg)](https://codeclimate.com/github/railsconfig/config)
|
7
|
+
[![Issue Count](https://codeclimate.com/github/railsconfig/config/badges/issue_count.svg)](https://codeclimate.com/github/railsconfig/config)
|
8
|
+
[![Test Coverage](https://codeclimate.com/github/railsconfig/config/badges/coverage.svg)](https://codeclimate.com/github/railsconfig/config/coverage)
|
9
|
+
|
10
|
+
## Summary
|
11
|
+
|
12
|
+
Config helps you easily manage environment specific settings in an easy and usable manner.
|
13
|
+
|
14
|
+
## Features
|
15
|
+
|
16
|
+
* simple YAML config files
|
17
|
+
* config files support ERB
|
18
|
+
* config files support inheritance and multiple environments
|
19
|
+
* access config information via convenient object member notation
|
20
|
+
* support for multi-level settings (`Settings.group.subgroup.setting`)
|
21
|
+
* local developer settings ignored when committing the code
|
22
|
+
|
23
|
+
## Compatibility
|
24
|
+
|
25
|
+
* Ruby `2.x`
|
26
|
+
* Rails `>= 3.1`, `4` and `5`
|
27
|
+
* Padrino
|
28
|
+
* Sinatra
|
29
|
+
|
30
|
+
For older versions of Rails or Ruby use [AppConfig](http://github.com/fredwu/app_config).
|
31
|
+
|
32
|
+
## Installing
|
33
|
+
|
34
|
+
### Installing on Rails 3, 4 or 5
|
35
|
+
|
36
|
+
Add `gem 'config'` to your `Gemfile` and run `bundle install` to install it. Then run
|
37
|
+
|
38
|
+
rails g config:install
|
39
|
+
|
40
|
+
which will generate customizable config file `config/initializers/config.rb` and set of default settings files:
|
41
|
+
|
42
|
+
config/settings.yml
|
43
|
+
config/settings/development.yml
|
44
|
+
config/settings/production.yml
|
45
|
+
config/settings/test.yml
|
46
|
+
|
47
|
+
You can now edit them to adjust to your needs.
|
48
|
+
|
49
|
+
### Installing on Padrino
|
50
|
+
|
51
|
+
Add the gem to your `Gemfile` and run `bundle install` to install it. Then edit `app.rb` and register `Config`
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
register Config
|
55
|
+
```
|
56
|
+
|
57
|
+
### Installing on Sinatra
|
58
|
+
|
59
|
+
Add the gem to your `Gemfile` and run `bundle install` to install it. Afterwards in need to register `Config` in your
|
60
|
+
app and give it a root so it can find the config files.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
set :root, File.dirname(__FILE__)
|
64
|
+
register Config
|
65
|
+
```
|
66
|
+
|
67
|
+
### Installing on other ruby projects
|
68
|
+
|
69
|
+
Add the gem to your `Gemfile` and run `bundle install` to install it.
|
70
|
+
Then initialize `Config` manually within your configure block.
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
Config.load_and_set_settings(Config.setting_files("/path/to/config_root", "your_project_environment"))
|
74
|
+
```
|
75
|
+
|
76
|
+
It's also possible to initialize `Config` manually within your configure block if you want to just give it some yml
|
77
|
+
paths to load from.
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
Config.load_and_set_settings("/path/to/yaml1", "/path/to/yaml2", ...)
|
81
|
+
```
|
82
|
+
|
83
|
+
## Accessing the Settings object
|
84
|
+
|
85
|
+
After installing the gem, `Settings` object will become available globally and by default will be compiled from the
|
86
|
+
files listed below. Settings defined in files that are lower in the list override settings higher.
|
87
|
+
|
88
|
+
config/settings.yml
|
89
|
+
config/settings/#{environment}.yml
|
90
|
+
config/environments/#{environment}.yml
|
91
|
+
|
92
|
+
config/settings.local.yml
|
93
|
+
config/settings/#{environment}.local.yml
|
94
|
+
config/environments/#{environment}.local.yml
|
95
|
+
|
96
|
+
Entries can be accessed via object member notation:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
Settings.my_config_entry
|
100
|
+
```
|
101
|
+
|
102
|
+
Nested entries are supported:
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
Settings.my_section.some_entry
|
106
|
+
```
|
107
|
+
|
108
|
+
Alternatively, you can also use the `[]` operator if you don't know which exact setting you need to access ahead of time.
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
# All the following are equivalent to Settings.my_section.some_entry
|
112
|
+
Settings.my_section[:some_entry]
|
113
|
+
Settings.my_section['some_entry']
|
114
|
+
Settings[:my_section][:some_entry]
|
115
|
+
```
|
116
|
+
|
117
|
+
### Reloading settings
|
118
|
+
|
119
|
+
You can reload the Settings object at any time by running `Settings.reload!`.
|
120
|
+
|
121
|
+
### Reloading settings and config files
|
122
|
+
|
123
|
+
You can also reload the `Settings` object from different config files at runtime.
|
124
|
+
|
125
|
+
For example, in your tests if you want to test the production settings, you can:
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
Rails.env = "production"
|
129
|
+
Settings.reload_from_files(
|
130
|
+
Rails.root.join("config", "settings.yml").to_s,
|
131
|
+
Rails.root.join("config", "settings", "#{Rails.env}.yml").to_s,
|
132
|
+
Rails.root.join("config", "environments", "#{Rails.env}.yml").to_s
|
133
|
+
)
|
134
|
+
```
|
135
|
+
|
136
|
+
### Environment specific config files
|
137
|
+
|
138
|
+
You can have environment specific config files. Environment specific config entries take precedence over common config
|
139
|
+
entries.
|
140
|
+
|
141
|
+
Example development environment config file:
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
#{Rails.root}/config/environments/development.yml
|
145
|
+
```
|
146
|
+
|
147
|
+
Example production environment config file:
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
#{Rails.root}/config/environments/production.yml
|
151
|
+
```
|
152
|
+
|
153
|
+
### Developer specific config files
|
154
|
+
|
155
|
+
If you want to have local settings, specific to your machine or development environment,
|
156
|
+
you can use the following files, which are automatically `.gitignore` :
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
Rails.root.join("config", "settings.local.yml").to_s,
|
160
|
+
Rails.root.join("config", "settings", "#{Rails.env}.local.yml").to_s,
|
161
|
+
Rails.root.join("config", "environments", "#{Rails.env}.local.yml").to_s
|
162
|
+
```
|
163
|
+
|
164
|
+
### Adding sources at runtime
|
165
|
+
|
166
|
+
You can add new YAML config files at runtime. Just use:
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
Settings.add_source!("/path/to/source.yml")
|
170
|
+
Settings.reload!
|
171
|
+
```
|
172
|
+
|
173
|
+
This will use the given source.yml file and use its settings to overwrite any previous ones.
|
174
|
+
|
175
|
+
On the other hand, you can prepend a YML file to the list of configuration files:
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
Settings.prepend_source!("/path/to/source.yml")
|
179
|
+
Settings.reload!
|
180
|
+
```
|
181
|
+
|
182
|
+
This will do the same as `add_source`, but the given YML file will be loaded first (instead of last) and its settings
|
183
|
+
will be overwritten by any other configuration file. This is especially useful if you want to define defaults.
|
184
|
+
|
185
|
+
One thing I like to do for my Rails projects is provide a local.yml config file that is .gitignored (so its independent
|
186
|
+
per developer). Then I create a new initializer in `config/initializers/add_local_config.rb` with the contents
|
187
|
+
|
188
|
+
```ruby
|
189
|
+
Settings.add_source!("#{Rails.root}/config/settings/local.yml")
|
190
|
+
Settings.reload!
|
191
|
+
```
|
192
|
+
|
193
|
+
> Note: this is an example usage, it is easier to just use the default local files `settings.local.yml,
|
194
|
+
settings/#{Rails.env}.local.yml and environments/#{Rails.env}.local.yml` for your developer specific settings.
|
195
|
+
|
196
|
+
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.
|
197
|
+
|
198
|
+
```ruby
|
199
|
+
Settings.add_source!({some_secret: ENV['some_secret']})
|
200
|
+
Settings.reload!
|
201
|
+
```
|
202
|
+
|
203
|
+
You may pass a hash to `prepend_source!` as well.
|
204
|
+
|
205
|
+
## Embedded Ruby (ERB)
|
206
|
+
|
207
|
+
Embedded Ruby is allowed in the configuration files. Consider the two following config files.
|
208
|
+
|
209
|
+
* ```#{Rails.root}/config/settings.yml```
|
210
|
+
|
211
|
+
```yaml
|
212
|
+
size: 1
|
213
|
+
server: google.com
|
214
|
+
```
|
215
|
+
|
216
|
+
* ```#{Rails.root}/config/environments/development.yml```
|
217
|
+
|
218
|
+
```yaml
|
219
|
+
size: 2
|
220
|
+
computed: <%= 1 + 2 + 3 %>
|
221
|
+
section:
|
222
|
+
size: 3
|
223
|
+
servers: [ {name: yahoo.com}, {name: amazon.com} ]
|
224
|
+
```
|
225
|
+
|
226
|
+
Notice that the environment specific config entries overwrite the common entries.
|
227
|
+
|
228
|
+
```ruby
|
229
|
+
Settings.size # => 2
|
230
|
+
Settings.server # => google.com
|
231
|
+
```
|
232
|
+
|
233
|
+
Notice the embedded Ruby.
|
234
|
+
|
235
|
+
```ruby
|
236
|
+
Settings.computed # => 6
|
237
|
+
```
|
238
|
+
|
239
|
+
Notice that object member notation is maintained even in nested entries.
|
240
|
+
|
241
|
+
```ruby
|
242
|
+
Settings.section.size # => 3
|
243
|
+
```
|
244
|
+
|
245
|
+
Notice array notation and object member notation is maintained.
|
246
|
+
|
247
|
+
```ruby
|
248
|
+
Settings.section.servers[0].name # => yahoo.com
|
249
|
+
Settings.section.servers[1].name # => amazon.com
|
250
|
+
```
|
251
|
+
|
252
|
+
## Configuration
|
253
|
+
|
254
|
+
There are multiple configuration options available, however you can customize `Config` only once, preferably during
|
255
|
+
application initialization phase:
|
256
|
+
|
257
|
+
```ruby
|
258
|
+
Config.setup do |config|
|
259
|
+
config.const_name = 'Settings'
|
260
|
+
...
|
261
|
+
end
|
262
|
+
```
|
263
|
+
|
264
|
+
After installing `Config` in Rails, you will find automatically generated file that contains default configuration
|
265
|
+
located at `config/initializers/config.rb`.
|
266
|
+
|
267
|
+
### General
|
268
|
+
|
269
|
+
* `const_name` - name of the object holing you settings. Default: `'Settings'`
|
270
|
+
|
271
|
+
### Merge customization
|
272
|
+
|
273
|
+
* `overwrite_arrays` - overwrite arrays found in previously loaded settings file. Default: `true`
|
274
|
+
* `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`
|
275
|
+
|
276
|
+
Check [Deep Merge](https://github.com/danielsdeleo/deep_merge) for more details.
|
277
|
+
|
278
|
+
### Validation
|
279
|
+
|
280
|
+
With Ruby 2.1 or newer, you can optionally define a schema to validate presence (and type) of specific config values:
|
281
|
+
|
282
|
+
```ruby
|
283
|
+
Config.setup do |config|
|
284
|
+
# ...
|
285
|
+
config.schema do
|
286
|
+
required(:youtube).schema do
|
287
|
+
required(:api_key).filled
|
288
|
+
end
|
289
|
+
end
|
290
|
+
end
|
291
|
+
```
|
292
|
+
|
293
|
+
The above example demonstrates how to ensure that the configuration has the `youtube` structure
|
294
|
+
with the `api_key` field filled.
|
295
|
+
|
296
|
+
If you define a schema it will automatically be used to validate your config. If validation fails it will
|
297
|
+
raise a `Config::Validation::Error` containing a nice message with information about all the mismatches
|
298
|
+
between the schema and your config.
|
299
|
+
|
300
|
+
Check [dry-validation](https://github.com/dry-rb/dry-validation) for more details.
|
301
|
+
|
302
|
+
### Environment variables
|
303
|
+
|
304
|
+
See section below for more details.
|
305
|
+
|
306
|
+
## Working with environment variables
|
307
|
+
|
308
|
+
To load environment variables from the `ENV` object, that will override any settings defined in files, set the `use_env`
|
309
|
+
to true in your `config/initializers/config.rb` file:
|
310
|
+
|
311
|
+
```ruby
|
312
|
+
Config.setup do |config|
|
313
|
+
config.const_name = 'Settings'
|
314
|
+
config.use_env = true
|
315
|
+
end
|
316
|
+
```
|
317
|
+
|
318
|
+
Now config would read values from the ENV object to the settings. For the example above it would look for keys starting
|
319
|
+
with `Settings`:
|
320
|
+
|
321
|
+
```ruby
|
322
|
+
ENV['Settings.section.size'] = 1
|
323
|
+
ENV['Settings.section.server'] = 'google.com'
|
324
|
+
```
|
325
|
+
|
326
|
+
It won't work with arrays, though.
|
327
|
+
|
328
|
+
### Working with Heroku
|
329
|
+
|
330
|
+
Heroku uses ENV object to store sensitive settings. You cannot upload such files to Heroku because it's ephemeral
|
331
|
+
filesystem gets recreated from the git sources on each instance refresh. To use config with Heroku just set the
|
332
|
+
`use_env` var to `true` as mentioned above.
|
333
|
+
|
334
|
+
To upload your local values to Heroku you could ran `bundle exec rake config:heroku`.
|
335
|
+
|
336
|
+
### Fine-tuning
|
337
|
+
|
338
|
+
You can customize how environment variables are processed:
|
339
|
+
|
340
|
+
* `env_prefix` (default: `SETTINGS`) - which ENV variables to load into config
|
341
|
+
* `env_separator` (default: `.`) - what string to use as level separator - default value of `.` works well with
|
342
|
+
Heroku, but you might want to change it for example for `__` to easy override settings from command line, where using
|
343
|
+
dots in variable names might not be allowed (eg. Bash)
|
344
|
+
* `env_converter` (default: `:downcase`) - how to process variables names:
|
345
|
+
* `nil` - no change
|
346
|
+
* `:downcase` - convert to lower case
|
347
|
+
* `env_parse_values` (default: `true`) - try to parse values to a correct type (`Integer`, `Float`, `String`)
|
348
|
+
|
349
|
+
For instance, given the following environment:
|
350
|
+
|
351
|
+
```bash
|
352
|
+
SETTINGS__SECTION__SERVER_SIZE=1
|
353
|
+
SETTINGS__SECTION__SERVER=google.com
|
354
|
+
```
|
355
|
+
|
356
|
+
And the following configuration:
|
357
|
+
|
358
|
+
```ruby
|
359
|
+
Config.setup do |config|
|
360
|
+
config.use_env = true
|
361
|
+
config.env_prefix = 'Settings'
|
362
|
+
config.env_separator = '__'
|
363
|
+
config.env_converter = :downcase
|
364
|
+
config.env_parse_values = true
|
365
|
+
end
|
366
|
+
```
|
367
|
+
|
368
|
+
The following settings will be available:
|
369
|
+
|
370
|
+
```ruby
|
371
|
+
Settings.section.server_size # => 1
|
372
|
+
Settings.section.server # => 'google.com'
|
373
|
+
```
|
374
|
+
|
375
|
+
## Contributing
|
376
|
+
|
377
|
+
Bootstrap
|
378
|
+
|
379
|
+
```bash
|
380
|
+
appraisal install
|
381
|
+
```
|
382
|
+
|
383
|
+
Run the test suite:
|
384
|
+
|
385
|
+
```bash
|
386
|
+
appraisal rspec
|
387
|
+
```
|
388
|
+
|
389
|
+
If you modified any of the documentation files verify their format:
|
390
|
+
|
391
|
+
```bash
|
392
|
+
mdl --style .mdlstyle.rb *.md
|
393
|
+
```
|
394
|
+
|
395
|
+
## Authors
|
396
|
+
|
397
|
+
* [Piotr Kuczynski](http://github.com/pkuczynski)
|
398
|
+
* [Fred Wu](http://github.com/fredwu)
|
399
|
+
* [Jacques Crocker](http://github.com/railsjedi)
|
400
|
+
* Inherited from [AppConfig](http://github.com/cjbottaro/app_config) by [Christopher J. Bottaro](http://github.com/cjbottaro)
|
401
|
+
|
402
|
+
## License
|
403
|
+
|
404
|
+
Config is released under the [MIT License](http://www.opensource.org/licenses/MIT).
|