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.



18
19
20
# File 'lib/marc_alephsequential/reader.rb', line 18

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.



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

def current_id
  @current_id
end

#linesObject (readonly)

Returns the value of attribute lines.



15
16
17
# File 'lib/marc_alephsequential/reader.rb', line 15

def lines
  @lines
end

Instance Method Details

#each {|agroup.to_record| ... } ⇒ Object

Yields:

  • (agroup.to_record)


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/reader.rb', line 22

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? 
      yield agroup.to_record unless agroup.empty?
      agroup = ASLineGroup.new
      @current_id = nextid
    else
      agroup.add @areader.next
    end
  end
  # yield whatever is left, unless there's nothing left
  yield agroup.to_record unless agroup.empty?
end