Skip to content

Commit a2ae5d7

Browse files
mjgiarlopkuczynski
authored andcommitted
Add parsing of ENV variable values to Boolean type
Fixes #178
1 parent 3ca8206 commit a2ae5d7

4 files changed

Lines changed: 29 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### New features
6+
7+
* Add parsing of ENV variable values to Boolean type
8+
59
...
610

711
## 1.5.0
@@ -86,6 +90,3 @@
8690

8791
* 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))
8892
* Adding support for Rails 4.1.0.rc ([#70](https://github.com/railsjedi/config/issues/70))
89-
90-
91-

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,14 @@ You can customize how environment variables are processed:
344344
* `env_converter` (default: `:downcase`) - how to process variables names:
345345
* `nil` - no change
346346
* `:downcase` - convert to lower case
347-
* `env_parse_values` (default: `true`) - try to parse values to a correct type (`Integer`, `Float`, `String`)
347+
* `env_parse_values` (default: `true`) - try to parse values to a correct type (`Boolean`, `Integer`, `Float`, `String`)
348348

349349
For instance, given the following environment:
350350

351351
```bash
352352
SETTINGS__SECTION__SERVER_SIZE=1
353353
SETTINGS__SECTION__SERVER=google.com
354+
SETTINGS__SECTION__SSL_ENABLED=false
354355
```
355356

356357
And the following configuration:
@@ -370,6 +371,7 @@ The following settings will be available:
370371
```ruby
371372
Settings.section.server_size # => 1
372373
Settings.section.server # => 'google.com'
374+
Settings.section.ssl_enabled # => false
373375
```
374376

375377
## Contributing

lib/config/options.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,14 @@ def __convert(h) #:nodoc:
188188

189189
# Try to convert string to a correct type
190190
def __value(v)
191-
Integer(v) rescue Float(v) rescue v
191+
case v
192+
when 'false'
193+
false
194+
when 'true'
195+
true
196+
else
197+
Integer(v) rescue Float(v) rescue v
198+
end
192199
end
193200
end
194201
end

spec/config_env_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@
4848
end
4949

5050
context 'and parsing ENV variable names is enabled' do
51+
it 'should recognize "false" and expose as Boolean' do
52+
ENV['Settings.new_var'] = 'false'
53+
54+
expect(config.new_var).to eq(false)
55+
expect(config.new_var.is_a? FalseClass).to eq(true)
56+
end
57+
58+
it 'should recognize "true" and expose as Boolean' do
59+
ENV['Settings.new_var'] = 'true'
60+
61+
expect(config.new_var).to eq(true)
62+
expect(config.new_var.is_a? TrueClass).to eq(true)
63+
end
64+
5165
it 'should recognize numbers and expose them as integers' do
5266
ENV['Settings.new_var'] = '123'
5367

0 commit comments

Comments
 (0)