README.md in anyway_config-2.0.6 vs README.md in anyway_config-2.1.0

- old
+ new

@@ -327,43 +327,87 @@ ### Data population Your config is filled up with values from the following sources (ordered by priority from low to high): -- `RAILS_ROOT/config/my_cool_gem.yml` (for the current `RAILS_ENV`, supports `ERB`): +1) **YAML configuration files**: `RAILS_ROOT/config/my_cool_gem.yml`. +Recognizes Rails environment, supports `ERB`: + ```yml test: host: localhost port: 3002 development: host: localhost port: 3000 ``` -**NOTE:** you can override the default YML lookup path by setting `MYCOOLGEM_CONF` env variable. +### Multi-env configuration -- `Rails.application.secrets.my_cool_gem` (if `secrets.yml` present): +_⚡️ This feature will be turned on by default in the future releases. You can turn it on now via `config.anyway_config.future.use :unwrap_known_environments`._ +If the YML does not have keys that are one of the "known" Rails environments (development, production, test)—the same configuration will be available in all environments, similar to non-Rails behavior: + ```yml +host: localhost +port: 3002 +# These values will be active in all environments +``` + +To extend the list of known environments, use the setting in the relevant part of your Rails code: + +```ruby +Rails.application.config.anyway_config.known_environments << "staging" +``` + +If your YML defines at least a single "environmental" top-level, you _have_ to separate all your settings per-environment. You can't mix and match: + +```yml +staging: + host: localhost # This value will be loaded when Rails.env.staging? is true + +port: 3002 # This value will not be loaded at all +``` + +You can specify the lookup path for YAML files in one of the following ways: + +- By setting `config.anyway_config.default_config_path` to a target directory path: + +```ruby +config.anyway_config.default_config_path = "/etc/configs" +config.anyway_config.default_config_path = Rails.root.join("etc", "configs") +``` + +- By setting `config.anyway_config.default_config_path` to a Proc, which accepts a config name and returns the path: + +```ruby +config.anyway_config.default_config_path = ->(name) { Rails.root.join("data", "configs", "#{name}.yml") } +``` + +- By overriding a specific config YML file path via the `<NAME>_CONF` env variable, e.g., `MYCOOLGEM_CONF=path/to/cool.yml` + +2) **Rails secrets**: `Rails.application.secrets.my_cool_gem` (if `secrets.yml` present). + +```yml # config/secrets.yml development: my_cool_gem: port: 4444 ``` -- `Rails.application.credentials.my_cool_gem` (if supported): +3) **Rails credentials**: `Rails.application.credentials.my_cool_gem` (if supported): ```yml my_cool_gem: host: secret.host ``` **NOTE:** You can backport Rails 6 per-environment credentials to Rails 5.2 app using [this patch](https://gist.github.com/palkan/e27e4885535ff25753aefce45378e0cb). -- `ENV['MYCOOLGEM_*']`. +4) **Environment variables**: `ENV['MYCOOLGEM_*']`. See [environment variables](#environment-variables). ### Organizing configs @@ -442,23 +486,39 @@ ## Using with Ruby The default data loading mechanism for non-Rails applications is the following (ordered by priority from low to high): -- `./config/<config-name>.yml` (`ERB` is supported if `erb` is loaded) +1) **YAML configuration files**: `./config/<config-name>.yml`. In pure Ruby apps, we do not know about _environments_ (`test`, `development`, `production`, etc.); thus, we assume that the YAML contains values for a single environment: ```yml host: localhost port: 3000 ``` -**NOTE:** you can override the default YML lookup path by setting `MYCOOLGEM_CONF` env variable. +`ERB` is supported if `erb` is loaded (thus, you need to call `require "erb"` somewhere before loading configuration). -- `ENV['MYCOOLGEM_*']`. +You can specify the lookup path for YAML files in one of the following ways: +- By setting `Anyway::Settings.default_config_path` to a target directory path: + +```ruby +Anyway::Settings.default_config_path = "/etc/configs" +``` + +- By setting `Anyway::Settings.default_config_path` to a Proc, which accepts a config name and returns the path: + +```ruby +Anyway::Settings.default_config_path = ->(name) { Rails.root.join("data", "configs", "#{name}.yml") } +``` + +- By overriding a specific config YML file path via the `<NAME>_CONF` env variable, e.g., `MYCOOLGEM_CONF=path/to/cool.yml` + +2) **Environment variables**: `ENV['MYCOOLGEM_*']`. + See [environment variables](#environment-variables). ## Environment variables Environmental variables for your config should start with your config name, upper-cased. @@ -664,10 +724,10 @@ If you want to delete the env var, pass `nil` as the value. This helper is automatically included to RSpec if `RAILS_ENV` or `RACK_ENV` env variable is equal to "test". It's only available for the example with the tag `type: :config` or with the path `spec/configs/...`. -You can add it manually by requiring `"anyway/testing/helpers"` and including the `Anyway::Test::Helpers` module (into RSpec configuration or Minitest test class). +You can add it manually by requiring `"anyway/testing/helpers"` and including the `Anyway::Testing::Helpers` module (into RSpec configuration or Minitest test class). ## OptionParser integration It's possible to use config as option parser (e.g., for CLI apps/libraries). It uses [`optparse`](https://ruby-doc.org/stdlib-2.5.1/libdoc/optparse/rdoc/OptionParser.html) under the hood.