Class: BerkeleyLibrary::TIND::MARC::XMLWriter
- Inherits:
-
Object
- Object
- BerkeleyLibrary::TIND::MARC::XMLWriter
- Includes:
- Logging, Util::Files
- Defined in:
- lib/berkeley_library/tind/marc/xml_writer.rb
Constant Summary collapse
- UTF_8 =
Constants
Encoding::UTF_8.name
- EMPTY_COLLECTION_DOC =
Nokogiri::XML::Builder.new(encoding: UTF_8) do |xml| xml.collection(xmlns: ::MARC::MARC_NS) end.doc.freeze
- COLLECTION_CLOSING_TAG =
'</collection>'.freeze
- DEFAULT_NOKOGIRI_OPTS =
{ encoding: UTF_8 }.freeze
Instance Attribute Summary collapse
-
#nokogiri_options ⇒ Object
readonly
Returns the value of attribute nokogiri_options.
-
#out ⇒ Object
readonly
------------------------------------------------------------ Fields.
Class Method Summary collapse
-
.open(out, **nokogiri_options) {|writer| ... } ⇒ Object
Opens a new XMLWriter with the specified output destination and Nokogiri options, writes the XML prolog and opening
<collection>
tag, yields the writer to write one or more MARC records, and closes the writer.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the underlying stream.
-
#initialize(out, **nokogiri_options) ⇒ XMLWriter
constructor
Initializes a new XMLWriter.
-
#write(record) ⇒ Object
Writes the specified record to the underlying stream, writing the XML prolog and opening
<collection>
tag if they have not yet been written.
Constructor Details
#initialize(out, **nokogiri_options) ⇒ XMLWriter
Initializes a new BerkeleyLibrary::TIND::MARC::XMLWriter.
File.open('marc.xml', 'wb') do |f|
w = XMLWriter.new(f)
marc_records.each { |r| w.write(r) }
w.close
end
53 54 55 56 |
# File 'lib/berkeley_library/tind/marc/xml_writer.rb', line 53 def initialize(out, **) @nokogiri_options = () @out = ensure_io(out) end |
Instance Attribute Details
#nokogiri_options ⇒ Object (readonly)
Returns the value of attribute nokogiri_options.
29 30 31 |
# File 'lib/berkeley_library/tind/marc/xml_writer.rb', line 29 def @nokogiri_options end |
#out ⇒ Object (readonly)
Fields
28 29 30 |
# File 'lib/berkeley_library/tind/marc/xml_writer.rb', line 28 def out @out end |
Class Method Details
.open(out, **nokogiri_options) {|writer| ... } ⇒ Object
Opens a new BerkeleyLibrary::TIND::MARC::XMLWriter with the specified output destination and
Nokogiri options, writes the XML prolog and opening <collection>
tag, yields the writer to write one or more MARC records, and closes
the writer.
XMLWriter.open('marc.xml') do |w|
marc_records.each { |r| w.write(r) }
end
Note that unlike initializing a writer with #new and closing it
immediately, this will write an XML document with an empty
<collection></collection>
tag even if no records are written.
81 82 83 84 85 86 |
# File 'lib/berkeley_library/tind/marc/xml_writer.rb', line 81 def open(out, **) writer = new(out, **) writer.send(:ensure_open!) yield writer if block_given? writer.close end |
Instance Method Details
#close ⇒ Object
Closes the underlying stream. If the XML prolog and opening <collection>
tag have already been written, the closing <collection/>
tag is written
first.
108 109 110 111 |
# File 'lib/berkeley_library/tind/marc/xml_writer.rb', line 108 def close out.write(COLLECTION_CLOSING_TAG) if @open out.close end |
#write(record) ⇒ Object
Writes the specified record to the underlying stream, writing the
XML prolog and opening <collection>
tag if they have not yet
been written.
98 99 100 101 102 103 |
# File 'lib/berkeley_library/tind/marc/xml_writer.rb', line 98 def write(record) ensure_open! record_element = XMLBuilder.new(record).build record_element.write_to(out, ) out.write("\n") end |