Sha256: b1d2db25a271fd7dfcb295877983fd9d1f87347357cf7621924bfe3f9adafa4a

Contents?: true

Size: 1.29 KB

Versions: 6

Compression:

Stored size: 1.29 KB

Contents

require "yaml"

module Dry
  module Web
    class Settings
      SettingValueError = Class.new(StandardError)

      def self.schema
        @schema ||= {}
      end

      def self.setting(name, type = nil)
        settings(name => type)
      end

      def self.settings(new_schema)
        check_schema_duplication(new_schema)
        @schema = schema.merge(new_schema)

        self
      end

      def self.check_schema_duplication(new_schema)
        shared_keys = new_schema.keys & schema.keys

        raise ArgumentError, "Setting :#{shared_keys.first} has already been defined" if shared_keys.any?
      end
      private_class_method :check_schema_duplication

      def self.load(root, env)
        yaml_path = root.join("config/settings.yml")
        yaml_data = File.exist?(yaml_path) ? YAML.load_file(yaml_path)[env.to_s] : {}
        schema = self.schema

        Class.new do
          extend Dry::Configurable

          schema.each do |key, type|
            value = ENV.fetch(key.to_s.upcase) { yaml_data[key.to_s.downcase] }

            begin
              value = type[value] if type
            rescue => e
              raise SettingValueError, "error typecasting +#{key}+: #{e}"
            end

            setting key, value
          end
        end.config
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dry-web-0.6.0 lib/dry/web/settings.rb
dry-web-0.5.0 lib/dry/web/settings.rb
dry-web-0.4.1 lib/dry/web/settings.rb
dry-web-0.4.0 lib/dry/web/settings.rb
dry-web-0.3.1 lib/dry/web/settings.rb
dry-web-0.3.0 lib/dry/web/settings.rb