Sha256: 0b1d25c0e88673ccb9d50fe4d897c6c47f438bced11cdc5f218e64264fd459c5

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module Shimmer
  class Config
    include Singleton

    class MissingConfigError < StandardError; end

    def method_missing(method_name, **options)
      default_provided = options.key?(:default)
      default_value = options.delete(:default) if default_provided
      raise ArgumentError, "unknown option#{"s" if options.length > 1}: #{options.keys.join(", ")}." if options.any?

      method_name = method_name.to_s
      type = :string
      key = method_name.delete_suffix("!").delete_suffix("?")
      required = method_name.end_with?("!")
      type = :bool if method_name.end_with?("?")
      value = ENV[key.upcase].presence
      value ||= Rails.application.credentials.public_send(key)
      value = default_value if value.nil?
      raise MissingConfigError, "#{key.upcase} environment value is missing" if required && value.blank?

      coerce value, type
    end

    def respond_to_missing?(method_name)
      true
    end

    private

    def coerce(value, type)
      return !value.downcase.in?(["n", "0", "no", "false"]) if type == :bool && value.is_a?(String)

      value
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
shimmer-0.0.32 lib/shimmer/utils/config.rb
shimmer-0.0.31 lib/shimmer/utils/config.rb
shimmer-0.0.30 lib/shimmer/utils/config.rb
shimmer-0.0.29 lib/shimmer/utils/config.rb