Sha256: d28b16aabfae407d6a84d96021293ea9c1a1d7d6165ad0e15a35802eb8ff3926

Contents?: true

Size: 566 Bytes

Versions: 1

Compression:

Stored size: 566 Bytes

Contents

require 'environs/missing_env_var_error'

class Env
  class << self
    def method_missing(method, *args, &block)
      key = method.to_s.upcase
      key.match(/\w+/) ? env_key(key, *args) : super
    end

    private

    def env_key(key, nil_allowance = {})
      ENV.fetch(key) { |k| key_not_found(nil_allowance, key) }
    end

    def key_not_found(nil_allowance, key)
      nil_allowance[:allow_nil] ? nil : env_var_error(key)
    end

    def env_var_error(key)
      raise MissingEnvVarError, "The #{key} environment variable is not set."
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
environs-1.1.0 lib/environs/env.rb