Settingslogic

Settingslogic is an old library of mine that I decided to go ahead and share with the world. It’s nothing crazy or new. Just a simple solution to a simple problem. Settingslogic provides globally accessible settings via an ERB enabled YAML file using a singleton design pattern. It has been great for my apps, maybe you will enjoy it too.

So here is my question to you.….is Settingslogic a great settings solution or the greatest?

* Documentation: settingslogic.rubyforge.org * Repository: github.com/binarylogic/settingslogic/tree/master

Install and use

sudo gem install settingslogic

For rails, as a gem (recommended):

# config/environment.rb
config.gem "settingslogic"

Or as a plugin (for older versions of rails)

script/plugin install git://github.com/binarylogic/settingslogic.git

Create your settings

By default Settingslogic tries to load config/application.yml. This is just a typical YAML file, notice ERB is allowed.

# app/config/application.yml
defaults: &defaults
  cool:
    saweet: nested settings
  neat_setting: 24
  awesome_setting: <%= "Did you know 5 + 5 = " + (5 + 5) + "?" %>

development:
  <<: *defaults
  neat_setting: 800

test:
  <<: *defaults

production:
  <<: *defaults

Take note of the environment namespacing. If your framework supports environments this is a good way to support environment specific settings. If you are using this in an area where there are no environment disregard the namespacing. It will work just fine without it.

Access your settings

>> RAILS_ENV
=> "development"

>> Settings.cool
=> "#<Settingslogic::Settings ... >"

>> Settings.cool.saweet
=> "nested settings"

>> Settings.neat_setting
=> 800

>> Settings.awesome_setting
=> "Did you know 5 + 5 = 10?"

Multiple settings

settings1 = Settings.new(:settings1) # looks for config/settings1.yml
settings2 = Settings.new("settings2.yaml") # looks for settings2.yml
settings2 = Settings.new("/abs/path/settings2.yaml") # looks for /abs/path/settings2.yml
settings3 = Settings.new(:some_setting => "some value")

Configure

Configuration is optional. See Settingslogic::Config for more details.

# config/initializers/settingslogic.rb
Settingslogic::Config.configure do |config|
  config.file_name = :config # will look for config/config.yml
  config.file_name = "config" # will look for config
  config.file_name = "config.yaml" # will look for confg.yaml
  config.file_name = "/absolute/path/config.yml" # will look for /absolute/path/config.yml
end

Copyright © 2008 Ben Johnson of Binary Logic, released under the MIT license