Skip to content

Latest commit

 

History

History
107 lines (71 loc) · 4.43 KB

NOKOGIRI_DATASTREAMS.textile

File metadata and controls

107 lines (71 loc) · 4.43 KB

Using OM-based NokogiriDatastreams

Setup

This tutorial assumes that you’ve run script/console from the root of ActiveFedora and have imported the hydrangea:fixture_mods_article1 object. If you haven’t done that, see CONSOLE_GETTING_STARTED for instructions.

The model definition we’re using in this tutorial is {SpecialThing} ( see the code )

Look in these datastream definitions to see the OM Terminologies they define. They have extra comments in the code:

The First Pass with OM

First, load the Fedora object as an instance of the SpecialThing Model

st = SpecialThing.load_instance("hydrangea:fixture_mods_article1")

Take a look at the datastreams in the object.

st.datastreams.keys

Each datastream is associated with a class that is aware of its content. These classes are specified in the model.

st.datastreams["rightsMetadata"].class
st.datastreams["descMetadata"].class

You can retrieve the xml from the object as xml (string) using to_xml, or you can access it as a Nokogiri::XML::Document using .ng_xml

st.datastreams["rightsMetadata"].to_xml
st.datastreams["rightsMetadata"].ng_xml.class

An OM terminology is attached to the datastream’s class. OM’s convenience methods use the terminology to look up nodes and values for you.

st.datastreams["rightsMetadata"].class.terminology
Hydra::RightsMetadataDatastream.terminology

The Terminology in Hydra::ModsArticleDatastream lets you retrieve values from the descMetadata datastream’s MODS content.

mods_ds = st.datastreams["descMetadata"]
mods_ds.term_values(:person, :first_name)
mods_ds.term_values(:person, :last_name)

You can use OM’s find_by_terms method to retrieve xml nodes from the datastream. It returns Nokogiri::XML::Node objects.

mods_ds.find_by_terms(:person)
mods_ds.find_by_terms(:person).length
mods_ds.find_by_terms(:person).each {|n| puts n.to_xml}

Learning More about OM

Hydra::ModsArticleDatastream has all of the behaviors of an OM::Document. For deeper exposure to what you can do with OM, see the OM documentation for Getting Started, Querying Documents, and Updating Documents. There is also information in the solrizer documentation about Solrizing documents.

You can run most of the examples from those tutorials against the descMetadata datastream you’ve created here.

doc = st.datastreams["descMetadata"]  # the datastream is the OM Document
...
doc.class
=> Hydra::ModsArticleDatastream  # Hydra::ModsArticleDatastream is the Document Class
terminology = doc.class.terminology # The terminology is attached to the Document Class

Setting the XML in a NokogiriDatastream from a file

Creating a new Datastream using from_xml

my_path = "spec/fixtures/mods_articles/hydrangea_article1.xml"
f = File.new(my_path)
mods_ds = Hydra::ModsArticleDatastream.from_xml(f)

If you want to add that datastream to an object, set the datastream’s dsid and then pass the datastream into the object’s add_datastream method.

mods_ds.dsid = "descMetadata"
st.add_datastream(mods_ds)

Saving the Datastream

After calling add_datastream, then everything will be ready to save to Fedora. In order to make sure that your updated datastream is actually saved to fedora, call .save on the datastream. If you call .save on the object, the changes you’ve made to the datastream might not be saved.

st.datastreams["descMetadata"].save