Class: SyncEm

Inherits:
Object
  • Object
show all
Defined in:
lib/syncem.rb

Overview

SyncEm is a simple decorator of an existing object that makes all its methods thread-safe.

For more information read README file.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2018-2019 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(origin) ⇒ SyncEm

Returns a new instance of SyncEm.



37
38
39
40
# File 'lib/syncem.rb', line 37

def initialize(origin)
  @origin = origin
  @mutex = Mutex.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/syncem.rb', line 42

def method_missing(*args)
  @mutex.synchronize do
    if @origin.respond_to?(args[0])
      @origin.__send__(*args) do |*a|
        yield(*a) if block_given?
      end
    else
      super
    end
  end
end

Instance Method Details

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/syncem.rb', line 54

def respond_to?(method, include_private = false)
  @origin.respond_to?(method, include_private)
end

#respond_to_missing?(_method, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/syncem.rb', line 58

def respond_to_missing?(_method, _include_private = false)
  true
end