/konfigurator

Small and flexible configuration toolkit inspired i.a. by Sinatra settings.

Primary LanguageRubyMIT LicenseMIT

Konfigurator

Konfigurator is a small and flexible configuration toolkit, which allow you to configure your apps, classes or modules with DSL-style or Sinatra-like settings.

Installation

You can install Konfigurator simply using rubygems:

sudo gem install konfigurator

…or install it from source:

git clone git://github.com/nu7hatch/konfigurator.git
cd konfigurator
rake install

Sinatra-like configuration

Konfigurator is very easy to use. Take a look at simple example.

class MyClass do 
  include Konfigurator

  set :foo, "bar"
  enable :bar
  disable :bla

  configure :production do
    enable :bla
    set :spam, "eggs!"
  end
end

Now you can get configured options directly from your class:

MyClass.foo # => "bar"
MyClass.bar # => true
MyClass.bla # => false

… or when current environment is set to :production:

MyClass.bla  # => true
MyClass.spam # => "eggs!"

All settings are also available from objects via #settings method:

obj = MyObject.new
obj.settings.foo # => "bar"
obk.settings.bar # => true

Remember! when option is not set then NoMethodError will be raised after try to get it direcly from class, eg:

MyObject.set :exist
MyObject.exist      # => true
MyObject.not_exist  # => will raise NoMethodError

DSL-style configuration

Not there is also possible to use nice-looking DSL syntax provided by Konfigurator::DSL. It allow you to configure your apps/classes such like here:

Foo.configure do 
  host "127.0.0.1"
  port 8080
  password "secret"
end

But what’s important in this kind of configuration, you have to define all possible options first. You can define configuration attributes easy using #attr_config (or #attr_setting alias).

class Foo 
  include Konfigurator::DSL

  attr_config :host, :port
  attr_config :password
end

Other use cases behave almost the same as with Konfigurator::Simple:

Foo.host # => "127.0.0.1"
Foo.port # => 8080

Foo.env :production
Foo.configure :production do 
  host "production.com"
  port 80
end

foo = Foo.new
foo.settings.host # => "production.com"
foo.settings.port # => 80

Configuring Rails 3 application

You can easily mix Konfigurator with your Rails 3 app. Add this line to your config/application.rb:

require 'konfigurator/railtie'

… and now you can set your own configuration, eg:

module MyApp
  class Application < Rails::Application
    set :foo, "Foo"
    set :bat, "Bar"
    enable :foobar

    # ...
  end
end

Now you can use it in controllers…

class UsersController
  def index
    if settings.foobar and settings.foo == "Foo"
      # ...
    end
  end
end

… and views…

<h1><%= settings.foo %></h1>
<p><%= settings.bar %></p>

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 Kriss ‘nu7hatch’ Kowalik. See LICENSE for details.