Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Extensible, persistent, structured configuration for Ruby
tag: release-0.1.0

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.


Construct is extensible, persistent, structured configuration for Ruby and
humans with text editors. What do I mean?

You have a ruby program. Maybe it's a Ramaze app. It needs to store some
configuration data. It should be easily serialized to YAML so anyone can edit
the file easily. It should support nested structures. It should minimize
typing, and make access unambiguous. It should take no effort to create and
extend multi-layer configuration with many data types and accessors. Moreover,
it should allow you to define a schema for the options that are available,
making documentation easy. That's where Construct comes in.

  gem install construct

  config =

Hey, we have a configuration object. Let's describe a web site. Keys are always symbols, but can be accessed via methods just like an OpenStruct. = "Aphyr"
  config.base_url = ""

And getting at them is easy, too.
    => "Aphyr"

If the key you want is already a method, just use config#[] and config#[]=. Construct converts strings to symbols for you..

How about a database connection? Those parameters all belong together.

  config.db = {
    :user => 'cr',
    :password => 'some password',

Secretly, Construct converted that hash into another Construct. You can chain
methods to easily access nested options.

    => 'cr'

Databases need a host. Let's define a host field.

  config.db.schema[:host] = {
    :default => '',
    :desc => 'The host the database adapter connects to.'
    => ''

You can override this, naturally. = ''
    => ''

And it serializes neatly to YAML. Keys are expressed as strings, to save typing and make things look clean.

  config.to_yaml =>
    user: cr
    password: some password
  name: Aphyr

Hey, this is easy! Now let's load that configuration from a string.

  config = Construct.load yaml

Maybe you need to implement extra logic in your config--perhaps you can specify either a db hash as shown above, or a connection string. Just subclass Construct::Construct or define methods directly on the construct.

  class DBConfig < Construct::Construct
    def db_str
      self[:db_str] || make_db_string_from_hash(db)

Problem solved. Now get back to having fun instead of worrying about configuration!
Something went wrong with that request. Please try again.