Configlet

Description

A stupid simple wrapper for environment variables.

This doesn’t deserve to be released as a gem, but I’m using it in two different projects. Seriously, go find a real configuration library and use it instead.

Examples

require "configlet"

# assuming...
ENV["THUNK_STATUS"]   = "crazy-awesome"
ENV["THUNK_FINISHED"] = "false"

# some bits of optional config
Configlet.prefix = :thunk
Configlet.default :token => "default-token"
Configlet.munge(:finished) { |k| "true" == k }

# grabbin' values
Configlet[:status]   # => "crazy-awesome"
Configlet[:finished] # => false
Configlet[:token]    # => "default-token"

There’s also a block form, which is nice if you’re setting a lot of defaults:

Configlet.config :thunk do
  default :token => "default-token"
  default :host  => "thunk.local"
end

Defaults can be lazy, too: Just use a lambda. The default will be resolved the first time its value is grabbed.

default :db => lambda { "db://whatever/#{Rails.env}" } # or...
default(:db) { "db://whatever/#{Rails.env}" }

Configlet is a module, so include or extend it wherever you want. It extends itself for your convenience.

Transformation

Want something other than a string? You can munge any value:

default :url => "http://example.org"
munge(:url) { |v| URI.parse v }
Configlet[:url] # => #<URI::HTTP:0xdeadbeef URL:http://example.org>

In fact, there’s a shortcut just for URLs. I haven’t added any others ‘cause I haven’t needed to care:

url :url => "http://example.org"
Configlet[:url] # => #<URI::HTTP:0xcafebabe URL:http://example.org>

Installation

$ gem install configlet

License

Copyright 2010 John Barnette ([email protected])

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.