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.0.35 lib/karafka/env.rb
karafka-2.0.34 lib/karafka/env.rb
karafka-2.0.33 lib/karafka/env.rb
karafka-2.0.32 lib/karafka/env.rb
karafka-2.0.31 lib/karafka/env.rb
karafka-2.0.30 lib/karafka/env.rb
karafka-2.0.29 lib/karafka/env.rb
karafka-2.0.28 lib/karafka/env.rb
karafka-2.0.27 lib/karafka/env.rb
karafka-2.0.26 lib/karafka/env.rb
karafka-2.0.24 lib/karafka/env.rb
karafka-2.0.23 lib/karafka/env.rb
karafka-2.0.22 lib/karafka/env.rb
karafka-2.0.21 lib/karafka/env.rb
karafka-2.0.20 lib/karafka/env.rb
karafka-2.0.19 lib/karafka/env.rb
karafka-2.0.18 lib/karafka/env.rb
karafka-2.0.17 lib/karafka/env.rb
karafka-2.0.16 lib/karafka/env.rb
karafka-2.0.15 lib/karafka/env.rb