Construct: A Tiny Gem for Ruby Configuration

Ruby developer Kyle Kingsbury has released Construct, a small gem designed to eliminate the boilerplate of writing configuration classes. The project is available on GitHub under the aphyr account and can be installed with gem install construct.

Key Features

Construct offers OpenStruct-style access to key-value pairs, allowing simple assignments like config.offices = ['Sydney', 'Tacoma']. Nested structures are handled transparently: a hash assigned to a key can be accessed with method calls, so config.fruits = {:banana => 'slightly radioactive', :apple => 'safe'} lets you retrieve config.fruits.banana directly.

Default values are defined through an overridable, self-documenting schema:

config.define(:address, :default => '1 North College St')
config.address # => '1 North College St'
config.address = 'Urnud'
config.address # => 'Urnud'

YAML serialization is straightforward, with config.to_yaml for saving and Construct.load(yaml) for loading. The gem also supports subclassing, letting you define custom methods on your config classes, such as a fooo method that appends an extra 'o' to an existing foo attribute.