Class: RMARC::MarcXmlReader

Inherits:
Object
  • Object
show all
Defined in:
lib/rmarc/marc_xml_reader.rb

Overview

This class provides an iterator over a collection of MARC XML records.

Example usage:

reader = RMARC::MarcXmlReader.new("file.xml")
while reader.has_next
    record = reader.next()
end

You can also pass a file object:

input = File.new("file.mrc")
reader = RMARC::MarcXmlReader.new(input)
while reader.has_next
    record = reader.next()
end
input.close

Or any other IO subclass.

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ MarcXmlReader

Default constructor



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rmarc/marc_xml_reader.rb', line 115

def initialize(input)
  if input.class == String
    @input = File.new(input)
  elsif input.class == File
    @input = input
  else
    throw "Unable to read from path or file"                
  end
  @queue = RecordStack.new(1)
  Thread.new do
    producer = Listener.new(@queue)
    REXML::Document.parse_stream(@input, producer)
  end
end

Instance Method Details

#has_nextObject

Returns true if the iteration has more records, false otherwise.



131
132
133
134
135
136
137
# File 'lib/rmarc/marc_xml_reader.rb', line 131

def has_next
  if (@queue.has_next == false && @queue.empty?) 
    return false
  else
    return true
  end
end

#nextObject

Returns the next record in the iteration.



140
141
142
143
# File 'lib/rmarc/marc_xml_reader.rb', line 140

def next
  obj = @queue.pop
  return obj
end