Sha256: af6a6f741fa28d1120cf3cebb128fdf6d988a3d75dd0a1d61526acb5283e09a2

Contents?: true

Size: 1.85 KB

Versions: 7

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module Dry
  module System
    module ProviderSources
      # @api private
      module Settings
        InvalidSettingsError = Class.new(ArgumentError) do
          # @api private
          def initialize(errors)
            message = <<~STR
              Could not load settings. The following settings were invalid:

              #{setting_errors(errors).join("\n")}
            STR

            super(message)
          end

          private

          def setting_errors(errors)
            errors.sort_by { |k, _| k }.map { |key, error| "#{key}: #{error}" }
          end
        end

        # @api private
        class Config
          # @api private
          def self.load(root:, env:, loader: Loader)
            loader = loader.new(root: root, env: env)

            new.tap do |settings_obj|
              errors = {}

              settings.to_a.each do |setting|
                value = loader[setting.name.to_s.upcase]

                begin
                  if value
                    settings_obj.config.public_send(:"#{setting.name}=", value)
                  else
                    settings_obj.config[setting.name]
                  end
                rescue => e # rubocop:disable Style/RescueStandardError
                  errors[setting.name] = e
                end
              end

              raise InvalidSettingsError, errors unless errors.empty?
            end
          end

          include Dry::Configurable

          private

          def method_missing(name, *args, &block)
            if config.respond_to?(name)
              config.public_send(name, *args, &block)
            else
              super
            end
          end

          def respond_to_missing?(name, include_all = false)
            config.respond_to?(name, include_all) || super
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
dry-system-1.1.1 lib/dry/system/provider_sources/settings/config.rb
dry-system-1.1.0 lib/dry/system/provider_sources/settings/config.rb
dry-system-1.1.0.beta2 lib/dry/system/provider_sources/settings/config.rb
dry-system-1.1.0.beta1 lib/dry/system/provider_sources/settings/config.rb
dry-system-1.0.1 lib/dry/system/provider_sources/settings/config.rb
dry-system-1.0.0 lib/dry/system/provider_sources/settings/config.rb
dry-system-1.0.0.rc1 lib/dry/system/provider_sources/settings/config.rb