{"id":14955932,"url":"https://github.com/dcotecnologia/confset","last_synced_at":"2025-09-29T23:32:06.170Z","repository":{"id":38295985,"uuid":"496055523","full_name":"dcotecnologia/confset","owner":"dcotecnologia","description":"Easiest way to add multi-environment yaml settings to Rails, Sinatra, Pandrino and other Ruby projects.","archived":false,"fork":true,"pushed_at":"2024-04-29T16:23:35.000Z","size":632,"stargazers_count":1,"open_issues_count":2,"forks_count":0,"subscribers_count":0,"default_branch":"dev","last_synced_at":"2024-04-29T17:41:16.181Z","etag":null,"topics":["config","helper","library","rails","ruby","settings","sinatra"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"rubyconfig/config","license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dcotecnologia.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2022-05-25T02:28:59.000Z","updated_at":"2024-04-29T17:41:16.182Z","dependencies_parsed_at":"2023-10-14T04:04:48.928Z","dependency_job_id":null,"html_url":"https://github.com/dcotecnologia/confset","commit_stats":{"total_commits":503,"total_committers":89,"mean_commits":5.651685393258427,"dds":0.679920477137177,"last_synced_commit":"405e9f496fd71db1330ae43c126c450d222750da"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcotecnologia%2Fconfset","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcotecnologia%2Fconfset/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcotecnologia%2Fconfset/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dcotecnologia%2Fconfset/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dcotecnologia","download_url":"https://codeload.github.com/dcotecnologia/confset/tar.gz/refs/heads/dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234673608,"owners_count":18869698,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["config","helper","library","rails","ruby","settings","sinatra"],"created_at":"2024-09-24T13:12:02.487Z","updated_at":"2025-09-29T23:32:00.811Z","avatar_url":"https://github.com/dcotecnologia.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Confset\n\n[![Gem Version](https://badge.fury.io/rb/confset.svg)](https://badge.fury.io/rb/confset)\n[![Downloads Total](https://img.shields.io/gem/dt/confset)](https://rubygems.org/gems/confset)\n[![Build](https://github.com/dcotecnologia/confset/actions/workflows/tests.yml/badge.svg)](https://github.com/dcotecnologia/confset/actions/workflows/tests.yml)\n[![CodeQL](https://github.com/dcotecnologia/confset/actions/workflows/codeql.yml/badge.svg)](https://github.com/dcotecnologia/confset/actions/workflows/codeql.yml)\n[![Release](https://github.com/dcotecnologia/confset/actions/workflows/release.yml/badge.svg?branch=master)](https://github.com/dcotecnologia/confset/actions/workflows/release.yml)\n\n## Summary\n\nConfset helps you easily manage environment specific settings in an easy and\nusable manner.\n\nThis project is a fork of \u003chttps://github.com/rubyconfig/config\u003e.\n\n## Features\n\n* simple YAML config files\n* config files support ERB\n* config files support inheritance and multiple environments\n* access config information via convenient object member notation\n* support for multi-level settings (`Settings.group.subgroup.setting`)\n* local developer settings ignored when committing the code\n\n## Compatibility\n\nCurrent version supports and is [tested](.github/workflows/build.yml#L19)\nfor the following interpreters and frameworks:\n\n* Interpreters\n  * [Ruby](https://www.ruby-lang.org) `\u003e= 2.7`\n  * Tested versions: 2.7.x, 3.0.x, 3.1.x and 3.2.x\n* Application frameworks\n  * Rails `\u003e= 6.0`\n  * Padrino\n  * Sinatra\n\n## Installing\n\nAdd the gem to your `Gemfile` and run `bundle install` to install it:\n\n```ruby\ngem \"confset\", \"~\u003e 1.0.3\"\n```\n\nYou can also install by the GitHub packages server:\n\n```ruby\nsource \"https://rubygems.pkg.github.com/dcotecnologia\" do\n  gem \"confset\", \"1.0.3\"\nend\n```\n\nOr directly using the repository version:\n\n```ruby\ngem \"confset\", github: \"dcotecnologia/confset\", branch: \"master\"\n```\n\n### Installing on Rails\n\nThen run\n\n    rails g confset:install\n\nwhich will generate customizable config file `config/initializers/confset.rb` and set of default settings files:\n\n    config/settings.yml\n    config/settings.local.yml\n    config/settings/development.yml\n    config/settings/production.yml\n    config/settings/test.yml\n\nYou can now edit them to adjust to your needs.\n\n### Installing on Padrino\n\nThen edit `app.rb` and register `Confset`\n\n```ruby\nregister Confset\n```\n\n### Installing on Sinatra\n\nAfterwards in need to register `Confset` in your app and give it a root so\nit can find the config files.\n\n```ruby\nset :root, File.dirname(__FILE__)\nregister Confset\n```\n\n### Installing on other ruby projects\n\nAdd the gem to your `Gemfile` and run `bundle install` to install it.\nThen initialize `Confset` manually within your configure block.\n\n```ruby\nConfset.load_and_set_settings(Confset.setting_files(\"/path/to/config_root\", \"your_project_environment\"))\n```\n\nIt's also possible to initialize `Confset` manually within your configure block\nif you want to just give it some yml paths to load from.\n\n```ruby\nConfset.load_and_set_settings(\"/path/to/yaml1\", \"/path/to/yaml2\", ...)\n```\n\n## Accessing the Settings object\n\nAfter installing the gem, `Settings` object will become available globally and\nby default will be compiled from the files listed below. Settings defined in\nfiles that are lower in the list override settings higher.\n\n    config/settings.yml\n    config/settings/#{environment}.yml\n    config/environments/#{environment}.yml\n\n    config/settings.local.yml\n    config/settings/#{environment}.local.yml\n    config/environments/#{environment}.local.yml\n\nEntries can be accessed via object member notation:\n\n```ruby\nSettings.my_config_entry\n```\n\nNested entries are supported:\n\n```ruby\nSettings.my_section.some_entry\n```\n\nAlternatively, you can also use the `[]` operator if you don't know which exact\nsetting you need to access ahead of time.\n\n```ruby\n# All the following are equivalent to Settings.my_section.some_entry\nSettings.my_section[:some_entry]\nSettings.my_section['some_entry']\nSettings[:my_section][:some_entry]\n```\n\n### Reloading settings\n\nYou can reload the Settings object at any time by running `Settings.reload!`.\n\n### Reloading settings and config files\n\nYou can also reload the `Settings` object from different config files at runtime.\n\nFor example, in your tests if you want to test the production settings, you can:\n\n```ruby\nRails.env = \"production\"\nSettings.reload_from_files(\n  Rails.root.join(\"config\", \"settings.yml\").to_s,\n  Rails.root.join(\"config\", \"settings\", \"#{Rails.env}.yml\").to_s,\n  Rails.root.join(\"config\", \"environments\", \"#{Rails.env}.yml\").to_s\n)\n```\n\n### Environment specific config files\n\nYou can have environment specific config files. Environment specific config\nentries take precedence over common config entries.\n\nExample development environment config file:\n\n```ruby\n#{Rails.root}/config/environments/development.yml\n```\n\nExample production environment config file:\n\n```ruby\n#{Rails.root}/config/environments/production.yml\n```\n\n### Developer specific config files\n\nIf you want to have local settings, specific to your machine or development\nenvironment, you can use the following files, which are automatically `.gitignore` :\n\n```ruby\nRails.root.join(\"config\", \"settings.local.yml\").to_s,\nRails.root.join(\"config\", \"settings\", \"#{Rails.env}.local.yml\").to_s,\nRails.root.join(\"config\", \"environments\", \"#{Rails.env}.local.yml\").to_s\n```\n\n**NOTE:** The file `settings.local.yml` will not be loaded in tests to prevent\nlocal configuration from causing flaky or non-deterministic tests.\nEnvironment-specific files (e.g. `settings/test.local.yml`) will still be loaded\nto allow test-specific credentials.\n\n### Adding sources at runtime\n\nYou can add new YAML config files at runtime. Just use:\n\n```ruby\nSettings.add_source!(\"/path/to/source.yml\")\nSettings.reload!\n```\n\nThis will use the given source.yml file and use its settings to overwrite any previous ones.\n\nOn the other hand, you can prepend a YML file to the list of configuration files:\n\n```ruby\nSettings.prepend_source!(\"/path/to/source.yml\")\nSettings.reload!\n```\n\nThis will do the same as `add_source`, but the given YML file will be loaded\nfirst (instead of last) and its settings will be overwritten by any other\nconfiguration file. This is especially useful if you want to define defaults.\n\nOne thing I like to do for my Rails projects is provide a local.yml config file\nthat is .gitignored (so its independent per developer). Then I create a new\ninitializer in `config/initializers/add_local_confset.rb` with the contents\n\n```ruby\nSettings.add_source!(\"#{Rails.root}/config/settings/local.yml\")\nSettings.reload!\n```\n\n\u003e Note: this is an example usage, it is easier to just use the default local\n\u003e files `settings.local.yml`, `settings/#{Rails.env}.local.yml` and\n\u003e `environments/#{Rails.env}.local.yml` for your developer specific settings.\n\nYou also have the option to add a raw hash as a source. One use case might be\nstoring settings in the database or in environment variables that overwrite what\nis in the YML files.\n\n```ruby\nSettings.add_source!({some_secret: ENV['some_secret']})\nSettings.reload!\n```\n\nYou may pass a hash to `prepend_source!` as well.\n\n## Embedded Ruby (ERB)\n\nEmbedded Ruby is allowed in the YAML configuration files. ERB will be evaluated\nat load time by default, and when the `evaluate_erb_in_yaml` configuration\nis set to `true`.\n\nConsider the two following config files.\n\n* ```#{Rails.root}/config/settings.yml```\n\n```yaml\nsize: 1\nserver: google.com\n```\n\n* ```#{Rails.root}/config/environments/development.yml```\n\n```yaml\nsize: 2\ncomputed: \u003c%= 1 + 2 + 3 %\u003e\nsection:\n  size: 3\n  servers: [ {name: yahoo.com}, {name: amazon.com} ]\n```\n\nNotice that the environment specific config entries overwrite the common entries.\n\n```ruby\nSettings.size   # =\u003e 2\nSettings.server # =\u003e google.com\n```\n\nNotice the embedded Ruby.\n\n```ruby\nSettings.computed # =\u003e 6\n```\n\nNotice that object member notation is maintained even in nested entries.\n\n```ruby\nSettings.section.size # =\u003e 3\n```\n\nNotice array notation and object member notation is maintained.\n\n```ruby\nSettings.section.servers[0].name # =\u003e yahoo.com\nSettings.section.servers[1].name # =\u003e amazon.com\n```\n\n## Configuration\n\nThere are multiple configuration options available, however you can customize\n`Confset` only once, preferably during application initialization phase:\n\n```ruby\nConfset.setup do |config|\n  config.const_name = 'Settings'\n  # ...\nend\n```\n\nAfter installing `Confset` in Rails, you will find automatically generated file\nthat contains default configuration located at `config/initializers/confset.rb`.\n\n### General\n\n* `const_name` - name of the object holing you settings. Default: `'Settings'`\n* `evaluate_erb_in_yaml` - evaluate ERB in YAML config files. Set to false if\nthe config file contains ERB that should not be evaluated at load time. Default: `true`\n\n### Merge customization\n\n* `overwrite_arrays` - overwrite arrays found in previously loaded settings file.\nDefault: `true`\n* `merge_hash_arrays` - merge hashes inside of arrays from previously loaded\nsettings files. Makes sense only when `overwrite_arrays = false`. Default: `false`\n* `knockout_prefix` - ability to remove elements of the array set in earlier\nloaded settings file. Makes sense only when `overwrite_arrays = false`, otherwise\narray settings would be overwritten by default. Default: `nil`\n* `merge_nil_values` - `nil` values will overwrite an existing value when\nmerging configs. Default: `true`.\n\n```ruby\n# merge_nil_values is true by default\nc = Confset.load_files(\"./spec/fixtures/development.yml\") # =\u003e #\u003cConfset::Options size=2, ...\u003e\nc.size # =\u003e 2\nc.merge!(size: nil) =\u003e #\u003cConfset::Options size=nil, ...\u003e\nc.size # =\u003e nil\n```\n\n```ruby\n# To reject nil values when merging settings:\nConfset.setup do |config|\n  config.merge_nil_values = false\nend\n\nc = Confset.load_files(\"./spec/fixtures/development.yml\") # =\u003e #\u003cConfset::Options size=2, ...\u003e\nc.size # =\u003e 2\nc.merge!(size: nil) =\u003e #\u003cConfset::Options size=nil, ...\u003e\nc.size # =\u003e 2\n```\n\nCheck [Deep Merge](https://github.com/danielsdeleo/deep_merge) for more details.\n\n### Validation\n\nWith Ruby 2.1 or newer, you can optionally define a [schema](https://github.com/dry-rb/dry-schema)\nor [contract](https://github.com/dry-rb/dry-validation) (added in `config-2.1`)\nusing [dry-rb](https://github.com/dry-rb) to validate presence (and type) of\nspecific config values. Generally speaking contracts allow to describe more\ncomplex validations with depencecies between fields.\n\nIf you provide either validation option (or both) it will\nautomatically be used to validate your config. If validation fails it will\nraise a `Confset::Validation::Error` containing information about all\nthe mismatches between the schema and your config.\n\nBoth examples below demonstrates how to ensure that the configuration\nhas an optional `email` and the `youtube` structure with the `api_key` field filled.\nThe contract adds an additional rule.\n\n#### Contract\n\nLeverage dry-validation, you can create a contract with a params schema and rules:\n\n```ruby\nclass ConfigContract \u003c Dry::Validation::Contract\n  params do\n    optional(:email).maybe(:str?)\n\n    required(:youtube).schema do\n      required(:api_key).filled\n    end\n  end\n\n  rule(:email) do\n    unless /\\A[\\w+\\-.]+@[a-z\\d\\-]+(\\.[a-z\\d\\-]+)*\\.[a-z]+\\z/i.match?(value)\n      key.failure('has invalid format')\n    end\n  end\nend\n\nConfset.setup do |config|\n  config.validation_contract = ConfigContract.new\nend\n```\n\nThe above example adds a rule to ensure the `email` is valid by matching\nit against the provided regular expression.\n\nCheck [dry-validation](https://github.com/dry-rb/dry-validation) for more details.\n\n#### Schema\n\nYou may also specify a schema using [dry-schema](https://github.com/dry-rb/dry-schema):\n\n```ruby\nConfset.setup do |config|\n  # ...\n  config.schema do\n    optional(:email).maybe(:str?)\n\n    required(:youtube).schema do\n      required(:api_key).filled\n    end\n  end\nend\n```\n\nCheck [dry-schema](https://github.com/dry-rb/dry-schema) for more details.\n\n### Missing keys\n\nFor an example settings file:\n\n```yaml\nsize: 1\nserver: google.com\n```\n\nYou can test if a value was set for a given key using `key?` and its alias `has_key?`:\n\n```ruby\nSettings.key?(:path)\n# =\u003e false\nSettings.key?(:server)\n# =\u003e true\n```\n\nBy default, accessing to a missing key returns `nil`:\n\n```ruby\nSettings.key?(:path)\n# =\u003e false\nSettings.path\n# =\u003e nil\n```\n\nThis is not \"typo-safe\". To solve this problem, you can configure\nthe `fail_on_missing` option:\n\n```ruby\nConfset.setup do |config|\n  config.fail_on_missing = true\n  # ...\nend\n```\n\nSo it will raise a `KeyError` when accessing a non-existing key\n(similar to `Hash#fetch` behaviour):\n\n```ruby\nSettings.path\n# =\u003e raises KeyError: key not found: :path\n```\n\n### Environment variables\n\nSee section below for more details.\n\n## Working with environment variables\n\nTo load environment variables from the `ENV` object, that will override any settings\ndefined in files, set the `use_env` to true in your\n`config/initializers/confset.rb` file:\n\n```ruby\nConfset.setup do |config|\n  config.const_name = 'Settings'\n  config.use_env = true\nend\n```\n\nNow config would read values from the ENV object to the settings. For the\nexample above it would look for keys starting with `Settings`:\n\n```ruby\nENV['Settings.section.size'] = 1\nENV['Settings.section.server'] = 'google.com'\n```\n\nIt won't work with arrays, though.\n\nIt is considered an error to use environment variables to simutaneously assign\na \"flat\" value and a multi-level value to a key.\n\n```ruby\n# Raises an error when settings are loaded\nENV['BACKEND_DATABASE'] = 'development'\nENV['BACKEND_DATABASE_USER'] = 'postgres'\n```\n\nInstead, specify keys of equal depth in the environment variable names:\n\n```ruby\nENV['BACKEND_DATABASE_NAME'] = 'development'\nENV['BACKEND_DATABASE_USER'] = 'postgres'\n```\n\n### Working with Heroku\n\nHeroku uses ENV object to store sensitive settings.\nYou cannot upload such files to Heroku because it's ephemeral filesystem gets\nrecreated from the git sources on each instance refresh. To use config with Heroku\njust set the `use_env` var to `true` as mentioned above.\n\nTo upload your local values to Heroku you could ran `bundle exec rake config:heroku`.\n\n### Fine-tuning\n\nYou can customize how environment variables are processed:\n\n* `env_prefix` (default: `const_name`) - load only ENV variables starting with\nthis prefix (case-sensitive)\n* `env_separator` (default: `'.'`)  - what string to use as level separator -\ndefault value of `.` works well with   Heroku, but you might want to change it\nfor example for `__` to easy override settings from command line, where using dots\nin variable names might not be allowed (eg. Bash)\n* `env_converter` (default: `:downcase`)  - how to process variables names:\n  * `nil` - no change\n  * `:downcase` - convert to lower case\n* `env_parse_values` (default: `true`) - try to parse values to a correct type (`Boolean`, `Integer`, `Float`, `String`)\n\nFor instance, given the following environment:\n\n```bash\nSETTINGS__SECTION__SERVER_SIZE=1\nSETTINGS__SECTION__SERVER=google.com\nSETTINGS__SECTION__SSL_ENABLED=false\n```\n\nAnd the following configuration:\n\n```ruby\nConfset.setup do |config|\n  config.use_env = true\n  config.env_prefix = 'SETTINGS'\n  config.env_separator = '__'\n  config.env_converter = :downcase\n  config.env_parse_values = true\nend\n```\n\nThe following settings will be available:\n\n```ruby\nSettings.section.server_size # =\u003e 1\nSettings.section.server # =\u003e 'google.com'\nSettings.section.ssl_enabled # =\u003e false\n```\n\n### Working with AWS Secrets Manager\n\nIt is possible to parse variables stored in an AWS Secrets Manager Secret as if\nthey were environment variables by using `Confset::Sources::EnvSource`.\n\nFor example, the plaintext secret might look like this:\n\n```json\n{\n  \"Settings.foo\": \"hello\",\n  \"Settings.bar\": \"world\",\n}\n```\n\nIn order to load those settings, fetch the settings from AWS Secrets Manager,\nparse the plaintext as JSON, pass the resulting `Hash` into a new `EnvSource`,\nload the new source, and reload.\n\n```ruby\n# fetch secrets from AWS\nclient = Aws::SecretsManager::Client.new\nresponse = client.get_secret_value(secret_id: \"#{ENV['ENVIRONMENT']}/my_application\")\nsecrets = JSON.parse(response.secret_string)\n\n# load secrets into config\nsecret_source = Confset::Sources::EnvSource.new(secrets)\nSettings.add_source!(secret_source)\nSettings.reload!\n```\n\nIn this case, the following settings will be available:\n\n```ruby\nSettings.foo # =\u003e \"hello\"\nSettings.bar # =\u003e \"world\"\n```\n\nBy default, `EnvSource` will use configuration for `env_prefix`, `env_separator`,\n`env_converter`, and `env_parse_values`, but any of these can be overridden in\nthe constructor.\n\n```ruby\nsecret_source = Confset::Sources::EnvSource.new(secrets,\n                                               prefix: 'MyConfig',\n                                               separator: '__',\n                                               converter: nil,\n                                               parse_values: false)\n```\n\n## Contributing\n\nAny and all contributions offered in any form, past present or future are\nunderstood to be in complete agreement and acceptance with [MIT](LICENSE) license.\n\n### Running specs\n\nSetup\n\n```sh\nbundle install\n```\n\nRun lint:\n\n```sh\nbundle exec rubocop --parallel\n```\n\nRun specs:\n\n```sh\nbundle exec rspec\n```\n\n## Authors\n\n* [Danilo Carolino](@danilogco) - [DCO Tecnologia](https://github.com/dcotecnologia)\n\n## Contributors\n\n* [Piotr Kuczynski](@pkuczynski) - original config gem project\n* [Fred Wu](@fredwu) - original config gem project\n* [Jacques Crocker](@railsjedi) - original config gem project\n* Inherited from [AppConfig](https://github.com/cjbottaro/app_config) by\n[Christopher J. Bottaro](@cjbottaro)\n\n### Code Contributors\n\nAny and all contributions offered in any form, past present or future are\nunderstood to be in complete agreement and acceptance with the [MIT](LICENSE) license.\n\n## License\n\nCopyright (c) 2022 DCO Tecnologia. Released under the [MIT License](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcotecnologia%2Fconfset","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdcotecnologia%2Fconfset","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdcotecnologia%2Fconfset/lists"}