ConfigX

ConfigX provides you with a simple DSL to configure classes, modules and objects. The interface is kind of like attr accessors and class attr accessors on steroids. It comes with a few useful features.

  1. Default values
  2. Attr accessors
  3. Memoization of values
  4. Inheritance (You can also access the config of a class in their instances)
  5. Blocks can be evaluated within your configurable
  6. You can nest your configs
  7. You can clone your configurables. When you do that blocks will be evaluated in the cloned object.
  8. You can call to_h on a config to get your config values in a hash

The configurable method builds the interface for your configuration at parse time. All necessary methods are defined into modules that are then used to extend your classes and objects before you actually use them in your application.

Installation

Add this line to your application's Gemfile:

gem 'config_x'

And then execute:

$ bundle

Or install it yourself as:

$ gem install config_x

Usage

Configure on class level with configurable :class

class Client
  extend ConfigX

  configurable :class do
    attr :env, accessor: true do
      attr :url, default: 'www.example.com'
      attr :api_key, memoize: true 
    end
  end
end

Client.config.env.api_key { SecureRandom.hex }

p Client.config.env.api_key # 2594c07670bd16dfdd48f9874d190f80
p Client.config.env.url # www.example.com"
p Client.config.to_h # {:env=>{:url=>"www.example.com", :api_key=>"2594c07670bd16dfdd48f9874d190f80"}}

Configure on an instance level with configurable :instance

class Client
  extend ConfigX

  configurable :instance do
    attr :env, accessor: true do
      attr :url, default: 'www.example.com'
      attr :api_key, memoize: true
      attr :timeout, instance_exec: true
    end
  end

  def timeout 
    5
  end
end

client = Client.new  
client.config.env.api_key { SecureRandom.hex }
client.config.env.timeout { timeout } # or client.config.env.timeout { |client| client.timeout } 

p client.config.env.api_key # 2594c07670bd16dfdd48f9874d190f80
p client.config.env.url # www.example.com"
p client.config.env.timeout # 5
p client.config.to_h # {:env=>{:url=>"www.example.com", :api_key=>"2594c07670bd16dfdd48f9874d190f80", :timeout=>5}}

configure & configure!

You can use the configuremethod to assign values and configure! will freeze the underlying hash so that values are safe from being overwritten. Blocks cannot be memoized when you use configure!

client = Client.new
client.configure! do |config|
  config.env.api_key = '123123'
  config.env.timeout = 5
end

For further examples just have a look at the specs.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Andreas Robecke/config_x.