philiprehberger-config_kit
Layered configuration with YAML, ENV, and defaults.
Requirements
- Ruby >= 3.1
Installation
Add to your Gemfile:
gem "philiprehberger-config_kit"Or install directly:
gem install philiprehberger-config_kitUsage
Basic (defaults only)
require "philiprehberger/config_kit"
config = Philiprehberger::ConfigKit.define do
string :app_name, default: "my-app"
integer :port, default: 3000
boolean :debug, default: false
end
config[:app_name] # => "my-app"
config[:port] # => 3000
config[:debug] # => falseWith YAML file
# config.yml
app_name: "production-app"
port: 8080config = Philiprehberger::ConfigKit.define(yaml: "config.yml") do
string :app_name, default: "my-app"
integer :port, default: 3000
boolean :debug, default: false
end
config[:app_name] # => "production-app" (from YAML)
config[:port] # => 8080 (from YAML)
config[:debug] # => false (from default)With ENV overrides
config = Philiprehberger::ConfigKit.define(yaml: "config.yml") do
string :app_name, default: "my-app", env: "APP_NAME"
integer :port, default: 3000, env: "PORT"
boolean :debug, default: false, env: "DEBUG"
end
# ENV["PORT"] = "9090" would override both YAML and default
config[:port] # => 9090Resolution order
Values are resolved in this order (highest priority first):
-
ENV variables (if
env:key is set and the variable exists) -
YAML file (if
yaml:path is provided and the key exists) - Defaults (from the schema definition)
Export all values
config.to_h
# => { app_name: "my-app", port: 3000, debug: false }API
| Method | Description |
|---|---|
Philiprehberger::ConfigKit.define(yaml:, env:, &block) |
Create a new config store |
config.get(key) / config[key]
|
Get a config value |
config.to_h |
Export all values as a hash |
config.keys |
List all defined keys |
config.key?(key) |
Check if a key is defined |
Schema DSL
| Method | Type | Cast behavior |
|---|---|---|
string(name, default:, env:) |
:string |
.to_s |
integer(name, default:, env:) |
:integer |
Integer(value) |
float(name, default:, env:) |
:float |
Float(value) |
boolean(name, default:, env:) |
:boolean |
true/"true"/"1"/"yes" are truthy |
Development
bundle install
bundle exec rspec
bundle exec rubocopLicense
MIT