Sha256: d59fd0b604cae72b3e6020ff524724f7b72d9f9d541aba28201348597c08e674

Contents?: true

Size: 522 Bytes

Versions: 2

Compression:

Stored size: 522 Bytes

Contents

require "forwardable"

module ClimateControl
  class Environment
    extend Forwardable

    def initialize
      @semaphore = Mutex.new
      @owner = nil
    end

    def_delegators :env, :[]=, :to_hash, :[], :delete

    def synchronize
      if @owner == Thread.current
        return yield if block_given?
      end

      @semaphore.synchronize do
        @owner = Thread.current
        yield if block_given?
      ensure
        @owner = nil
      end
    end

    private

    def env
      ENV
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
climate_control-1.0.1 lib/climate_control/environment.rb
climate_control-1.0.0 lib/climate_control/environment.rb