Skip to content

Instantly share code, notes, and snippets.

@Drowze
Created March 20, 2025 15:42
Show Gist options
  • Save Drowze/852ce557b1d5df192d8a5234b3dc45a5 to your computer and use it in GitHub Desktop.
Save Drowze/852ce557b1d5df192d8a5234b3dc45a5 to your computer and use it in GitHub Desktop.
Using dry-configurable to store app settings.
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'dry-configurable'
end
require "singleton"
class Settings
include Singleton
extend Dry::Configurable
# "reader: true" allows calling Settings.client_id instead of Settings.config.client_id
setting :client_id, reader: true
# "constructor: ..." allows doing some type coercion on the supplied value
setting :redis_db, reader: true, constructor: ->(value) { Integer(value) }
# "default: ..." allows to set some pre-defined default
setting :coverband_enabled, reader: true, default: false, constructor: ->(value) { value == "true" }
setting :foobar, reader: true
end
ENV["CLIENT_ID"] = "abc123"
ENV["REDIS_DB"] = "1"
ENV["COVERBAND_ENABLED"] = "true"
Settings.settings.map(&:name).each do |setting|
value = ENV[setting.to_s.upcase]
Settings.config.public_send("#{setting}=", value) if value
end
p Settings.client_id
# => "abc123"
p Settings.redis_db
# => 1
p Settings.coverband_enabled
# => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment