class AutomationObject::Proxy::MutexProxy

Proxy class for protecting object with Mutex

Public Class Methods

new(subject) click to toggle source
Calls superclass method
# 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

Public Instance Methods

add_mutex(mutex_object) click to toggle source
# 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) click to toggle source
# 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
method_missing(method_symbol, *args, &block) click to toggle source
# 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
protect_object(object) click to toggle source
# 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