Module: MARC::NokogiriReader

Includes:
GenericPullParser
Defined in:
lib/marc/xml_parsers.rb

Overview

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.

Constant Summary collapse

SAX_METHODS =
[:xmldecl, :start_document, :end_document, :start_element,
:end_element, :comment, :warning, :error, :cdata_block, :processing_instruction]

Constants included from GenericPullParser

GenericPullParser::CF_TAG, GenericPullParser::DF_TAG, GenericPullParser::LEAD_TAG, GenericPullParser::REC_TAG, GenericPullParser::SF_TAG

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GenericPullParser

#characters, #end_element_namespace, #start_element_namespace, #yield_record

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



160
161
162
163
164
# File 'lib/marc/xml_parsers.rb', line 160

def method_missing(method_name, *args)
  unless SAX_METHODS.include?(method_name)
    raise NoMethodError.new("undefined method '#{method_name} for #{self}", "no_meth")
  end
end

Class Method Details

.extended(receiver) ⇒ Object



132
133
134
135
# File 'lib/marc/xml_parsers.rb', line 132

def self.extended(receiver)
  require "nokogiri"
  receiver.init
end

Instance Method Details

#each(&block) ⇒ Object

Loop through the MARC records in the XML document



144
145
146
147
148
149
150
151
# File 'lib/marc/xml_parsers.rb', line 144

def each(&block)
  if block
    @block = block
    @parser.parse(@handle)
  else
    enum_for(:each)
  end
end

#error(evt) ⇒ Object

Raises:



153
154
155
# File 'lib/marc/xml_parsers.rb', line 153

def error(evt)
  raise(XMLParseError, "XML parsing error: #{evt}")
end

#initObject

Sets our instance variables for SAX parsing in Nokogiri and parser



138
139
140
141
# File 'lib/marc/xml_parsers.rb', line 138

def init
  super
  @parser = Nokogiri::XML::SAX::Parser.new(self)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/marc/xml_parsers.rb', line 166

def respond_to_missing?(method_name, include_private = false)
  SAX_METHODS.include?(method_name) || super
end