Installation
Add this line to your application's Gemfile:
gem "env_vars"
And then execute:
$ bundle
Or install it yourself as:
$ gem install env_vars
Usage
Config = Env::Vars.new do
mandatory :database_url, string
optional :timeout, int, 10
optional :force_ssl, bool, false
optional :rails_env, "development", string, aliases: %w[env]
end
Config.database_url
Config.timeout
Config.force_ssl?
If you're going to use env_vars
as your main configuration object, you can also set arbitrary properties, like the following:
Config = Env::Vars.new do
optional :redis_url, string, "redis://127.0.0.1"
property :redis, -> { Redis.new } # pass an object that responds to #call
property(:now) { Time.now } # or pass a block.
end
Config.redis.set("key", "value")
Config.redis.get("key")
#=> "value"
Values are cached by default. If you want to dynamically generate new values, set cache: false
.
Config = Env::Vars.new do
property(:uuid, cache: false) { SecureRandom.uuid }
end
Types
You can coerce values to the following types:
string
: Is the default. E.g.optional :name, string
.int
: E.g.optional :timeout, int
.float
: E.g.optional :wait, float
.bigdecimal
: E.g.optional :fee, bigdecimal
.bool
: E.g.optional :force_ssl, bool
. Any ofyes
,true
or1
is considered astrue
. Any other value will be coerced tofalse
.symbol
: E.g.optional :app_name, symbol
.array
: E.g.optional :chars, array
oroptional :numbers, array(int)
. The environment variable must be something likea,b,c
.
Dotenv integration
If you're using dotenv, you can simply require env_vars/dotenv
. This will load environment variables from .env.local.%{environment}
, .env.local
, .env.%{environment}
and .env
files, respectively. You must add dotenv
to your Gemfile
.
require "env_vars/dotenv"
Configuring Rails
If you want to use env_vars
even on your Rails configuration files like database.yml
and secrets.yml
, you must load it from config/boot.rb
, right after setting up Bundler.
ENV["BUNDLE_GEMFILE"] ||= File.("../../Gemfile", __FILE__)
# Set up gems listed in the Gemfile.
require "bundler/setup"
# Load configuration.
require "env_vars/dotenv"
require File.("../config", __FILE__)
Development
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 tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/fnando/env_vars. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Icon
Icon made by eucalyp from Flaticon is licensed by Creative Commons BY 3.0.