Class: MARC::AlephSequential::BufferedLineReader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/marc_alephsequential/buffered_linereader.rb

Overview

AlephSequential is a line-oriented format, with the first field of each line indicating the record number. Rather than try to screw around with keeping track of the last line read, checking to see if we have one, blah blah blah, I'm going to use a buffered line reader class so I can #peek at the next line to know if its id is different than the current record.

Direct Known Subclasses

ASLineReader

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename_or_io) ⇒ BufferedLineReader

Returns a new instance of BufferedLineReader.


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/marc_alephsequential/buffered_linereader.rb', line 19

def initialize(filename_or_io)

  @passed_in = filename_or_io

  @underlying_line_number = 0
  @buffer_size            = 10
  @buffer                 = []

  if filename_or_io.is_a? String
    @handle = File.open(filename_or_io, 'r:utf-8')
    if filename_or_io =~ /\.gz$/
      @handle = Zlib::GzipReader.new(@handle)
    end
  elsif filename_or_io.respond_to?("read")
    @handle = filename_or_io
  else
    raise ArgumentError.new("BufferedLineReader needs an IO object or filename, got #{filename_or_io} (#{filename_or_io.inspect})")
  end

  @iter     = @handle.enum_for(:each_line)
  @finished = false  # Fill up the buffer

  self.fillbuffer
end

Instance Attribute Details

#buffer_sizeObject

Returns the value of attribute buffer_size


16
17
18
# File 'lib/marc_alephsequential/buffered_linereader.rb', line 16

def buffer_size
  @buffer_size
end

#underlying_line_numberObject (readonly)

Returns the value of attribute underlying_line_number


17
18
19
# File 'lib/marc_alephsequential/buffered_linereader.rb', line 17

def underlying_line_number
  @underlying_line_number
end

Instance Method Details

#eachObject Also known as: each_line


78
79
80
81
82
83
84
85
# File 'lib/marc_alephsequential/buffered_linereader.rb', line 78

def each
  begin
    while true
      yield self.next
    end
  rescue StopIteration
  end
end

#fillbuffer(buffer_size = @buffer_size) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/marc_alephsequential/buffered_linereader.rb', line 48

def fillbuffer(buffer_size = @buffer_size)
  begin
    buffer_size.times do
      raw                     = @iter.next
      @underlying_line_number += 1
      @buffer.push process_raw(raw, @underlying_line_number)
    end
  rescue StopIteration
    @finished = true
  end
end

#has_next?Boolean

Returns:

  • (Boolean)

44
45
46
# File 'lib/marc_alephsequential/buffered_linereader.rb', line 44

def has_next?
  return !(@finished && @buffer.size == 0)
end

#nextObject

Raises:

  • (StopIteration)

65
66
67
68
69
70
# File 'lib/marc_alephsequential/buffered_linereader.rb', line 65

def next
  raise StopIteration, "End of #{@passed_in}", nil if @buffer.size == 0
  rv = @buffer.shift
  fillbuffer if @buffer.size == 0
  rv
end

#peekObject


73
74
75
76
# File 'lib/marc_alephsequential/buffered_linereader.rb', line 73

def peek
  fillbuffer unless @buffer.size > 0
  @buffer[0]
end

#process_raw(raw, line_number) ⇒ Object

Empty version here; can override for processing lines on the fly


61
62
63
# File 'lib/marc_alephsequential/buffered_linereader.rb', line 61

def process_raw(raw, line_number)
  raw
end