Skip to content

Commit

Permalink
Add parsing of ENV variable values to Boolean type
Browse files Browse the repository at this point in the history
Fixes #178
  • Loading branch information
mjgiarlo authored and pkuczynski committed Oct 23, 2017
1 parent 3ca8206 commit a2ae5d7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### New features

* Add parsing of ENV variable values to Boolean type

...

## 1.5.0
Expand Down Expand Up @@ -86,6 +90,3 @@

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



4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,14 @@ You can customize how environment variables are processed:
* `env_converter` (default: `:downcase`) - how to process variables names:
* `nil` - no change
* `:downcase` - convert to lower case
* `env_parse_values` (default: `true`) - try to parse values to a correct type (`Integer`, `Float`, `String`)
* `env_parse_values` (default: `true`) - try to parse values to a correct type (`Boolean`, `Integer`, `Float`, `String`)

For instance, given the following environment:

```bash
SETTINGS__SECTION__SERVER_SIZE=1
SETTINGS__SECTION__SERVER=google.com
SETTINGS__SECTION__SSL_ENABLED=false
```

And the following configuration:
Expand All @@ -370,6 +371,7 @@ The following settings will be available:
```ruby
Settings.section.server_size # => 1
Settings.section.server # => 'google.com'
Settings.section.ssl_enabled # => false
```

## Contributing
Expand Down
9 changes: 8 additions & 1 deletion lib/config/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,14 @@ def __convert(h) #:nodoc:

# Try to convert string to a correct type
def __value(v)
Integer(v) rescue Float(v) rescue v
case v
when 'false'
false
when 'true'
true
else
Integer(v) rescue Float(v) rescue v
end
end
end
end
14 changes: 14 additions & 0 deletions spec/config_env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@
end

context 'and parsing ENV variable names is enabled' do
it 'should recognize "false" and expose as Boolean' do
ENV['Settings.new_var'] = 'false'

expect(config.new_var).to eq(false)
expect(config.new_var.is_a? FalseClass).to eq(true)
end

it 'should recognize "true" and expose as Boolean' do
ENV['Settings.new_var'] = 'true'

expect(config.new_var).to eq(true)
expect(config.new_var.is_a? TrueClass).to eq(true)
end

it 'should recognize numbers and expose them as integers' do
ENV['Settings.new_var'] = '123'

Expand Down

0 comments on commit a2ae5d7

Please sign in to comment.