0.0
No commit activity in last 3 years
No release in over 3 years
Use environment-specific YAML for configuration while allowing for ENV variables to override
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

Build Status

FlexibleConfig

FlexibleConfig promotes good OOP design, and the separation of logic and configuration in your Ruby classes.

FlexibleConfig allows you to set class constants cleanly in ruby with the heirarchical structure and clean workflow of YML config, without sacrificing the flexibility and immediacy of ENVironment variables.

Here is how your class constants could look like using FlexibleConfig:

module Bidding
  class Calculator
    FlexibleConfig.use 'auction.bidding' do |config|
      BASE_RATE   = config['base_rate']
      TIME_DECAY  = config['time_decay']
      WIGGLE_ROOM = config['wiggle_room']
    end
  end
end

Or the more safer, (but more verbose) syntax:

module Bidding
  class Calculator
    FlexibleConfig.use 'auction.bidding' do |cfg|
      BIDDING_ENABLED = cfg.fetch('enabled') { true }
      BIDDER_EMAIL    = cfg.to_s('email_from')
      BASE_RATE       = cfg.to_f('base_rate') { 1.0 }
      TIME_DECAY      = cfg.to_f('time_decay') { 3.0 }
      ATTEMPTS        = cfg.to_i('wiggle_room') { 2 }
    end
  end
end

Then config as follows:

In YAML:

config/settings/auction.yml

default:
  base_rate: 1.0
  time_decay: 3.0
  wiggle_room: 0.2

development:
  wiggle_room: 0.6

production:
  wiggle_room: 0.2

Using ENV variables to override:

AUCTION_BIDDING_BASE_RATE=1.0
AUCTION_BIDDING_TIME_DECAY=3.0
AUCTION_BIDDING_WIGGLE_ROOM=0.2

Casting Booleans from ENV

If your ENV variable is equal to the string 'true' or 'false' then using the default #fetch method on the config object will cast it to the Ruby TrueClass or FalseClass automatically.

Casting of Other Types

Casting to any Ruby type will work as long as the Ruby object and its string representation are safely interchangable:

example.yml

default:
  safe: '123'
  unsafe: '123oops'
FlexibleConfig.use 'example' do |cfg|
  BASE_RATE  = cfg.to_i('safe') # => 123
  TIME_DECAY = cfg.to_i('unsafe') # => raises UnsafeConversion
end

Viewing actual config variables in an environment

Add this to the Rakefile of your project:

# Load flexible config tasks
spec = Gem::Specification.find_by_name 'flexible-config'
load "#{spec.gem_dir}/lib/tasks/flexible_config.rake"

Type into the command line:

bundle exec rake flexible_config:print

Contributing

If you'd like to become a contributor, the easiest way it to fork this repo, make your changes, run the specs and submit a pull request once they pass.

To run specs, run bundle install && bundle exec rspec

If your changes seem reasonable and the specs pass I'll give you commit rights to this repo and add you to the list of people who can push the gem.

Copyright

Copyright (c) 2015 Grouper. See LICENSE for details.