class Dor::IdentityMetadataDS

Object identity and source metadata

Constants

BARCODE_TYPE_ID
CATKEY_TYPE_ID

ids for previous and current catkeys

PREVIOUS_CATKEY_TYPE_ID

Public Class Methods

xml_template() click to toggle source
# File lib/dor/datastreams/identity_metadata_ds.rb, line 34
def self.xml_template
  Nokogiri::XML('<identityMetadata/>')
end

Public Instance Methods

add_otherId(other_id) click to toggle source
# File lib/dor/datastreams/identity_metadata_ds.rb, line 87
def add_otherId(other_id)
  ng_xml_will_change!
  (name, val) = other_id.split(/:/, 2)
  node = ng_xml.root.add_child('<otherId/>').first
  node['name'] = name
  node.content = val
  node
end
add_other_Id(type, val) click to toggle source
# File lib/dor/datastreams/identity_metadata_ds.rb, line 96
def add_other_Id(type, val)
  raise 'There is an existing entry for ' + type + ', consider using update_other_Id().' if otherId(type).length > 0

  add_otherId(type + ':' + val)
end
add_value(name, value, attrs = {}) click to toggle source
# File lib/dor/datastreams/identity_metadata_ds.rb, line 38
def add_value(name, value, attrs = {})
  ng_xml_will_change!
  add_child_node(ng_xml.root, :value, name, value, attrs)
end
barcode() click to toggle source
# File lib/dor/datastreams/identity_metadata_ds.rb, line 148
def barcode
  otherId(BARCODE_TYPE_ID).first
end
barcode=(val) click to toggle source

Convenience method to set the barcode @param [String] val the new barcode @return [String] same value, as per Ruby assignment convention

# File lib/dor/datastreams/identity_metadata_ds.rb, line 155
def barcode=(val)
  if val.blank? # if we are setting the barcode to blank, remove the node from XML
    remove_other_Id(BARCODE_TYPE_ID)
  elsif barcode.blank? # if there is no current barcode, then add it
    add_other_Id(BARCODE_TYPE_ID, val)
  else # if there is a current barcode, update the current barcode to the new value
    update_other_Id(BARCODE_TYPE_ID, val)
  end

  val
end
catkey() click to toggle source

Convenience method to get the current catkey @return [String] current catkey value (or nil if none found)

# File lib/dor/datastreams/identity_metadata_ds.rb, line 120
def catkey
  otherId(CATKEY_TYPE_ID).first
end
catkey=(val) click to toggle source

Convenience method to set the catkey @param [String] val the new source identifier @return [String] same value, as per Ruby assignment convention

# File lib/dor/datastreams/identity_metadata_ds.rb, line 127
def catkey=(val)
  # if there was already a catkey in the record, store that in the "previous" spot (assuming there is no change)
  add_otherId("#{PREVIOUS_CATKEY_TYPE_ID}:#{catkey}") if val != catkey && !catkey.blank?

  if val.blank? # if we are setting the catkey to blank, remove the node from XML
    remove_other_Id(CATKEY_TYPE_ID)
  elsif catkey.blank? # if there is no current catkey, then add it
    add_other_Id(CATKEY_TYPE_ID, val)
  else # if there is a current catkey, update the current catkey to the new value
    update_other_Id(CATKEY_TYPE_ID, val)
  end

  val
end
objectId() click to toggle source
# File lib/dor/datastreams/identity_metadata_ds.rb, line 43
def objectId
  find_by_terms(:objectId).text
end
otherId(type = nil) click to toggle source
# File lib/dor/datastreams/identity_metadata_ds.rb, line 73
def otherId(type = nil)
  result = find_by_terms(:otherId).to_a
  if type.nil?
    result.collect { |n| [n['name'], n.text].join(':') }
  else
    result.select { |n| n['name'] == type }.collect(&:text)
  end
end
other_ids=(values) click to toggle source

@param [Array<String>] values

# File lib/dor/datastreams/identity_metadata_ds.rb, line 83
def other_ids=(values)
  values.each { |value| add_otherId(value) }
end
previous_catkeys() click to toggle source

