module MARC::NokogiriReader

NokogiriReader uses the Nokogiri SAX Parser to quickly read a MARCXML document. Because dynamically subclassing MARC::XMLReader is a little ugly, we need to recreate all of the SAX event methods from Nokogiri::XML::SAX::Document here rather than subclassing.

Public Class Methods

extended(receiver) click to toggle source
# File lib/marc/xml_parsers.rb, line 93
def self.extended(receiver)
  require 'nokogiri'
  receiver.init
end

Public Instance Methods

each(&block) click to toggle source

Loop through the MARC records in the XML document

# File lib/marc/xml_parsers.rb, line 107
def each(&block)    
  @block = block
  @parser.parse(@handle)
end
init() click to toggle source

Sets our instance variables for SAX parsing in Nokogiri and parser

# File lib/marc/xml_parsers.rb, line 99
def init
  @record = {:record=>nil,:field=>nil,:subfield=>nil}
  @current_element = nil
  @ns = "http://www.loc.gov/MARC21/slim"
  @parser = Nokogiri::XML::SAX::Parser.new(self)         
end
method_missing(methName, *args) click to toggle source
# File lib/marc/xml_parsers.rb, line 113
def method_missing(methName, *args)
  sax_methods = [:xmldecl, :start_document, :end_document, :start_element,
    :end_element, :comment, :warning, :error, :cdata_block]
  unless sax_methods.index(methName)
    raise NoMethodError.new("undefined method '#{methName} for #{self}", 'no_meth')
  end
end

Private Instance Methods

attributes_to_hash(attributes) click to toggle source
# File lib/marc/xml_parsers.rb, line 123
def attributes_to_hash(attributes)
  hash = {}
  attributes.each do | att |
    hash[att.localname] = att.value
  end
  hash
end