Skip to content

Lesson: Write your RDF into Fedora Datastreams (AF7)

Thomas Johnson edited this page Jun 9, 2014 · 10 revisions

Once you've defined your RDF terminology, converting it into a Fedora datastream is rather straightforward. Simply create a datastream that extends either from ActiveFedora::NtriplesRDFDatastream or ActiveFedora::RdfxmlRDFDatastream depending on your preference for serialization format. Then copy your property statements into the body of the class. There's no need to say include ActiveFedora::RdfObject, that's all taken care of by the superclass.

class DublinCoreDatastream < ActiveFedora::RDFDatastream
  property :title, predicate: RDF::DC.title
  property :creator, predicate: RDF::DC.creator
  property :contributor, predicate: RDF::DC.contributor
  property :date, predicate: RDF::DC.date
  property :subject, predicate: RDF::DC.subject
  property :relation, predicate: RDF::DC.relation
  property :created, predicate: RDF::DC.created 
  property :issued, predicate: RDF::DC.issued
end

Now that we've defined our datastream, let's use it in an ActiveFedora model.

class MyObj < ActiveFedora::Base
  has_metadata 'descMetadata', type: DublinCoreDatastream
  has_attributes :title, :created, datastream: 'descMetadata', multiple: false
end

That's all there is to it. The has_metadata asserts that we have a datastream called "descMetadata" and that the type of datastream it has is DublinCoreDatastream. has_attributes is a function that proxies the title and created methods to the DublinCoreDatastream.

Here's how we can interact with our new object:

require "./app/models/datastreams/dublin_core_datastream"
require "./app/models/my_obj"
m = MyObj.new
=> #<MyObj pid: nil, title: nil, created: nil>
m.title = "One Hundred Years of Solitude"
=> "One Hundred Years of Solitude"
m.created = '1967'
=> "1967"
m.save
=> true
m.inspect
=> "#<MyObj pid: \"changeme:7224\", title: \"One Hundred Years of Solitude\", created: \"1967\">"
m.title
=> "One Hundred Years of Solitude"
m.dump :ntriples
=> "_:g70247429361520 <http://purl.org/dc/terms/title> \"One Hundred Years of Solitude\" .\n_:g70247429361520 <http://purl.org/dc/terms/created> \"1967\" .\n"
m.save
=> true
m.inspect
=> "#<MyObj pid: \"changeme:7224\", title: \"One Hundred Years of Solitude\", created: \"1967\">"

Next Step

Go on to Lesson: Control Indexing of your RDF metadata (AF7) or return to the Tame your RDF Metadata with ActiveFedora landing page.

Clone this wiki locally