Convenience method to get the previous catkeys (will be an array) @return [Array] previous catkey values (empty array if none found)

# File lib/dor/datastreams/identity_metadata_ds.rb, line 144
def previous_catkeys
  otherId(PREVIOUS_CATKEY_TYPE_ID)
end
release_tags() click to toggle source

Helper method to get the release tags as a nodeset @return [Nokogiri::XML::NodeSet] all release tags and their attributes

# File lib/dor/datastreams/identity_metadata_ds.rb, line 169
def release_tags
  release_tags = ng_xml.xpath('//release')
  return_hash = {}
  release_tags.each do |release_tag|
    hashed_node = release_tag_node_to_hash(release_tag)
    if !return_hash[hashed_node[:to]].nil?
      return_hash[hashed_node[:to]] << hashed_node[:attrs]
    else
      return_hash[hashed_node[:to]] = [hashed_node[:attrs]]
    end
  end
  return_hash
end
remove_other_Id(type, val = nil) click to toggle source
# File lib/dor/datastreams/identity_metadata_ds.rb, line 110
def remove_other_Id(type, val = nil)
  ng_xml.search('//otherId[@name=\'' + type + '\']')
        .select { |node| val.nil? || node.content == val }
        .each { ng_xml_will_change! }
        .each(&:remove)
        .any?
end
sourceId() click to toggle source
# File lib/dor/datastreams/identity_metadata_ds.rb, line 47
def sourceId
  node = find_by_terms(:sourceId).first
  node ? [node['source'], node.text].join(':') : nil
end
Also aliased as: source_id
sourceId=(value) click to toggle source

@param [String, Nil] value The value to set or a nil/empty string to delete sourceId node @return [String, Nil] The same value, as per Ruby convention for assignment operators @note The actual values assigned will have leading/trailing whitespace stripped.

# File lib/dor/datastreams/identity_metadata_ds.rb, line 56
def sourceId=(value)
  ng_xml_will_change!
  node = find_by_terms(:sourceId).first
  unless value.present? # so setting it to '' is the same as removal: worth documenting maybe?
    node&.remove
    return nil
  end
  parts = value.split(':', 2).map(&:strip)
  raise ArgumentError, "Source ID must follow the format 'namespace:value', not '#{value}'" unless
    parts.length == 2 && parts[0].present? && parts[1].present?

  node ||= ng_xml.root.add_child('<sourceId/>').first
  node['source'] = parts[0]
  node.content = parts[1]
end
Also aliased as: source_id=
source_id()
Alias for: sourceId
source_id=(value)
Alias for: sourceId=
update_other_Id(type, new_val, val = nil) click to toggle source
# File lib/dor/datastreams/identity_metadata_ds.rb, line 102
def update_other_Id(type, new_val, val = nil)
  ng_xml.search('//otherId[@name=\'' + type + '\']')
        .select { |node| val.nil? || node.content == val }
        .each { ng_xml_will_change! }
        .each { |node| node.content = new_val }
        .any?
end

Private Instance Methods

release_tag_node_to_hash(rtag) click to toggle source

Convert one release element into a Hash @param rtag [Nokogiri::XML::Element] the release tag element @return [Hash{:to, :attrs => String, Hash}] in the form of !{:to => String :attrs = Hash}

# File lib/dor/datastreams/identity_metadata_ds.rb, line 188
def release_tag_node_to_hash(rtag)
  to = 'to'
  release = 'release'
  when_word = 'when' # TODO: Make to and when_word load from some config file instead of hardcoded here
  attrs = rtag.attributes
  return_hash = { to: attrs[to].value }
  attrs.tap { |a| a.delete(to) }
  attrs[release] = rtag.text.casecmp('true') == 0 # save release as a boolean
  return_hash[:attrs] = attrs

  # convert all the attrs beside :to to strings, they are currently Nokogiri::XML::Attr
  (return_hash[:attrs].keys - [to]).each do |a|
    return_hash[:attrs][a] = return_hash[:attrs][a].to_s if a != release
  end

  return_hash[:attrs][when_word] = Time.parse(return_hash[:attrs][when_word]) # convert when to a datetime
  return_hash
end