Sha256: 796b2ad0b01302ca7fcbee6a97d470fc14d1b84b4e643f80a236f7a172eb1a57

Contents?: true

Size: 667 Bytes

Versions: 23

Compression:

Stored size: 667 Bytes

Contents

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

23 entries across 23 versions & 1 rubygems

Version Path
mongo-2.13.3 spec/support/local_resource_registry.rb
mongo-2.14.1 spec/support/local_resource_registry.rb
mongo-2.15.0.alpha spec/support/local_resource_registry.rb
mongo-2.13.2 spec/support/local_resource_registry.rb
mongo-2.14.0 spec/support/local_resource_registry.rb
mongo-2.14.0.rc1 spec/support/local_resource_registry.rb
mongo-2.13.1 spec/support/local_resource_registry.rb
mongo-2.12.4 spec/support/local_resource_registry.rb
mongo-2.11.6 spec/support/local_resource_registry.rb
mongo-2.13.0 spec/support/local_resource_registry.rb
mongo-2.12.3 spec/support/local_resource_registry.rb
mongo-2.13.0.rc1 spec/support/local_resource_registry.rb
mongo-2.12.2 spec/support/local_resource_registry.rb
mongo-2.11.5 spec/support/local_resource_registry.rb
mongo-2.13.0.beta1 spec/support/local_resource_registry.rb
mongo-2.12.1 spec/support/local_resource_registry.rb
mongo-2.12.0.rc0 spec/support/local_resource_registry.rb
mongo-2.11.4 spec/support/local_resource_registry.rb
mongo-2.11.3 spec/support/local_resource_registry.rb
mongo-2.11.2 spec/support/local_resource_registry.rb