Flexible and simple in-application settings for Rails/Sinatra applications
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lib v0.1.2 Aug 5, 2011
spec additional tests Jun 15, 2011
.gitignore switch to bundler, fixed a few tests, restructure Jun 15, 2011
.travis.yml added travis config Aug 5, 2011
Gemfile switch to bundler, fixed a few tests, restructure Jun 15, 2011
Rakefile switch to bundler, fixed a few tests, restructure Jun 15, 2011
app-config.gemspec gemspec update Aug 5, 2011



AppConfig is a library to manage your (web) application dynamic settings with flexible access and configuration strategy.

Primary datasource for AppConfig is an ActiveRecord model.


via rubygems:

gem install app-config

via github:

git clone git://github.com/sosedoff/app-config.git
cd app-config
gem build
gem install app-config-x.y.z.gem

Supported platforms

  • Ruby 1.8.7
  • Ruby EE 1.8.7
  • Ruby 1.9.2

Data Formats

You can use following formats:

  • String
  • Boolean
  • Array
  • Hash

String format is a default format. Everything is a string by default.

Boolean format is just a flag, values 'true', 'on', 'yes', 'y', '1' are equal to True. Everything else is False.

Array format is a multiline text which is transformed into array. Each evelemnt will be trimmed. Empty strings are ignored.

Hash format is special key-value string, "foo: bar, user: username", which is transformed into Hash instance. Only format "keyname: value, keyname2: value2" is supported. No nested hashes allowed.


AppConfig is designed to work with ActiveRecord model. Only ActiveRecord >= 3.0.0 is supported.

By default model "Setting" will be used as a data source.


In order to use AppConfig you should have a source table, similar to this:

class CreateSettings < ActiveRecord::Migration
  def self.up
    create_table :settings do |t|
      t.string   :keyname,       :null => false, :limit => 64
      t.text     :value,         :null => false
      t.string   :value_format,  :limit => 64,   :default => "string"
      t.string   :name,          :limit => 64
      t.string   :description,   :limit => 512

  def self.down
    drop_table :settings

Required columns are:

  • keyname
  • value
  • value_format

Columns :name and :description are optional and used only for informative purpose (ex: show name and description in admin panel).

There is no need in indexes since the data is being loaded only once on application start.

Simple model with validations should look like this:

class Setting < ActiveRecord::Base
  validates_presence_of   :keyname, :value
  validates_uniqueness_of :keyname


Default configuration method is:


If your settings model has a different schema, you can redefine columns:

  :model  => Setting,           # model class name as source
  :key    => 'KEYNAME_FIELD',   # field that contains name
  :format => 'FORMAT_FIELD',    # field that contains key format
  :value  => 'VALUE_FIELD',     # field that contains value data

Load all settings somewhere in your application. In Rails it should be initializer file.


Configuration in Rails 3: (you can put this into environment/ENV or application.rb) Make sure your application does not have any critical parts depending on AppConfig at startup.

config.after_initialize do
  AppConfig.configure(:model => Setting)
  AppConfig.load if Setting.table_exists?

AppConfig gives you 3 ways to access variables:

AppConfig.my_setting      # method-like
AppConfig[:my_setting]    # hash-like by symbol key
AppConfig['my_setting']   # hash-like by string key

You can define settings items manually. NOTE: THESE KEYS WILL BE REMOVED ON RELOAD/LOAD.

AppConfig.set_key('KEYNAME', 'VALUE', 'FORMAT')

Everytime you change your settings on the fly, use reload:


Cleanup everything:



Copyright (c) 2011 Dan Sosedoff.