Sha256: cf8dd6f3a8393f7943fd1984bf7aa71bbdfc051838fe2bf66b9e008481bffb17

Contents?: true

Size: 927 Bytes

Versions: 6

Compression:

Stored size: 927 Bytes

Contents

require 'active_support/string_inquirer'

module BerkeleyLibrary
  module Logging
    class << self
      FALLBACK_ENV = 'development'.freeze
      ENV_PREDICATES = %i[production? test? development?].freeze
      private_constant :ENV_PREDICATES

      def env
        return Rails.env if defined?(Rails)

        @env ||= begin
          # Note: can't just self.env= b/c it returns the wrong value -- see
          # https://stackoverflow.com/q/65226532/27358
          env = (ENV['RAILS_ENV'] || ENV['RACK_ENV'] || FALLBACK_ENV)
          ensure_rails_env_like(env)
        end
      end

      def env=(v)
        if defined?(Rails)
          Rails.env = v
        else
          @env = ensure_rails_env_like(v)
        end
      end

      private

      def ensure_rails_env_like(v)
        return v if ENV_PREDICATES.all? { |p| v.respond_to?(p) }

        ActiveSupport::StringInquirer.new(v)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
berkeley_library-logging-0.2.5 lib/berkeley_library/logging/env.rb
berkeley_library-logging-0.2.4 lib/berkeley_library/logging/env.rb
berkeley_library-logging-0.2.3 lib/berkeley_library/logging/env.rb
berkeley_library-logging-0.2.2 lib/berkeley_library/logging/env.rb
berkeley_library-logging-0.2.1 lib/berkeley_library/logging/env.rb
berkeley_library-logging-0.2.0 lib/berkeley_library/logging/env.rb