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?
Bugs / feature suggestions: binarylogic.lighthouseapp.com/projects/19028-settingslogic
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?"
“defaults” (or “default”) is a special namespace. Settings in defaults will automatically be merged with any new settings under your specific environment. This gives you a nice way to set defaults for settings files that may need to be generated programatically without YAML variables.
defaults: application: name: my app port: 3000 development: production: application: port: 80 Would give you settings like this: >> RAILS_ENV => "development" >> Settings.application.name => "my app" >> Settings.application.port => 3000 >> RAILS_ENV => "production" >> Settings.application.name => "my app" >> Settings.application.port => 80
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")
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