{"id":29272082,"url":"https://github.com/acook/config_module","last_synced_at":"2025-07-05T00:10:31.009Z","repository":{"id":6831232,"uuid":"8079543","full_name":"acook/config_module","owner":"acook","description":"Load important configuration files into their own modules","archived":false,"fork":false,"pushed_at":"2023-09-15T05:22:35.000Z","size":134,"stargazers_count":2,"open_issues_count":5,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-06-16T09:16:39.862Z","etag":null,"topics":["configuration-management","ruby","ruby-gem","settings-storage","yaml-configuration"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/acook.png","metadata":{"files":{"readme":"README.markdown","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-02-07T19:10:07.000Z","updated_at":"2023-04-21T13:37:19.000Z","dependencies_parsed_at":"2022-08-26T07:10:32.465Z","dependency_job_id":null,"html_url":"https://github.com/acook/config_module","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/acook/config_module","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acook%2Fconfig_module","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acook%2Fconfig_module/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acook%2Fconfig_module/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acook%2Fconfig_module/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/acook","download_url":"https://codeload.github.com/acook/config_module/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acook%2Fconfig_module/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263636825,"owners_count":23492312,"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":["configuration-management","ruby","ruby-gem","settings-storage","yaml-configuration"],"created_at":"2025-07-05T00:10:30.254Z","updated_at":"2025-07-05T00:10:30.993Z","avatar_url":"https://github.com/acook.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"ConfigModule\n=============\n\nLoad important configuration files into their own modules!\n\nReference documentation for the [Latest Released](http://rubydoc.info/gems/config_module/file/README.markdown) and [Edge Version](https://github.com/acook/config_module#readme) is available.\n\n[![Gem Version](https://img.shields.io/gem/v/config_module.svg?style=for-the-badge)](https://rubygems.org/gems/config_module)\n[![Gem Downloads](https://img.shields.io/gem/dt/config_module.svg?style=for-the-badge)](https://rubygems.org/gems/config_module)\n[![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/acook/config_module/ruby.yml?style=for-the-badge)](https://github.com/acook/config_module/actions/workflows/ruby.yml)\n[![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/acook/config_module?style=for-the-badge)](https://codeclimate.com/github/acook/config_module)\n\nInstallation\n------------\n\nInstall the [gem](http://rubygems.org/gems/config_module), preferably using [Bundler](http://gembundler.com/):\n\n  ```ruby\n  gem 'config_module'     # in your Gemfile\n  ```\n\n  ```bash\n  bundle install          # on the command line\n  ```\n\nYou may need to tell Ruby that you want to use it (depending how you're using Bundler):\n\n  ```ruby\n  require 'config_module' # in your file\n  ```\n\nSetup\n-----\n\nYou only need to add two lines inside any module definition to make it a ConfigModule.\n\n1. Add the ConfigModule functionality to your module:\n\n  ```ruby\n  extend ConfigModule\n  ```\n\n2. Specify the name of your configuration file:\n\n  ```ruby\n  config_file './some_config.yml'\n  ```\n\nDone!\n\nYou're set up, and you can add any other functionality, aliases, or derived values to your module\nlike any other Ruby module.\n\nUsage\n-----\n\nNow give it a try, any [valid](#caveats)\nkey in your configuration file will now be a method:\n\n```ruby\nSomeConfig.my_key\n```\n\nYou can even chain them! Try it:\n\n```ruby\nSomeConfig.my_key.my_subkey\n```\n\nHow cool is that?\n\nExtras\n------\n\nIn addition to the basics, ConfigModule also supplies a couple of helpers you might find useful.\n\n### Namespaces\n\nYou can also set the \"namespace\" you want to use, this is great for apps with different configurations per environment:\n\n```ruby\nnamespace ENV['my_environment']\n```\n\nThis will set the root of the configuration tree to whichever branch you specify, so you don't have to.\n\nDepending on your configuration file's structure, it might be useful to pull out a deeper subtree, in that case you can include multiple keys separated by commas, or even give it an array.\n\nCheck out the [example section](#example) below to see how it's used.\n\n### The `config` Method\n\nThere's also a new method available in your module that points to the root of your configuration data:\n\n```ruby\ndef foo\n  config.foo\nend\n```\n\n### Check for Presence of Configuration Keys with `has_key?`\n\nYou might want to check to see if the key exists (especially useful along with namespaces) before calling the method. Much like a Hash, you can use the `has_key?` method and do something like:\n\n```ruby\nif MyConfig.has_key? :some_option then\n  MyConfig.some_option\nelse\n  'my default value'\nend\n```\n\n### Hash-like Access\n\nYou can access config options like a hash too, if you want:\n\n  ```ruby\n  MyConfig[:some_key].is_a? Hash #=\u003e true\n  ```\n\n  This is useful mainly when you'd rather get a `nil` instead of raising an error for nonexistant keys:\n\n  ```ruby\n  MyConfig[:nonexistant_key] #=\u003e nil\n  MyConfig.nonexistant_key   #=\u003e raises ConfigModule::ConfigOption::NotFoundError\n  ```\n\n  It'll also avoid any naming conflicts that might arise between methods names and key names. You can use it in concert with the above `config` method instead of `self` to enhance readability:\n\n  ```ruby\n  def bar\n    config[:my_key]\n  end\n  ```\n  \n  Lastly, it also doesn't wrap the returned value in a `ConfigOption`, it will return the underlying value such as a `Hash` directly.\n\n\n### Enumerable\n\n  `ConfigOption` is the way ConfigModule packages up subtrees, and unlike `OpenStruct`, it is `Enumerable`:\n\n  ```ruby\n  MyConfig.some_key_with_subkeys.each do |subkey|\n    puts subkey\n  end\n  ```\n\nExample\n-------\n\nGiven a YAML file `./config/example.yml':\n\n```yaml\n---\n:example:\n  :production:\n    :foo: bar\n    :noodle: boom!\n```\n\nAnd you set up your module:\n\n```ruby\nrequire 'config_module'\n\nmodule ExampleConfig\n  extend ConfigModule\n\n  config_file './config/example.yml'\n  namespace :example, Rails.env\n\n  module_function\n\n  def kanoodle\n    'ka' + noodle\n  end\n\n  def all_keys\n    config.map do |key, _|\n      key\n    end\n  end\nend\n```\n\nThen you can use it like this:\n\n```ruby\nExampleConfig.foo       #=\u003e 'bar'\nExampleConfig[:foo]     #=\u003e 'bar'\nExampleConfig[:notakey] #=\u003e nil\nExampleConfig.kanoodle  #=\u003e 'kaboom!'\nExampleConfig.all_keys  #=\u003e [:foo, :noodle]\n```\n\nPretty nifty, huh?\n\nCaveats\n-------\n\n- **Q:** You mention \"valid key\". What's a valid key?\n- **A:** It's any object that you can call `.to_sym` on (same as `OpenStruct`)!\n\nWho made this anyway?\n---------------------\n\n    © 2016-2019 Anthony M. Cook\n    Contributors: Brian Hawley\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Facook%2Fconfig_module","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Facook%2Fconfig_module","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Facook%2Fconfig_module/lists"}