Skip to content

Instantly share code, notes, and snippets.

@aseroff
Last active March 13, 2019 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aseroff/e1d205d1274b776a25d12bd307cfdc82 to your computer and use it in GitHub Desktop.
Save aseroff/e1d205d1274b776a25d12bd307cfdc82 to your computer and use it in GitHub Desktop.
def marc_record(textbook)
# Generate a MARC record with a leader
record = MARC::Record.new()
# Leader example:
# *00-04 Record length computed automatically
record.leader[5] = "n"
record.leader[6] = "a"
record.leader[7] = "i"
record.leader[9] = "a"
# ...
# Control Field (00#) example:
# *008
# 00-05 Date of creation, 06 Type of publication date, 07-10 Date 1, 11-14 Date 2, 15-17 Country of publication code, 35-37 Language of publication code
record.append(MARC::ControlField.new('008', textbook.created_at.strftime('%y%m%d') + "c" + (textbook.copyright_year ? textbook.copyright_year.to_s : "0000") + "9999" + "mnu" + " " + textbook.language + " ") )
# ...
# Data Field examples:
# *050: Library of Congress Call Number
# *$a: Classification number (R)
# $b: Item number (NR)
textbook.subjects.pluck(:call_number).uniq.compact.each do |call_number|
loc_field = MARC::DataField.new('050', ' ', '4') # First indicator: 0/1/# if in Library of Congress collection (blank is unknown). Second indicator: 0/4 call number assigned by LC or other (respectively)
loc_field.subfields << MARC::Subfield.new('a', call_number)
record.append(loc_field)
end
# 336: Content Type
# $a: Content type term
# $b: Content type code
# $2: Code source
content_record = MARC::DataField.new('336') # No indicators
content_record.subfields << MARC::Subfield.new('a', "text")
content_record.subfields << MARC::Subfield.new('b', "txt")
content_record.subfields << MARC::Subfield.new('2', "rdacontent")
record.append(content_record)
record
end
def generate_marc(textbooks)
writer = MARC::Writer.new("path/to/file.mrc")
textbooks.each do |t|
writer.write(marc_record(t))
end
writer.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment