Sha256: 78767b262b74a2ee30eb7ed6c1e4db3fea81ec8d26ce72ddb00a1c7de2e7b0c2

Contents?: true

Size: 716 Bytes

Versions: 18

Compression:

Stored size: 716 Bytes

Contents

# frozen_string_literal: true
# encoding: utf-8

require 'singleton'

class LocalResourceRegistry
  include Singleton

  def initialize
    @resources = []
  end

  def register(resource, finalizer)
    @resources << [resource, finalizer]
    # Return resource for chaining
    resource
  end

  def unregister(resource)
    @resources.delete_if do |_resource, finalizer|
      _resource == resource
    end
  end

  def close_all
    @resources.each do |resource, finalizer|
      if finalizer.is_a?(Symbol)
        resource.send(finalizer)
      elsif finalizer.is_a?(Proc)
        finalizer.call(resource)
      else
        raise "Unknown finalizer: #{finalizer}"
      end
    end
    @resources = []
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
mongo-2.16.4 spec/support/local_resource_registry.rb
mongo-2.17.4 spec/support/local_resource_registry.rb
mongo-2.18.3 spec/support/local_resource_registry.rb
mongo-2.18.2 spec/support/local_resource_registry.rb
mongo-2.17.3 spec/support/local_resource_registry.rb
mongo-2.18.1 spec/support/local_resource_registry.rb
mongo-2.18.0 spec/support/local_resource_registry.rb
mongo-2.17.2 spec/support/local_resource_registry.rb
mongo-2.16.3 spec/support/local_resource_registry.rb
mongo-2.18.0.beta1 spec/support/local_resource_registry.rb
mongo-2.16.2 spec/support/local_resource_registry.rb
mongo-2.17.1 spec/support/local_resource_registry.rb
mongo-2.16.1 spec/support/local_resource_registry.rb
mongo-2.17.0 spec/support/local_resource_registry.rb
mongo-2.16.0 spec/support/local_resource_registry.rb
mongo-2.15.1 spec/support/local_resource_registry.rb
mongo-2.16.0.alpha1 spec/support/local_resource_registry.rb
mongo-2.15.0 spec/support/local_resource_registry.rb