
Security News
Oxlint Introduces Type-Aware Linting Preview
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.
Rails vanilla config is good enough, but tends to get messy. This gem provides a few helpers to
Rails.application.configure do
extend Loadout::Helpers
config.some_secret = cred(:secret) { 'default' }
config.value_from_env_or_cred = env.cred(:key_name)
prefix(:service) do
config.x.service.optional_string = env.cred(:api_key) { 'default_key' }
config.x.service.required_string = env.cred(:api_secret)
config.x.service.optional_bool = bool.env(:bool_flag) { false }
config.x.service.number = int.env.cred(:number) { nil }
config.x.service.float = float.env.cred(:number)
config.x.service.array = list.env(:comma_list)
end
end
Note: this gem requires Ruby 3.
Install the gem and add to the application's Gemfile by executing:
$ bundle add loadout
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install loadout
Include helpers into your config/application.rb
and config/environments/*.rb
:
extend Loadout::Helpers
This should be done in each file where you'd like to use loadout.
Grab a value from credentials:
config.key = cred(:key_name)
If you don't set this credential, you will get an error:
Loadout::MissingConfigError: required credential (key_name) is not set
Or from ENV:
config.key = env(:key_name)
If you don't set this env, you will get an error:
Loadout::MissingConfigError: required environment variable (KEY_NAME) is not set
Look up ENV, then credentials, then fail:
config.key = env.cred(:key_name)
If neither are set, you will get an error:
Loadout::MissingConfigError: required environment variable (KEY_NAME) or credential (key_name) is not set
Or the other way around:
config.key = cred.env(:key_name)
If it's a nested credential value, you can supply multiple keys:
# Look up service.key_name in credentials
config.key = cred(:service, :key_name)
It will do the right thing if you also add env:
# Look up service.key_name in credentials, or SERVICE_KEY_NAME in ENV
config.key = cred.env(:service, :key_name)
Parse ENV value into a boolean:
# Valid true strings: 1/y/yes/t/true
# Valid false strings: "" or 0/n/no/f/false
# (case insensitive)
#
# Any other string will raise an error.
config.some_flag = bool.cred.env(:key_name)
If you set an invalid value, you will get an error:
Loadout::InvalidConfigError: invalid value for bool (`value`) in KEY_NAME
Note: because credentials come from YAML, they don't need to be parsed. Only ENV values are parsed.
Integers and floats are also supported:
config.some_int = int.cred.env(:int_key_name)
config.some_float = float.cred.env(:float_key_name)
Lists are supported too:
# Parses strings like "foo, bar, baz", "foo|bar|baz", "foo bar baz" into ['foo', 'bar', 'baz']
config.some_list = list.cred.env(:key_name)
You can set your own list separator (string or regex):
# Parses 'foo0bar0baz' into ['foo', 'bar', 'baz']
config.some_list = list('0').env(:key_name)
Use a block at the end to specify a default value:
config.some_list = list.cred.env(:key_name) { ['default'] }
Use prefix to avoid repeating the same nesting:
prefix(:service) do
config.x.service.api_key = env(:api_key) # Looks up "SERVICE_API_KEY"
config.x.service.api_secret = env(:api_secret) # Looks up "SERVICE_API_SECRET"
end
Note that left hand side is unaffected. Only loadout helpers get auto-prefixed.
If you'd like a way to shorten the left hand side too, you can assign the whole group as a hash or OrderedOptions (this is not a loadout feature, just something you can do with Rails):
prefix(:service) do
config.x.service = ActiveSupport::OrderedOptions[
api_key: env(:api_key),
api_secret: env(:api_secret)
]
end
Since prefix
returns the block's result, you can rewrite the above as follows:
config.x.service = prefix(:service) {
ActiveSupport::OrderedOptions[
api_key: env(:api_key),
api_secret: env(:api_secret)
]
}
prefix
lets you supply a default to the whole block:
prefix(:service, default: -> { 'SECRET' }) do
config.x.service.api_key = env(:api_key) # falls back to 'SECRET'
config.x.service.api_secret = env(:api_secret) # falls back to 'SECRET'
end
Instead of extend Loadout::Helpers
you can extend Loadout
to include one proxy method loadout
. Now all helpers live in one place.
Rails.application.configure do
extend Loadout
config.some_key = loadout.cred.env(:some_key)
end
Feel free to alias it to something shorter if you'd like:
Rails.application.configure do
extend Loadout
alias l loadout
config.some_key = l.cred.env(:some_key)
end
By default loadout will look into credentials
and ENV
in your config's context. If your credentials are called something else, or you want to supply an alternative source of ENV, you can configure it like so:
Rails.application.configure do
extend Loadout::Helpers
loadout creds: alt_credentials, env: alt_env
# Now loadout will use alt_credentials and alt_env to look up values.
end
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and the created tag, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/loadout. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Loadout project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
FAQs
Unknown package
We found that loadout demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.
Security News
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.