Skip to content

Instantly share code, notes, and snippets.

@mcritchlow
Forked from escowles/Gemfile
Last active August 29, 2015 13:57
Show Gist options
  • Save mcritchlow/9715389 to your computer and use it in GitHub Desktop.
Save mcritchlow/9715389 to your computer and use it in GitHub Desktop.
source 'https://rubygems.org'
gem 'active-fedora', github: 'projecthydra/active_fedora', branch: 'master'
gem 'linkeddata'
#!/usr/bin/env ruby
# mads demo using AF 7.0 rc3
require 'active-fedora'
require 'linkeddata'
# vocabularies
class DAMS < RDF::Vocabulary("http://library.ucsd.edu/ontology/dams#")
property :code
property :complexSubject
property :temporal
property :topic
end
class MADS < RDF::Vocabulary("http://www.loc.gov/mads/rdf/v1#")
# MADS classes
property :ComplexSubject
property :Temporal
property :Topic
property :MADSScheme
# MADS elements
property :isMemberOfMADSScheme
property :hasExactExternalAuthority
# elementList and elementList values
property :componentList
property :elementList
property :elementValue
property :TemporalElement
property :TopicElement
end
# scheme
class MadsScheme < ActiveFedora::Rdf::Resource
configure type: MADS.MADSScheme
property :name, predicate: RDF::RDFS.label
property :code, predicate: DAMS.code
property :externalAuthority, predicate: MADS.hasExactExternalAuthority
end
# simple subjects
class MadsBase < ActiveFedora::Rdf::Resource
property :name, predicate: MADS.authoritativeLabel
property :externalAuthority, predicate: MADS.hasExactExternalAuthority
property :scheme, predicate: MADS.isMemberOfMADSScheme, class_name: "MadsScheme"
accepts_nested_attributes_for :scheme
end
class MadsSimpleType < MadsBase
property :elementList, predicate: MADS.elementList, class_name: "MadsElementList"
accepts_nested_attributes_for :elementList
end
class MadsElement < ActiveFedora::Rdf::Resource
property :elementValue, predicate: MADS.elementValue
end
class MadsTemporal < MadsSimpleType
configure type: MADS.Temporal
end
class MadsTemporalElement < MadsElement
configure type: MADS.TemporalElement
end
class MadsTopic < MadsSimpleType
configure type: MADS.Topic
end
class MadsTopicElement < MadsElement
configure type: MADS.TopicElement
end
class MadsElementList < ActiveFedora::Rdf::List
configure type: MADS.elementList
property :temporalElement, predicate: MADS.TemporalElement, class_name: "MadsTemporalElement"
property :topicElement, predicate: MADS.TopicElement, class_name: "MadsTopicElement"
accepts_nested_attributes_for :temporalElement, :topicElement
end
# complex subject
class MadsComponentList < ActiveFedora::Rdf::List
configure type: MADS.componentList
property :temporal, predicate: MADS.Temporal, class_name: "MadsTemporal"
property :topic, predicate: MADS.Topic, class_name: "MadsTopic"
accepts_nested_attributes_for :temporal, :topic
end
class MadsComplexSubject < MadsBase
configure type: MADS.ComplexSubject
property :componentList, predicate: MADS.componentList, class_name: "MadsComponentList"
accepts_nested_attributes_for :componentList
end
class DamsDatastream < ActiveFedora::RdfxmlRDFDatastream
property :complexSubject, predicate: DAMS.complexSubject, class_name: "MadsComplexSubject"
property :scheme, predicate: MADS.isMemberOfMADSScheme, class_name: "MadsScheme"
property :temporal, predicate: DAMS.temporal, class_name: "MadsTemporal"
property :topic, predicate: DAMS.topic, class_name: "MadsTopic"
accepts_nested_attributes_for :complexSubject, :scheme, :temporal, :topic
end
class Record < ActiveFedora::Base
has_metadata :name=>"damsMetadata", :type=>DamsDatastream
has_attributes :complexSubject, :scheme, :temporal, :topic, datastream: 'damsMetadata', multiple: false
end
# scheme
scheme_params = {
scheme_attributes: [{
id: 'http://library.ucsd.edu/ark:20775/bb1111111x',
name: 'Library of Congress Subject Headings',
code: 'lcsh',
externalAuthority: RDF::Resource.new('http://id.loc.gov/vocabulary/subjects')
}]
}
scheme = Record.new()
scheme.damsMetadata.attributes = scheme_params
puts "standalone scheme:\n\n" + scheme.damsMetadata.content
# topic
topic_params = {
topic_attributes: [{
id: 'http://library.ucsd.edu/ark:20775/bb2222222x',
name: 'Civilization, Modern',
externalAuthority: RDF::Resource.new('http://id.loc.gov/authorities/subjects/sh85026469'),
elementList_attributes: [
{ topicElement_attributes: [{elementValue:"Civilization, Modern"}] }
],
scheme_attributes: scheme_params[:scheme_attributes]
}]
}
topic = Record.new()
topic.damsMetadata.attributes = topic_params
puts "\ntopic:\n\n" + topic.damsMetadata.content
# temporal
temporal_params = {
temporal_attributes: [{
id: 'http://library.ucsd.edu/ark:20775/bb3333333x',
name: '20th century',
externalAuthority: RDF::Resource.new('http://id.loc.gov/authorities/subjects/sh85139020'),
elementList_attributes: [
{ temporalElement_attributes: [{elementValue:"20th century"}] }
],
scheme_attributes: scheme_params[:scheme_attributes]
}]
}
temporal = Record.new()
temporal.damsMetadata.attributes = temporal_params
puts "\ntemporal:\n\n" + temporal.damsMetadata.content
# complex subject
complex_subject_params = {
complexSubject_attributes: [{
id: 'http://library.ucsd.edu/ark:20775/bb4444444x',
name: 'Civilization, Modern--20th century',
externalAuthority: RDF::Resource.new('http://id.loc.gov/authorities/subjects/sh85026473'),
componentList_attributes: [{
topic_attributes: topic_params[:topic_attributes],
temporal_attributes: temporal_params[:temporal_attributes]
}],
scheme_attributes: scheme_params[:scheme_attributes]
}]
}
subject = Record.new()
subject.damsMetadata.attributes = complex_subject_params
puts "\ncomplex subject:\n\n" + subject.damsMetadata.content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment