Sha256: 186b6b55213a27fa3972939ad8077bb107eb232edddfb76e2f1fbaa74384b581

Contents?: true

Size: 991 Bytes

Versions: 5

Compression:

Stored size: 991 Bytes

Contents

# frozen_string_literal: true

require "dry-initializer"
require "dry/schema"

module ENVV
  class Builder
    attr_reader :env, :schema

    class << self
      def call(env, schema = nil)
        new(env, schema).call
      end

      def to_proc
        method(:call).to_proc
      end
    end

    def initialize(env, schema)
      @env = env
      @schema = schema
    end

    def call
      validate_params!
      result = @schema.call(extract_env_vars)

      if result.failure?
        raise ValidationError, result.errors(full: true).to_h.values.flatten
      else
        ENVV::Registry[result.to_h.transform_keys(&:to_s)].freeze
      end
    end

    private

    def validate_params!
      raise InvalidSchemaError unless schema.is_a?(::Dry::Schema::Params)
      raise InvalidEnvError unless env.is_a?(::Enumerable)
    end

    def extract_env_vars
      keys = @schema.key_map.map { |key| key.name }
      @env.select { |name, value| keys.include?(name) }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
envv-0.2.1 lib/envv/builder.rb
envv-0.2.0 lib/envv/builder.rb
envv-0.1.2 lib/envv/builder.rb
envv-0.1.1 lib/envv/builder.rb
envv-0.1.0 lib/envv/builder.rb