Class: ZeevexCluster::Synchronized

Inherits:
ZeevexProxy::Base
  • Object
show all
Defined in:
lib/zeevex_cluster/synchronized.rb

Overview

Wraps an object, synchronizes all method calls The wrapped object can also be set and read out

which means this can also be used as a thread-safe reference
(like a 'volatile' variable in Java)

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Synchronized

Returns a new instance of Synchronized.



11
12
13
14
# File 'lib/zeevex_cluster/synchronized.rb', line 11

def initialize(obj)
  super
  @mutex = ::Mutex.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



31
32
33
34
# File 'lib/zeevex_cluster/synchronized.rb', line 31

def method_missing(method, *args, &block)
  result = @mutex.synchronize { @obj.__send__(method, *args, &block) }
  # result.__id__ == @obj.__id__ ? self : result
end

Instance Method Details

#_get_synchronized_objectObject



19
20
21
# File 'lib/zeevex_cluster/synchronized.rb', line 19

def _get_synchronized_object
  @mutex.synchronize { @obj }
end

#_set_synchronized_object(val) ⇒ Object



16
17
18
# File 'lib/zeevex_cluster/synchronized.rb', line 16

def _set_synchronized_object(val)
  @mutex.synchronize { @obj = val }
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
# File 'lib/zeevex_cluster/synchronized.rb', line 23

def respond_to?(method)
  if [:_set_synchronized_object, :_get_synchronized_object].include?(method.to_sym)
    true
  else
    @obj.respond_to?(method)
  end
end