Method: Vines::Storage.fiber
- Defined in:
- lib/vines/storage.rb
.fiber(method) ⇒ Object
Wrap a method with Fiber yield and resume logic. The method must yield its result to a block. This makes it easier to write asynchronous implementations of authenticate, find_user, and save_user that block and return a result rather than yielding.
For example: def find_user(jid)
http = EM::HttpRequest.new(url).get
http.callback { yield build_user_from_http_response(http) }
end fiber :find_user
Because find_user has been wrapped in Fiber logic, we can call it synchronously even though it uses asynchronous EventMachine calls.
user = storage.find_user(‘[email protected]’) puts user.nil?
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/vines/storage.rb', line 63 def self.fiber(method) old = instance_method(method) define_method method do |*args| fiber, yielding = Fiber.current, true old.bind(self).call(*args) do |user| fiber.resume(user) rescue yielding = false end Fiber.yield if yielding end end |