Sha256: 467e1aa45ea038ddf9504a15769ca8ac6abffe8804be1778896364eb5cfb0adc

Contents?: true

Size: 842 Bytes

Versions: 16

Compression:

Stored size: 842 Bytes

Contents

# frozen_string_literal: true

module Shimmer
  class Config
    include Singleton
    class MissingConfigError < StandardError; end

    def method_missing(method_name)
      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.send(key)
      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.in?(["n", "0", "no", "false"]) if type == :bool && value.is_a?(String)

      value
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
shimmer-0.0.28 lib/shimmer/utils/config.rb
shimmer-0.0.27 lib/shimmer/utils/config.rb
shimmer-0.0.26 lib/shimmer/utils/config.rb
shimmer-0.0.25 lib/shimmer/utils/config.rb
shimmer-0.0.24 lib/shimmer/utils/config.rb
shimmer-0.0.23 lib/shimmer/utils/config.rb
shimmer-0.0.22 lib/shimmer/utils/config.rb
shimmer-0.0.21 lib/shimmer/utils/config.rb
shimmer-0.0.20 lib/shimmer/utils/config.rb
shimmer-0.0.19 lib/shimmer/utils/config.rb
shimmer-0.0.18 lib/shimmer/utils/config.rb
shimmer-0.0.17 lib/shimmer/utils/config.rb
shimmer-0.0.16 lib/shimmer/utils/config.rb
shimmer-0.0.15 lib/shimmer/utils/config.rb
shimmer-0.0.14 lib/shimmer/utils/config.rb
shimmer-0.0.13 lib/shimmer/utils/config.rb