Class: CemAcpt::Utils::FinalizerQueue
- Inherits:
-
Object
- Object
- CemAcpt::Utils::FinalizerQueue
show all
- Defined in:
- lib/cem_acpt/utils/finalizer_queue.rb
Overview
A queue that can be finalized. When a queue is finalized, no more items can be added to it, and the queue is closed and converted to a frozen array.
Instance Method Summary
collapse
Constructor Details
Returns a new instance of FinalizerQueue.
11
12
13
14
15
16
|
# File 'lib/cem_acpt/utils/finalizer_queue.rb', line 11
def initialize
@queue = Queue.new
@array = []
@finalized = false
@mutex = Mutex.new
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, **kwargs, &block) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/cem_acpt/utils/finalizer_queue.rb', line 35
def method_missing(method_name, *args, **kwargs, &block)
if finalized?
@array.send(method_name, *args, **kwargs, &block)
elsif @queue.respond_to?(method_name)
@queue.send(method_name, *args, **kwargs, &block)
else
super
end
rescue StandardError => e
raise e if e.is_a?(NoMethodError) || e.is_a?(FinalizerQueueError)
new_err = FinalizerQueueError.new("Error calling #{method_name} on FinalizerQueue: #{e}")
new_err.set_backtrace(e.backtrace)
raise new_err
end
|
Instance Method Details
#finalize! ⇒ Object
18
19
20
21
22
23
|
# File 'lib/cem_acpt/utils/finalizer_queue.rb', line 18
def finalize!
return if finalized?
@finalized = true
new_array
end
|
#finalized? ⇒ Boolean
25
26
27
|
# File 'lib/cem_acpt/utils/finalizer_queue.rb', line 25
def finalized?
@finalized
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
51
52
53
54
55
|
# File 'lib/cem_acpt/utils/finalizer_queue.rb', line 51
def respond_to_missing?(method_name, include_private = false)
@array.respond_to?(method_name, include_private) if finalized?
super
end
|
#to_a ⇒ Object
29
30
31
32
33
|
# File 'lib/cem_acpt/utils/finalizer_queue.rb', line 29
def to_a
raise FinalizerQueueError, 'Cannot convert to array until finalized' unless finalized?
@array
end
|