Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Beautiful configurations for your projects and gems

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .document
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .rspec
Octocat-spinner-32 Gemfile
Octocat-spinner-32 Gemfile.lock
Octocat-spinner-32 LICENSE.txt
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 VERSION
Octocat-spinner-32 config_fun.gemspec
README.md

ConfigFun: Beautiful configurations for your projects and gems

http://github.com/gammons/config_fun

Does your gem or project need the user to create or add configuration to it? Use ConfigFun to allow them to easily configure your gem, in a quick, terse and painless way!

SYNOPSIS:

Adding ConfigFun to your project is super easy. Include the config_fun gem as part of your gem's dependencies.

Say you need to write a configuration that allows the user to add their account information, name, password, and some IMAP information. With ConfigFun, the configuration could look like this:

account do
  name "grant"
  password "bungalow"

  imap do
    server "imap.gmail.com"
    ssl true
  end
end

To parse this configuration, you'd simply Say the following:

config = ConfigFun.parse!(File.read(account_information_file))

..and the config variable would include a sweet hash of all the info the user provided!

config == {:account=> {:password => "bungalow", :imap => {:ssl => true, :server => "imap.gmail.com"}, :name => "grant"}}

DETAILS:

Arrays

if ConfigFun encounters a block twice within the same context, it will create an array.

user do
  room "123"
end

user do
  room "456"
end

The resulting hash will have a :user key with an array of :rooms as the value.

{:user=>[{:room=>"123"}, {:room=>"456"}]}

Setting values

ConfigFun will evaluate the result of anything that is being set. You can use strings or arrays.

user do
  name "bob"
  keys [1,2,3]
end

Using reserved words in ruby

If your config needs to use a ruby reserved word, place an underscore before the word.

_class do
  type "airplane"
end

...Results in...

{:class => {:type => 'airplane'}}

An important caveat

Blocks do not take any paramters. You may be tempted to do something like the following:

user "Bob" do
  room "123"
end

ConfigFun will not be able to parse this, because there is no equivalent hash it can boil down to.

The correct way to write the above code for ConfigFun is as follows:

user do
  name "bob"
  room "123"
end

REQUIREMENTS:

  • None!
  • Tested against ruby 1.8.7 and 1.9.2.

INSTALL:

gem install config_fun

AUTHORS:

  • Grant Ammons

LICENSE:

(The MIT License)

Copyright (c) 2010 Grant Ammons (grant@pipelinedealsco.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.