https://github.com/spickermann/has_configuration
Easy configuration management for Ruby classes with yaml file
https://github.com/spickermann/has_configuration
configuration configuration-management gem rails ruby yml
Last synced: 9 months ago
JSON representation
Easy configuration management for Ruby classes with yaml file
- Host: GitHub
- URL: https://github.com/spickermann/has_configuration
- Owner: spickermann
- License: mit
- Created: 2013-10-08T15:47:29.000Z (over 12 years ago)
- Default Branch: main
- Last Pushed: 2025-07-22T13:24:03.000Z (11 months ago)
- Last Synced: 2025-09-28T12:50:09.914Z (9 months ago)
- Topics: configuration, configuration-management, gem, rails, ruby, yml
- Language: Ruby
- Homepage:
- Size: 125 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
Has Configuration
=================
Load configuration settings from a YAML file and adds a class and an instance method `configuration` to an object.
[](https://github.com/spickermann/has_configuration/blob/main/MIT-LICENSE)
[](http://badge.fury.io/rb/has_configuration)
[](https://github.com/spickermann/has_configuration/actions/workflows/CI.yml)
[](https://app.codacy.com/gh/spickermann/has_configuration/dashboard)
[](https://app.codacy.com/gh/spickermann/has_configuration/dashboard)
Installation
------------
Include the gem into your Gemfile:
```ruby
gem 'has_configuration'
```
When you are still on Ruby 1.8 or on Ruby on Rails 2.3:
```ruby
gem 'has_configuration', '~> 0.2.4'
```
When you are still on Ruby 2.4 – 2.7:
```ruby
gem 'has_configuration', '~> 5.0.1'
```
Usage
-----
```ruby
has_configuration
# => loads setting without environment processing from the file #{self.class.name.downcase}.yml
has_configuration file: Rails.root.join('config', 'example.yml'), env: 'staging'
# => loads settings for staging environment from RAILS_ROOT/config/example.yml file
```
**options**
- file
-
The YAML file to load: Defaults toconfig/classname.ymlif Rails is
defined,classname.ymlotherwise. - env
-
The environment to load from the file. Defaults to `Rails.env` if Rails is defined, no default if not.
YAML File Example
-----------------
The YAML file may contain defaults. Nesting is not limited. ERB in the YAML file is evaluated.
```yaml
defaults: &defaults
user: root
some:
nested: value
development:
<<: *defaults
password: secret
production:
<<: *defaults
password: <%= ENV[:secret] %>
```
Configuration Retrieval
-----------------------
If the example above was loaded into a class `Foo` in `production` environment:
```ruby
Foo.configuration # =>
Foo.new.configuration # =>
# convenient getter methods
Foo.configuration.some.nested # => "value"
# to_h returns a HashWithIndifferentAccess
Foo.configuration.to_h # => { :user => "root", :password => "prod-secret"
# :some => { :nested => "value" } }
Foo.configuration.to_h[:some][:nested] # => "value"
Foo.configuration.to_h[:some]['nested'] # => "value"
# force a special key type (when merging with other hashes)
Foo.configuration.to_h(:symbolized) # => { :user => "root", :password => "prod-secret"
# :some => { :nested => "value" } }
Foo.configuration.to_h(:stringify) # => { 'user' => "root", 'password' => "prod-secret"
# 'some' => { 'nested' => "value" } }
```
Contributing
------------
1. [Fork it](http://github.com/spickermann/has_configuration/fork)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new pull request.