Method: MARC::Writer.encode
- Defined in:
- lib/marc/writer.rb
.encode(record, allow_oversized = false) ⇒ Object
a static method that accepts a MARC::Record object and returns the record encoded as MARC21 in transmission format
Second arg allow_oversized, default false, set to true to raise on MARC record that can’t fit into ISO 2709.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/marc/writer.rb', line 64 def self.encode(record, allow_oversized = false) directory = "" fields = "" offset = 0 record.each do |field| # encode the field field_data = "" if field.instance_of?(MARC::DataField) warn("Warn: Missing indicator") unless field.indicator1 && field.indicator2 field_data = (field.indicator1 || " ") + (field.indicator2 || " ") field.subfields.each do |s| field_data += SUBFIELD_INDICATOR + s.code + s.value end elsif field.instance_of?(MARC::ControlField) field_data = field.value end field_data += END_OF_FIELD # calculate directory entry for the field field_length = (field_data.respond_to?(:bytesize) ? field_data.bytesize : field_data.length) directory += sprintf("%03s", field.tag) + format_byte_count(field_length, allow_oversized, 4) + format_byte_count(offset, allow_oversized) # add field to data for other fields fields += field_data # update offset for next field offset += field_length end # determine the base (leader + directory) base = record.leader + directory + END_OF_FIELD # determine complete record marc = base + fields + END_OF_RECORD # update leader with the byte offest to the end of the directory bytesize = base.respond_to?(:bytesize) ? base.bytesize() : base.length marc[12..16] = format_byte_count(bytesize, allow_oversized) # update the record length bytesize = marc.respond_to?(:bytesize) ? marc.bytesize() : marc.length marc[0..4] = format_byte_count(bytesize, allow_oversized) # store updated leader in the record that was passed in record.leader = marc[0..LEADER_LENGTH - 1] # return encoded marc marc end |