Class: MARC::AlephSequential::Reader

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Log
Defined in:
lib/marc_alephsequential/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

log, #log, log=

Constructor Details

#initialize(filename_or_io, opts = {}) ⇒ Reader

Returns a new instance of Reader.



34
35
36
# File 'lib/marc_alephsequential/reader.rb', line 34

def initialize(filename_or_io, opts={})
  @areader = ASLineReader.new(filename_or_io)
end

Instance Attribute Details

#current_idObject

Returns the value of attribute current_id.



32
33
34
# File 'lib/marc_alephsequential/reader.rb', line 32

def current_id
  @current_id
end

#linesObject (readonly)

Returns the value of attribute lines.



31
32
33
# File 'lib/marc_alephsequential/reader.rb', line 31

def lines
  @lines
end

Instance Method Details

#eachObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/marc_alephsequential/reader.rb', line 38

def each

  unless block_given?
    return enum_for(:each)
  end

  agroup = ASLineGroup.new

  while @areader.has_next?
    nextid = @areader.peek.id
    if nextid != @current_id && @areader.peek.valid_id?
      begin
        yield agroup.to_record unless agroup.empty?
      rescue RuntimeError => e
        yield ErrorRecord.new(e)
      end
      agroup      = ASLineGroup.new
      @current_id = nextid
    else
      agroup.add @areader.next
    end
  end
  # yield whatever is left, unless there's nothing left
  begin
    yield agroup.to_record unless agroup.empty?
  rescue RuntimeError => e
    yield ErrorRecord.new(e)
  end

end