Created
July 17, 2013 20:16
-
-
Save escowles/6024052 to your computer and use it in GitHub Desktop.
nested attributes demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "active-fedora" | |
class MADS < RDF::Vocabulary("http://www.loc.gov/mads/rdf/v1#") | |
property :ComplexSubject | |
property :MADSScheme | |
property :MainTitleElement | |
property :SubTitleElement | |
property :Temporal | |
property :TemporalElement | |
property :Title | |
property :Topic | |
property :TopicElement | |
property :authoritativeLabel | |
property :componentList | |
property :elementList | |
property :elementValue | |
property :hasExactExternalAuthority | |
property :isMemberOfMADSScheme | |
end | |
class MadsMainTitleElement | |
include ActiveFedora::RdfObject | |
rdf_type MADS.MainTitleElement | |
map_predicates do |map| | |
map.elementValue(:in=> MADS) | |
end | |
end | |
class MadsSubTitleElement | |
include ActiveFedora::RdfObject | |
rdf_type MADS.SubTitleElement | |
map_predicates do |map| | |
map.elementValue(:in=> MADS) | |
end | |
end | |
class MadsTemporalElement | |
include ActiveFedora::RdfObject | |
rdf_type MADS.TemporalElement | |
map_predicates do |map| | |
map.elementValue(:in=> MADS) | |
end | |
end | |
class MadsTopicElement | |
include ActiveFedora::RdfObject | |
rdf_type MADS.TopicElement | |
map_predicates do |map| | |
map.elementValue(:in=> MADS) | |
end | |
end | |
class MadsElementList | |
include ActiveFedora::RdfList | |
map_predicates do |map| | |
map.mainTitleElement(:in=> MADS, :to =>"MainTitleElement", :class_name => "MadsMainTitleElement") | |
map.temporalElement(:in=> MADS, :to =>"TemporalElement", :class_name => "MadsTemporalElement") | |
map.topicElement(:in=> MADS, :to =>"TopicElement", :class_name => "MadsTopicElement") | |
map.subTitleElement(:in=> MADS, :to =>"SubTitleElement", :class_name => "MadsSubTitleElement") | |
end | |
accepts_nested_attributes_for :mainTitleElement, :subTitleElement, :temporalElement, :topicElement | |
end | |
class MadsTemporal | |
include ActiveFedora::RdfObject | |
rdf_type MADS.Temporal | |
map_predicates do |map| | |
map.label(:in => MADS, :to => 'authoritativeLabel') | |
map.scheme(:in => MADS, :to => 'isMemberOfMADSScheme') | |
map.externalAuthority(:in => MADS, :to => 'hasExactExternalAuthority') | |
map.elementList(:in => MADS, :class_name=>'MadsElementList') | |
end | |
accepts_nested_attributes_for :elementList | |
end | |
class MadsTitle | |
include ActiveFedora::RdfObject | |
rdf_type MADS.Title | |
map_predicates do |map| | |
map.label(:in => MADS, :to => 'authoritativeLabel') | |
map.scheme(:in => MADS, :to => 'isMemberOfMADSScheme') | |
map.externalAuthority(:in => MADS, :to => 'hasExactExternalAuthority') | |
map.elementList(:in => MADS, :class_name=>'MadsElementList') | |
end | |
accepts_nested_attributes_for :elementList | |
end | |
class MadsTopic | |
include ActiveFedora::RdfObject | |
rdf_type MADS.Topic | |
map_predicates do |map| | |
map.label(:in => MADS, :to => 'authoritativeLabel') | |
map.scheme(:in => MADS, :to => 'isMemberOfMADSScheme') | |
map.externalAuthority(:in => MADS, :to => 'hasExactExternalAuthority') | |
map.elementList(:in => MADS, :class_name=>'MadsElementList') | |
end | |
accepts_nested_attributes_for :elementList | |
end | |
class MadsComponentList | |
include ActiveFedora::RdfList | |
map_predicates do |map| | |
map.temporal(:in=> MADS, :to =>"Temporal", :class_name => "MadsTemporal") | |
map.topic(:in=> MADS, :to =>"Topic", :class_name => "MadsTopic") | |
end | |
accepts_nested_attributes_for :temporal, :topic | |
end | |
class MadsComplexSubject | |
include ActiveFedora::RdfObject | |
rdf_type MADS.ComplexSubject | |
map_predicates do |map| | |
map.label(:in => MADS, :to => 'authoritativeLabel') | |
map.externalAuthority(:in => MADS, :to => 'hasExactExternalAuthority') | |
map.componentList(:in => MADS, :class_name=>'MadsComponentList') | |
end | |
accepts_nested_attributes_for :componentList | |
end | |
# complex subject | |
# TODO: repeated components of the same type not working | |
# TODO: multiple components breaks rdf:list | |
exturi = RDF::Resource.new "http://id.loc.gov/authorities/subjects/sh85148221" | |
scheme = RDF::Resource.new "http://library.ucsd.edu/ark:/20775/bb1234567x" | |
params = { | |
complexSubject: { | |
label: "World politics--20th Century", | |
scheme: scheme, | |
externalAuthority: exturi, | |
componentList_attributes: [ | |
topic_attributes: { | |
label: "World politics", | |
elementList_attributes: [ | |
topicElement_attributes: [{ elementValue: "World politics" }] | |
] | |
}, | |
temporal_attributes: { | |
label: "20th Century", | |
elementList_attributes: [ | |
temporalElement_attributes: [{ elementValue: "20th Century"}], | |
] | |
} | |
] | |
} | |
} | |
subject = MadsComplexSubject.new(RDF::Graph.new) | |
subject.attributes = params[:complexSubject] | |
puts subject.graph.dump(:ntriples) | |
# title | |
# TODO: multiple elements breaks rdf:list | |
scheme = RDF::Resource.new "http://library.ucsd.edu/ark:/20775/bb1234567x" | |
params = { | |
title: { | |
label: "The Hobbit: Or, There and Back Again", | |
scheme: scheme, | |
elementList_attributes: [ | |
mainTitleElement_attributes: [{ elementValue: "The Hobbit" }] | |
] | |
} | |
} | |
#subTitleElement_attributes: [{ elementValue: "Or, There and Back Again" }] | |
title = MadsTitle.new(RDF::Graph.new) | |
title.attributes = params[:title] | |
puts title.graph.dump(:ntriples) | |
# topic | |
exturi = RDF::Resource.new "http://id.loc.gov/authorities/subjects/sh85124118" | |
scheme = RDF::Resource.new "http://library.ucsd.edu/ark:/20775/bb1234567x" | |
params = { | |
topic: { | |
label: "Socialism", | |
externalAuthority: exturi, | |
scheme: scheme, | |
elementList_attributes: [ | |
topicElement_attributes: [{ elementValue: "Socialism" }] | |
] | |
} | |
} | |
topic = MadsTopic.new(RDF::Graph.new) | |
topic.attributes = params[:topic] | |
puts topic.graph.dump(:ntriples) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'rails', '3.2.13' | |
gem "active-fedora", :git => "git://github.com/projecthydra/active_fedora.git" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment