Sha256: fdb65c5969f298431bcea802d47b665d8b2fa7ddd2a81b9beff7121286a8937f

Contents?: true

Size: 1.13 KB

Versions: 7

Compression:

Stored size: 1.13 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, include_all)
      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

7 entries across 7 versions & 1 rubygems

Version Path
shimmer-0.0.39 lib/shimmer/utils/config.rb
shimmer-0.0.38 lib/shimmer/utils/config.rb
shimmer-0.0.37 lib/shimmer/utils/config.rb
shimmer-0.0.36 lib/shimmer/utils/config.rb
shimmer-0.0.35 lib/shimmer/utils/config.rb
shimmer-0.0.34 lib/shimmer/utils/config.rb
shimmer-0.0.33 lib/shimmer/utils/config.rb