Sha256: 0eac2d47a20ea9695a6d207bf79cb037126642c7c430c355677c92b55e32c0df

Contents?: true

Size: 1.48 KB

Versions: 2

Compression:

Stored size: 1.48 KB

Contents

require "climate_control/errors"
require "climate_control/version"
require "monitor"

module ClimateControl
  extend self
  extend Gem::Deprecate

  SEMAPHORE = Monitor.new
  private_constant :SEMAPHORE

  def modify(environment_overrides = {}, &block)
    environment_overrides = environment_overrides.transform_keys(&:to_s)

    SEMAPHORE.synchronize do
      previous = ENV.to_hash

      begin
        copy environment_overrides
      ensure
        middle = ENV.to_hash
      end

      block.call
    ensure
      after = ENV
      (previous.keys | middle.keys | after.keys).each do |key|
        if previous[key] != after[key] && middle[key] == after[key]
          ENV[key] = previous[key]
        end
      end
    end
  end

  def unsafe_modify(environment_overrides = {}, &block)
    environment_overrides = environment_overrides.transform_keys(&:to_s)

    previous = ENV.to_hash

    begin
      copy environment_overrides
    ensure
      middle = ENV.to_hash
    end

    block.call
  ensure
    after = ENV
    (previous.keys | middle.keys | after.keys).each do |key|
      if previous[key] != after[key] && middle[key] == after[key]
        ENV[key] = previous[key]
      end
    end
  end

  def env
    ENV
  end

  deprecate :env, "ENV", 2022, 10

  private

  def copy(overrides)
    overrides.each do |key, value|
      ENV[key] = value
    rescue TypeError => e
      raise UnassignableValueError,
        "attempted to assign #{value} to #{key} but failed (#{e.message})"
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/climate_control-1.2.0/lib/climate_control.rb
climate_control-1.2.0 lib/climate_control.rb