Sha256: 7c80157166da7eb037f8ad61d9436acb1ffa8d36aca62243732dc55f2451f282

Contents?: true

Size: 1.43 KB

Versions: 112

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module Karafka
  # Env management class to get and set environment for Karafka
  class Env < String
    # Keys where we look for environment details for Karafka
    LOOKUP_ENV_KEYS = %w[
      KARAFKA_ENV
      RACK_ENV
      RAILS_ENV
    ].freeze

    # Default fallback env
    DEFAULT_ENV = 'development'

    private_constant :LOOKUP_ENV_KEYS, :DEFAULT_ENV

    # @return [Karafka::Env] env object
    # @note Will load appropriate environment automatically
    def initialize
      super('')

      LOOKUP_ENV_KEYS
        .map { |key| ENV[key] }
        .compact
        .first
        .then { |env| env || DEFAULT_ENV }
        .then { |env| replace(env) }
    end

    # @param method_name [String] method name
    # @param include_private [Boolean] should we include private methods as well
    # @return [Boolean] true if we respond to a given missing method, otherwise false
    def respond_to_missing?(method_name, include_private = false)
      (method_name[-1] == '?') || super
    end

    # Reacts to missing methods, from which some might be the env checks.
    # If the method ends with '?' we assume, that it is an env check
    # @param method_name [String] method name for missing or env name with question mark
    # @param arguments [Array] any arguments that we pass to the method
    def method_missing(method_name, *arguments)
      method_name[-1] == '?' ? self == method_name[0..-2] : super
    end
  end
end

Version data entries

112 entries across 112 versions & 1 rubygems

Version Path
karafka-2.4.13 lib/karafka/env.rb
karafka-2.4.12 lib/karafka/env.rb
karafka-2.4.11 lib/karafka/env.rb
karafka-2.4.10 lib/karafka/env.rb
karafka-2.4.9 lib/karafka/env.rb
karafka-2.4.8 lib/karafka/env.rb
karafka-2.4.7 lib/karafka/env.rb
karafka-2.4.6 lib/karafka/env.rb
karafka-2.4.5 lib/karafka/env.rb
karafka-2.4.4 lib/karafka/env.rb
karafka-2.4.3 lib/karafka/env.rb
karafka-2.4.0 lib/karafka/env.rb
karafka-2.4.0.rc1 lib/karafka/env.rb
karafka-2.3.4 lib/karafka/env.rb
karafka-2.4.0.beta2 lib/karafka/env.rb
karafka-2.4.0.beta1 lib/karafka/env.rb
karafka-2.3.3 lib/karafka/env.rb
karafka-2.3.2 lib/karafka/env.rb
karafka-2.3.1 lib/karafka/env.rb
karafka-2.3.0 lib/karafka/env.rb