class ROF::Ingesters::RelsExtIngester

Attributes

fdoc[R]

:models is a list of fedora content models this item has :item is the hash of the ROF item :fdoc is an optional fedora document to save to :pid is the namespaced identifier of this item

item[R]

:models is a list of fedora content models this item has :item is the hash of the ROF item :fdoc is an optional fedora document to save to :pid is the namespaced identifier of this item

models[R]

:models is a list of fedora content models this item has :item is the hash of the ROF item :fdoc is an optional fedora document to save to :pid is the namespaced identifier of this item

pid[R]

:models is a list of fedora content models this item has :item is the hash of the ROF item :fdoc is an optional fedora document to save to :pid is the namespaced identifier of this item

Public Class Methods

call(attributes) click to toggle source
# File lib/rof/ingesters/rels_ext_ingester.rb, line 8
def self.call(attributes)
  new(attributes).call
end
new(attributes = {}) click to toggle source
# File lib/rof/ingesters/rels_ext_ingester.rb, line 17
def initialize(attributes = {})
  @models = attributes.fetch(:models)
  @item = attributes.fetch(:item)
  @pid = item.fetch('pid')
  @fdoc = attributes.fetch(:fedora_document, nil)
end

Public Instance Methods

call() click to toggle source
# File lib/rof/ingesters/rels_ext_ingester.rb, line 24
def call
  content = build_content
  persist(content)
  content
end

Private Instance Methods

build_content() click to toggle source
# File lib/rof/ingesters/rels_ext_ingester.rb, line 36
def build_content
  # this is ugly to work around addRelationship bug in 3.6.x
  # (See bugs FCREPO-1191 and FCREPO-1187)

  # build up a json-ld object, and then persist that (into XML!)
  input = rels_ext
  context = input.fetch("@context", {}).merge(ROF::RelsExtRefContext)
  input["@context"] = context
  input["@id"] = "info:fedora/#{pid}"

  input["hasModel"] = models

  # RELS-EXT should only contain references to other (internal) fedora
  # objects. Rewrite them to have prefix "info:fedora/".
  # Also need to make sure json-ld interprets each of these object
  # references as an IRI instead of a string.
  # This is kinda hacky. Is there a better way?
  input.each do |relation, targets|
    next if relation == "@context" || relation == "@id" || relation == "hasModel"
    targets = [targets] if targets.is_a?(String) || targets.is_a?(Hash)
    input[relation] = targets.map do |target|
      target.is_a?(String) ? {"@id" => "info:fedora/#{target}"} : prefix_ids_recursive(target)
    end
  end

  graph = RDF::Graph.new << JSON::LD::API.toRdf(input)
  graph.dump(:rdfxml)
end
persist(content) click to toggle source
# File lib/rof/ingesters/rels_ext_ingester.rb, line 79
def persist(content)
  if fdoc
    ds = fdoc['RELS-EXT']
    ds.content = content
    ds.mimeType = "application/rdf+xml"
    ds.save
  else
    true
  end
end
prefix_ids_recursive(data) click to toggle source

prefix all strings or hash values with “info:fedora/” recursively decends into arrays and hashes

# File lib/rof/ingesters/rels_ext_ingester.rb, line 67
def prefix_ids_recursive(data)
    return "info:fedora/" + data if data.is_a?(String)
    if data.is_a?(Array)
        return data.map { |x| prefix_ids_recursive(x) }
    end
    if data.is_a?(Hash)
        return Hash[data.map { |k,v| [k, prefix_ids_recursive(v)] }]
    end
    # don't know what data is. just return it
    data
end
rels_ext() click to toggle source
# File lib/rof/ingesters/rels_ext_ingester.rb, line 32
def rels_ext
  item.fetch('rels-ext', {})
end