No release in over a year
Loads configuration setting from a yml file and adds a configuation method to class and instances
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

 Project Readme

Has Configuration

Load configuration settings from a YAML file and adds a class and an instance method configuration to an object.

License MIT Gem Version Build Status Codacy Badge Codacy Badge

Installation

Include the gem into your Gemfile:

gem 'has_configuration'

When you are still on Ruby 1.8 or on Ruby on Rails 2.3:

gem 'has_configuration', '~> 0.2.4'

When you are still on Ruby 2.4 – 2.7:

gem 'has_configuration', '~> 5.0.1'

Usage

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 to config/classname.yml if Rails is defined, classname.yml otherwise.
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.

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:

Foo.configuration                         # => <HasConfiguration::Configuration:0x00...>
Foo.new.configuration                     # => <HasConfiguration::Configuration:0x00...>

# 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
  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.