Version 1.0.0 of configurability has been released.
Configurability is a mixin that allows you to add configurability to one
more objects or classes. You can assign them each a subsection of the
configuration, and then later, when the configuration is loaded, the
configuration is split up and sent to the objects that will use it.
To add configurability to a class, just require the library and extend
class User extend Configurability end
Or, add it to a instance:
user = User.new user.extend( Configurability )
Later, when you’ve loaded the configuration, can can call
Configurability.configure_objects( config )
config will be spliced up and sent to all the objects that
been extended with it.
Configurability expects the configuration to be
broken up into a number of sections, each of which is accessible via
a method with the section name or the index operator (
section name as a
Symbol or a
config.section_name config[:section_name] config['section_name']
The section name is based on an object’s config key, which is the name
the object that is being extended with all non-word characters converted
_) by default. It will also have any leading Ruby-style
namespaces stripped, e.g.,
MyClass -> :myclass Acme::User -> :user "J. Random Hacker" -> :j_random_hacker
If the object responds to the
#name method, then the return value of
method is used to derive the name. If it doesn’t have a
name of its
Class will be used instead. If its class is anonymous,
the object’s config key will be
When the configuration is loaded, an instance variable called
to the appropriate section of the config object for each object that has
been extended with Configurability.
The default behavior above is just provided as a reasonable default; it
expected that you’ll want to customize at least one or two things about
how configuration is handled in your objects.
Setting a Custom Config Key
The first thing you might want to do is change the config section that
corresponds to your object. You can do that by declaring a different
config key, either using a declarative method:
class OutputFormatter extend Configurability config_key :format end
or by overriding the
#config_key method and returning the desired
as a Symbol:
class User extend Configurability def self::config_key return :employees end end
Changing How an Object Is Configured
You can also change what happens when an object is configured by
#configure method that takes the config section as an argument:
class WebServer extend Configurability def self::configure( configsection ) @default_bind_addr = configsection[:host] @default_port = configsection[:port] end end
If you still want the
config variable to be set, just
your implementation; don’t if you don’t want it to be set.
$ sudo gem install configurability
or from source:
$ wget http://deveiate.org/code/configurability-1.0.0.tar.gz
$ tar -xzvf configurability-1.0.0.tar.gz
$ cd configurability-1.0.0
$ sudo rake install
This is the first release.
You can submit bug reports, suggestions, and read more about future
the project page:
or clone it with Mercurial from the same address.
Copyright © 2010, Michael G.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
this list of conditions and the following disclaimer in the
and/or other materials provided with the distribution.
Neither the name of the author/s, nor the names of the project’s
contributors may be used to endorse or promote products derived from
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.