Class: LongBody::ThinHandler::FiberWrapper

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

Overview

A wrapper that allows us to access an object that yields from each() as if it were an Enumerator-ish object.

arr = %w( a b )
w = FiberWrapper.new(arr)
w.take #=> 'a'
w.take #=> 'b'
w.take #=> nil # Ended

Instance Method Summary collapse

Constructor Details

#initialize(eachable) ⇒ FiberWrapper

Returns a new instance of FiberWrapper.



18
19
20
21
22
23
24
# File 'lib/long_body/thin_handler.rb', line 18

def initialize(eachable)
  @fiber = Fiber.new do
    eachable.each{|chunk| Fiber.yield(chunk.to_s) }
    eachable.close if eachable.respond_to?(:close)
    nil
  end
end

Instance Method Details

#takeObject



26
27
28
29
30
# File 'lib/long_body/thin_handler.rb', line 26

def take
  @fiber.resume
rescue FiberError
  nil
end