Setty
Mini application configuration for Rails projects.
Installation
Add this line to your application's Gemfile:
gem 'setty'
And then execute:
$ bundle
Usage
The config files will be automatically loaded after rails loads.
Example:
# config/settings.yml
production:
secret_token: "<%= ENV['SECRET_TOKEN'] %>"
ssl_only: true
developement:
secret_token: "blablablabla"
ssl_only: false
# config/settings/products.yml
production:
minumum_photos_count: 4
development:
minumum_photos_count: 1
test:
minumum_photos_count: 0
This gives you the Settings
object:
# in development
Settings.secret_token #=> "blablablabla"
Settings.secret_token? #=> true
Settings.ssl_only #=> false
Settings.ssl_only? #=> false
Settings.products.minumum_photos_count #=> 1
You can find example of most important features and plugins - here.
Features:
- Configurable
- Environment dependent
- Interpolation
- Nested settings
- Reloading settings
Configurable
# application.rb
module MyApp
class Application < Rails::Application
# ... code ... code ...
config.setty.settings_object_name = 'Settings' # => settings will be loaded in `Settings`
config.setty.settings_path = 'settings' # => extracts settings from `config/settings/*` and `config/settings.yml`
config.setty.settings_environment = Rails.env # => enviroment name (defaults to Rails.env)
end
end
Environment dependent
# validations.yml
development:
require_user_to_belong_to_account: true
test:
require_user_to_belong_to_account: false
production:
require_user_to_belong_to_account: true
Depending on your Rails environment:
# in development
Settings.validations.require_user_to_belong_to_account #=> true
Settings.validations.require_user_to_belong_to_account? #=> true
# in test
Settings.validations.require_user_to_belong_to_account #=> false
Settings.validations.require_user_to_belong_to_account? #=> false
Interpolation
production:
archives_path: "<%= Rails.root.join('archives').realpath %>"
Plays nicely with Dotenv
# s3.yml
production:
access_key_id: "<%= ENV['S3_ACCESS_KEY'] %>"
secret_access_key: "<%= ENV['S3_SECRET_KEY'] %>"
region: "eu-west-1"
bucket: "my-app"
# in production
Settings.s3.access_key_id #=> S3_ACCESS_KEY from `.env.production`
Settings.s3.secret_access_key #=> S3_SECRET_KEY from `.env.production`
Nested Settings
The following yaml files:
config/settings.yml
config/settings/validations.yml
config/settings/validations/product.yml
config/settings/validations/category.yml
Will produce the following settings options hierarchy:
Settings
Settings.validations
Settings.validations.product
Settings.validations.category
Reloading
You can reload the current settings by calling:
Settings.reload
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request