Flexible Config

Ruby configuration management  

https://github.com/brett-richardson/flexible-config

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
comments powered by Disqus