Sha256: 3a64a91584561c72d70fb1fb05edd81b08e37cefce44dc90535bde811107ddd5
Contents?: true
Size: 1.46 KB
Versions: 2
Compression:
Stored size: 1.46 KB
Contents
# frozen_string_literal: true require "dry/core/class_builder" require "dry/types" require "dry/struct" require "dry/system/settings/file_loader" require "dry/system/constants" module Dry module System module Settings class DSL < BasicObject attr_reader :schema def initialize(&block) @schema = {} instance_eval(&block) end def call Core::ClassBuilder.new(name: "Configuration", parent: Configuration).call do |klass| schema.each do |key, type| klass.setting(key, type) end end end def key(name, type) schema[name] = type end end class Configuration < Dry::Struct def self.setting(*args) attribute(*args) end def self.init(root, env) env_data = load_files(root, env) attributes = {} errors = {} schema.each do |key| value = ENV.fetch(key.name.to_s.upcase) { env_data[key.name.to_s.upcase] } type_check = key.try(value || Undefined) attributes[key.name] = value if value errors[key] = type_check if type_check.failure? end raise InvalidSettingsError, errors unless errors.empty? new(attributes) end def self.load_files(root, env) FileLoader.new.(root, env) end private_class_method :load_files end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
dry-system-0.22.0 | lib/dry/system/settings.rb |
dry-system-0.21.0 | lib/dry/system/settings.rb |