Sha256: ecc9ec2c34224f5ecce35cc651e36d647d7ca9e8560bc320dc9c3eb8cc7dba5b
Contents?: true
Size: 1.65 KB
Versions: 1
Compression:
Stored size: 1.65 KB
Contents
require 'nokogiri' module BerkeleyLibrary module TIND module MARC class XMLBuilder attr_reader :marc_record def initialize(marc_record) @marc_record = marc_record end def build builder.doc.root.tap(&:unlink) end private def builder Nokogiri::XML::Builder.new do |xml| xml.record do add_leader(xml) marc_record.each_control_field { |cf| add_control_field(xml, cf) } marc_record.each_data_field { |df| add_data_field(xml, df) } end end end def add_leader(xml) leader = marc_record.leader return if leader.nil? || leader == '' # TIND uses <controlfield tag="000"/> instead of <leader/> leader_as_cf = ::MARC::ControlField.new('000', clean_leader(leader)) add_control_field(xml, leader_as_cf) end def add_data_field(xml, df) xml.datafield(tag: df.tag, ind1: df.indicator1, ind2: df.indicator2) do df.subfields.each do |sf| xml.subfield(sf.value, code: sf.code) end end end def add_control_field(xml, cf) # TIND uses \ (0x5c), not space (0x32), for unspecified values in positional fields value = cf.value&.gsub(' ', '\\') xml.controlfield(value, tag: cf.tag) end def clean_leader(leader) leader.gsub(/[^\w|^\s]/, 'Z').tap do |ldr| ldr[20..23] = '4500' unless ldr[20..23] == '4500' ldr[6..6] = 'Z' if ldr[6..6] == ' ' end end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
berkeley_library-tind-0.5.0 | lib/berkeley_library/tind/marc/xml_builder.rb |