Module: CouchRest::RestAPI

Defined in:
lib/vines/services/core_ext/couchrest.rb

Class Method Summary collapse

Class Method Details

.defer(method) ⇒ Object

Wrap a blocking IO method in a new method that pushes the original method onto EventMachine’s thread pool using EM#defer. The calling Fiber is paused while the thread pool does its work, then resumed when the thread finishes.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vines/services/core_ext/couchrest.rb', line 9

def self.defer(method)
  old = "_deferred_#{method}"
  alias_method old, method
  define_method method do |*args|
    fiber = Fiber.current
    op = proc do
      begin
        method(old).call(*args)
      rescue
        nil
      end
    end
    cb = proc {|result| fiber.resume(result) }
    EM.defer(op, cb)
    Fiber.yield
  end
end