The project is in a healthy, maintained state
Load environment variables from multiple .env files with configurable precedence, type coercion, required key validation, default values, and template generation for documentation.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

philiprehberger-env_loader

Tests Gem Version Last updated

Multi-source environment variable loader with precedence and validation

Requirements

  • Ruby >= 3.1

Installation

Add to your Gemfile:

gem "philiprehberger-env_loader"

Or install directly:

gem install philiprehberger-env_loader

Usage

require "philiprehberger/env_loader"

Philiprehberger::EnvLoader.load('.env', '.env.local',
  required: %w[DATABASE_URL SECRET_KEY],
  defaults: { 'PORT' => '3000' },
  types: { 'PORT' => :integer, 'DEBUG' => :boolean }
)

File Precedence

# Later files override earlier ones; existing ENV always wins
Philiprehberger::EnvLoader.load('.env', '.env.local', '.env.production')

Validation

Philiprehberger::EnvLoader.validate!('DATABASE_URL', 'REDIS_URL')
# raises EnvLoader::ValidationError if any key is missing or empty

Template Generation

Philiprehberger::EnvLoader.generate_template(
  output: '.env.template',
  keys: %w[DATABASE_URL REDIS_URL SECRET_KEY PORT]
)

API

Method Description
.load(*files, required:, types:, defaults:) Load variables from .env files with options
.validate!(*keys) Raise if any keys are missing or empty in ENV
.generate_template(output:, keys:) Generate a .env.template file
EnvLoader::Error Base error class for all gem errors
EnvLoader::ValidationError Raised when required keys are missing or empty

Development

bundle install
bundle exec rspec
bundle exec rubocop

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT