Class: AutomationObject::Proxy::MutexProxy
- Inherits:
-
Proxy
- Object
- Proxy
- AutomationObject::Proxy::MutexProxy
show all
- Defined in:
- lib/automation_object/proxy/mutex_proxy.rb
Overview
Proxy class for protecting object with Mutex
Instance Method Summary
collapse
Constructor Details
#initialize(subject) ⇒ MutexProxy
Returns a new instance of MutexProxy
9
10
11
12
13
14
|
# File 'lib/automation_object/proxy/mutex_proxy.rb', line 9
def initialize(subject)
super
@mutexes = [Mutex.new]
@skip_protection_classes = [TrueClass, FalseClass, String, Numeric, Array, Hash, Class, NilClass, Symbol]
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_symbol, *args, &block) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/automation_object/proxy/mutex_proxy.rb', line 28
def method_missing(method_symbol, *args, &block)
exec_procedures = []
exec_procedures.push(lambda do
execution_return = @subject.send(method_symbol, *args, &block)
return protect_object(execution_return)
end)
index = 0
@mutexes.each do |mutex|
index += 1
exec_procedures.push(lambda do
mutex.synchronize do
index -= 1
exec_procedures[index].call
end
end)
end
exec_procedures.last.call
end
|
Instance Method Details
#add_mutex(mutex_object) ⇒ Object
16
17
18
19
20
|
# File 'lib/automation_object/proxy/mutex_proxy.rb', line 16
def add_mutex(mutex_object)
raise ArgumentError, 'Expecting mutex_object argument to be a Mutex object' unless mutex_object.is_a?(Mutex)
@mutexes << mutex_object
end
|
#delete_mutex(mutex_object) ⇒ Object
22
23
24
25
26
|
# File 'lib/automation_object/proxy/mutex_proxy.rb', line 22
def delete_mutex(mutex_object)
raise ArgumentError, 'Expecting mutex_object argument to be a Mutex object' unless mutex_object.is_a?(Mutex)
@mutexes.delete(mutex_object)
end
|
#protect_object(object) ⇒ Object
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/automation_object/proxy/mutex_proxy.rb', line 49
def protect_object(object)
return object if @skip_protection_classes.include?(object.class)
protected_object = MutexProxy.new(object)
@mutexes.each do |mutex|
protected_object.add_mutex(mutex)
end
protected_object
end
|