Class: Kward::PluginDisposable
- Inherits:
-
Object
- Object
- Kward::PluginDisposable
- Defined in:
- lib/kward/plugins/resources.rb
Overview
Idempotent cleanup registration owned by a plugin runtime.
Instance Method Summary collapse
- #dispose ⇒ Object
- #disposed? ⇒ Boolean
-
#initialize(resource = nil, &cleanup) ⇒ PluginDisposable
constructor
A new instance of PluginDisposable.
Constructor Details
#initialize(resource = nil, &cleanup) ⇒ PluginDisposable
Returns a new instance of PluginDisposable.
8 9 10 11 12 13 14 15 |
# File 'lib/kward/plugins/resources.rb', line 8 def initialize(resource = nil, &cleanup) raise ArgumentError, "plugin cleanup requires a block or closeable resource" unless cleanup || resource&.respond_to?(:close) @resource = resource @cleanup = cleanup @mutex = Mutex.new @disposed = false end |
Instance Method Details
#dispose ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/kward/plugins/resources.rb', line 21 def dispose cleanup = @mutex.synchronize do return false if @disposed @disposed = true @cleanup end if cleanup cleanup.arity.zero? ? cleanup.call : cleanup.call(@resource) else @resource.close end true end |
#disposed? ⇒ Boolean
17 18 19 |
# File 'lib/kward/plugins/resources.rb', line 17 def disposed? @mutex.synchronize { @disposed } end |