Skip to content

Instantly share code, notes, and snippets.

@helrond
Created March 6, 2015 16:41
Show Gist options
  • Save helrond/f61dcb0ead26b8eca110 to your computer and use it in GitHub Desktop.
Save helrond/f61dcb0ead26b8eca110 to your computer and use it in GitHub Desktop.
class MODSSerializer < ASpaceExport::Serializer
serializer_for :mods
include JSONModel
def serialize_mods_inner(mods, xml)
xml.titleInfo {
xml.title mods.title
}
xml.typeOfResource mods.type_of_resource
xml.originInfo {
mods.dates.each do |date|
serialize_date(date, xml)
end
}
xml.language {
xml.languageTerm(:type => 'code') {
xml.text mods.language_term
}
}
xml.physicalDescription{
mods.extents.each do |extent|
xml.extent extent
end
}
mods.notes.each do |note|
if note.wrapping_tag
xml.send(note.wrapping_tag) {
serialize_note(note, xml)
}
else
serialize_note(note, xml)
end
end
mods.subjects.each do |subject|
xml.subject(:authority => subject['source']) {
subject['terms'].each do |term|
xml.topic term
end
}
end
xml.identifier(:type => 'local') {xml.text mods.digital_object_id}
mods.names.each do |name|
case name['role']
when 'subject'
xml.subject {
serialize_name(name, xml)
}
else
serialize_name(name, xml)
end
end
mods.parts.each do |part|
xml.part(:ID => part['id']) {
xml.detail {
xml.title part['title']
}
}
end
# flattened tree
mods.each_related_item do |item|
xml.relatedItem(:type => 'constituent') {
serialize_mods_inner(item, xml)
}
end
xml.relatedItem(:type => 'host', :displayLabel => 'collection') {
xml.name mods.resource
}
mods.component.each do |component|
xml.relatedItem(:type => 'host', :displayLabel => 'component') {
xml.name component['title']
xml.identifier component['ref_id']
xml.note component['resource']
xml.note component['resourceLink']
}
end
end
def serialize_date(date, xml)
atts={}
if(date['certainty'])
atts['qualifier'] = date['certainty']
end
case date['label']
when 'creation'
xml.dateCreated(atts) {xml.text format_date(date)}
when 'issued'
xml.dateIssued(atts) {xml.text format_date(date)}
when 'broadcast'
xml.dateIssued(atts) {xml.text format_date(date)}
when 'digitized'
xml.dateCaptured(atts) {xml.text format_date(date)}
when 'modified'
xml.dateModified(atts) {xml.text format_date(date)}
when 'copyright'
xml.copyrightDate(atts) {xml.text format_date(date)}
else
xml.dateOther(atts) {xml.text format_date(date)}
end
end
def format_date(date)
case date['expression']
when nil
case date['type']
when 'single'
date['begin']
else
date['begin'] + '-' + date['end']
end
else
date['expression']
end
end
end
ExportHelpers.module_eval do;
def generate_mods(id)
obj = resolve_references(DigitalObject.to_jsonmodel(id), ['repository::agent_representation', 'linked_agents', 'subjects', 'tree', 'linked_instances'])
mods = ASpaceExport.model(:mods).from_digital_object(JSONModel(:digital_object).new(obj))
ASpaceExport::serialize(mods)
end
end
class MODSModel < ASpaceExport::ExportModel
model_for :mods
include JSONModel
attr_accessor :digital_object_id
attr_accessor :dates
attr_accessor :resource
attr_accessor :component
@archival_object_map = {
:digital_object_id => :digital_object_id=,
:title => :title=,
:language => :language_term=,
:extents => :handle_extent,
:subjects => :handle_subjects,
:linked_agents => :handle_agents,
:notes => :handle_notes,
:dates => :handle_dates,
:linked_instances => :handle_instances,
}
def initialize
@digital_object_id = []
@extents = []
@notes = []
@subjects = []
@names = []
@parts = []
@dates = []
@component = []
@resource = nil
end
def handle_dates(dates)
dates.each do |date|
self.dates << {
'label' => date['label'],
'expression' => date['expression'],
'begin' => date['begin'],
'end' => date['end'],
'type' => date['date_type'],
'certainty' => date['certainty'],
'era' => date['era'],
'calendar' => date['calendar'],
}
end
end
def handle_instances(linked_instances)
linked_instances.each do |link|
component = link['_resolved']
resourceLink = component['resource']['ref']
self.component << {
'ref_id' => component['ref_id'],
'title' => component['display_string'],
}
resource = Resource.to_jsonmodel(JSONModel(:resource).id_for(resourceLink))
self.resource << {
'json' => resource
}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment