Class: Enumerator::IO::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/enumerator/io/reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(enumerator, &block) ⇒ Reader

Returns a new instance of Reader.



4
5
6
7
8
9
10
# File 'lib/enumerator/io/reader.rb', line 4

def initialize(enumerator, &block)
  @enumerator = enumerator
  @reader, @writer = ::IO.pipe
  @end_of_read_window = 0
  @read_bytes = 0
  @block = block
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/enumerator/io/reader.rb', line 27

def eof?
  moar_data if @read_bytes == 0
  @reader.eof?
end

#read(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/enumerator/io/reader.rb', line 12

def read(*args)
  length = args.first
  if length
    required_read_window = @read_bytes + length 
    if required_read_window > @end_of_read_window
      increase_read_window!(required_read_window - @end_of_read_window)
    end
  else
    slurp!
  end
  result = @reader.read(*args)
  @read_bytes += result.size if result
  result
end