Skip to content

Lesson: write your rdf into fedora datastreams

Justin Coyne edited this page Jan 20, 2014 · 3 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 map_predicates block 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::NtriplesRDFDatastream
  map_predicates do |map|
    map.title(in: RDF::DC)
    map.created(in: RDF::DC)
    #...
  end
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:

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"
Clone this wiki locally