Skip to content


Folders and files

Last commit message
Last commit date

Latest commit



30 Commits

Repository files navigation

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.

    :default => '',
    :desc => 'The host the database adapter connects to.'
    => ''

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

The schema is accessible as a hash via #schema, so you can easily pump out
configuration docs, HTML forms, whatever.

Construct 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
    def db_str
      self[:db_str] || make_db_string_from_hash(db)

Defining schemas at the class level, as opposed to creating a Construct
instance and operating on its schema is easy. Just use Construct.define to
operate on the class schema. When you create an instance of that class, the class schema is used as a default for the instance.

  class UserConfig < Construct
    define :state,
      :desc => "The user's home state"
      :default => "Oregon"

  conf =
  conf.state # => "Oregon"

Problem solved. Now get back to having fun instead of worrying about

Kyle Kingsbury <>
Spencer Miles <>


Extensible, persistent, structured configuration for Ruby







No packages published