Skip to content

Instantly share code, notes, and snippets.

@flyingzumwalt
Forked from ucsdlib/Gemfile
Last active December 18, 2015 23: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 flyingzumwalt/5861758 to your computer and use it in GitHub Desktop.
Save flyingzumwalt/5861758 to your computer and use it in GitHub Desktop.
source 'https://rubygems.org'
gem 'rails', '3.2.13'
gem "active-fedora", :git => "git://github.com/projecthydra/active_fedora.git", :branch => "rdf_nested_attributes"
#!/usr/bin/env ruby
require "active-fedora"
class MADS < RDF::Vocabulary("http://www.loc.gov/mads/rdf/v1#")
property :MADSScheme
property :isMemberOfMADSScheme
property :authoritativeLabel
property :elementList
property :elementValue
property :Topic
property :TopicElement
property :hasExactExternalAuthority
end
class TopicElement
include ActiveFedora::RdfObject
rdf_type MADS.TopicElement
map_predicates do |map|
map.elementValue(:in=> MADS)
end
end
class ElementList
# include ActiveFedora::RdfList
include ActiveFedora::RdfObject
map_predicates do |map|
map.topicElement(:in=> MADS, :to =>"TopicElement", :class_name => "TopicElement")
end
accepts_nested_attributes_for :topicElement
end
class Topic
include ActiveFedora::RdfObject
rdf_type MADS.Topic
rdf_subject { |ds| RDF::URI.new(Rails.configuration.id_namespace + ds.pid)}
map_predicates do |map|
map.name(:in => MADS, :to => 'authoritativeLabel')
map.elementList(:in => MADS, :class_name=>'ElementList')
map.externalAuthority(:in => MADS, :to => 'hasExactExternalAuthority')
end
accepts_nested_attributes_for :elementList
end
params = {
topic: {
name: "Baseball",
externalAuthority: "http://id.loc.gov/authorities/subjects/sh85012026",
elementList_attributes: [
topicElement_attributes: { elementValue: "Baseball" }
]
}
}
topic = Topic.new(RDF::Graph.new)
topic.attributes = params[:topic]
puts topic.graph.dump(:ntriples)
#!/usr/bin/env ruby
require "active-fedora"
class MADS < RDF::Vocabulary("http://www.loc.gov/mads/rdf/v1#")
property :MADSScheme
property :isMemberOfMADSScheme
property :authoritativeLabel
property :elementList
property :elementValue
property :Topic
property :TopicElement
property :hasExactExternalAuthority
end
class TopicElement
include ActiveFedora::RdfObject
rdf_type MADS.TopicElement
map_predicates do |map|
map.elementValue(:in=> MADS)
end
end
class ElementList
include ActiveFedora::RdfList
end
class Topic
include ActiveFedora::RdfObject
rdf_type MADS.Topic
rdf_subject { |ds| RDF::URI.new(Rails.configuration.id_namespace + ds.pid)}
map_predicates do |map|
map.name(:in => MADS, :to => 'authoritativeLabel')
map.elementList(:in => MADS, :class_name=>'ElementList')
map.externalAuthority(:in => MADS, :to => 'hasExactExternalAuthority')
end
accepts_nested_attributes_for :elementList
end
params = {
topic: {
name: "Baseball",
externalAuthority: "http://id.loc.gov/authorities/subjects/sh85012026",
},
topicElement: {
elementValue: "Baseball"
}
}
topic = Topic.new(RDF::Graph.new)
topic.attributes = params[:topic]
topicElement = TopicElement.new(RDF::Graph.new)
topic.attributes = params[:topicElement]
elementList = ElementList.new(RDF::Graph.new,RDF::Node.new)
elementList[0] = topicElement
topic.elementList = elementList
puts topic.graph.dump(:ntriples)
output from "bundle exec ./nested_element_list.rb":
_:bn1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.loc.gov/mads/rdf/v1#Topic> .
_:bn1 <http://www.loc.gov/mads/rdf/v1#authoritativeLabel> "Baseball" .
_:bn1 <http://www.loc.gov/mads/rdf/v1#hasExactExternalAuthority> "http://id.loc.gov/authorities/subjects/sh85012026" .
_:bn1 <http://www.loc.gov/mads/rdf/v1#elementList> "{:topicElement_attributes=>{:elementValue=>\"Baseball\"}}" .
output from "bundle exec ./nested_element_list2.rb":
_:bn1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.loc.gov/mads/rdf/v1#Topic> .
_:bn1 <http://www.loc.gov/mads/rdf/v1#authoritativeLabel> "Baseball" .
_:bn1 <http://www.loc.gov/mads/rdf/v1#hasExactExternalAuthority> "http://id.loc.gov/authorities/subjects/sh85012026" .
_:bn1 <http://www.loc.gov/mads/rdf/v1#elementList> "#<ElementList:0x007f84d229db88>" .
expected:
_:bn1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.loc.gov/mads/rdf/v1#Topic> .
_:bn1 <http://www.loc.gov/mads/rdf/v1#authoritativeLabel> "Baseball" .
_:bn1 <http://www.loc.gov/mads/rdf/v1#hasExactExternalAuthority> "http://id.loc.gov/authorities/subjects/sh85012026" .
_:bn1 <http://www.loc.gov/mads/rdf/v1#elementList> _:bn2 .
_:bn2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:bn3 .
_:bn2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .
_:bn3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.loc.gov/mads/rdf/v1#TopicElement> .
_:bn3 <http://www.loc.gov/mads/rdf/v1#elementValue> "Baseball" .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment