Skip to content

Commit

Permalink
[load_from_json] changelog updates, readme updates, code style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rustam Ibragimov committed Jul 26, 2018
1 parent dded003 commit 0202454
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
All notable changes to this project will be documented in this file.

## [Unreleased]
## Changed
### Added
- `load_from_json`- a command that provides an ability to define config settings
by loading them from a json file (in `load_from_yaml` manner);

### Changed
- Support for Ruby 2.2 has ended;

## [0.4.0] - 2018-06-24
Expand Down
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require 'qonfig'
- [State freeze](#state-freeze)
- [Settings as Predicates](#settings-as-predicates)
- [Load from YAML file](#load-from-yaml-file)
- [Load from JSON file](#load-from-json-file)
- [Load from ENV](#load-from-env)
- [Load from \_\_END\_\_](#load-from-__end__) (aka `load_from_self`)
- [Smart Mixin](#smart-mixin) (`Qonfig::Configurable`)
Expand Down Expand Up @@ -482,6 +483,65 @@ Config.new.to_h # => { "nonexistent_yaml" => {}, "another_key" => nil }

---

### Load from JSON file

- `:strict` mode (fail behaviour when the required yaml file doesnt exist):
- `true` (by default) - causes `Qonfig::FileNotFoundError`;
- `false` - do nothing, ignore current command;

```json
<!-- options.json -->
{
"user": "0exp",
"password": 12345,
"rubySettings": {
"allowedVersions": ["2.3", "2.4.2", "1.9.8"],
"gitLink": null,
"withAdditionals": false
}
}
```

```ruby
class Config < Qonfig::DataSet
load_from_json 'options.json'
end

config = Config.new

config.settings.user # => '0exp'
config.settings.password # => 12345
config.settings.rubySettings.allowedVersions # => ['2.3', '2.4.2', '1.9.8']
config.settings.rubySettings.gitLink # => nil
config.settings.rubySettings.withAdditionals # => false
```

```ruby
# --- strict mode ---
class Config < Qonfig::DataSet
setting :nonexistent_json do
load_from_yaml 'nonexistent_json.json', strict: true # true by default
end

setting :another_key
end

Config.new # => Qonfig::FileNotFoundError

# --- non-strict mode ---
class Config < Qonfig::DataSet
settings :nonexistent_json do
load_from_yaml 'nonexistent_json.json', strict: false
end

setting :another_key
end

Config.new.to_h # => { "nonexistent_json" => {}, "another_key" => nil }
```

---

### Load from ENV

- `:convert_values` (`false` by default):
Expand Down
2 changes: 1 addition & 1 deletion spec/features/load_from_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class NonFailingJSONConfig < Qonfig::DataSet
end

expect { NonFailingJSONConfig.new }.not_to raise_error
expect(NonFailingJSONConfig.new.to_h).to eq({ 'nested' => {} })
expect(NonFailingJSONConfig.new.to_h).to eq('nested' => {})
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/features/load_from_yaml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class NonFailingYAMLConfig < Qonfig::DataSet
end

expect { NonFailingYAMLConfig.new }.not_to raise_error
expect(NonFailingYAMLConfig.new.to_h).to eq({ 'nested' => {} })
expect(NonFailingYAMLConfig.new.to_h).to eq('nested' => {})
end
end
end
Expand Down

0 comments on commit 0202454

Please sign in to comment.