Class: Dor::IdentityMetadataDS
- Inherits:
-
ActiveFedora::OmDatastream
- Object
- ActiveFedora::OmDatastream
- Dor::IdentityMetadataDS
- Includes:
- SolrDocHelper
- Defined in:
- lib/dor/datastreams/identity_metadata_ds.rb
Overview
Object identity and source metadata
Constant Summary collapse
- CATKEY_TYPE_ID =
ids for previous and current catkeys
'catkey'
- PREVIOUS_CATKEY_TYPE_ID =
'previous_catkey'
- BARCODE_TYPE_ID =
'barcode'
Class Method Summary collapse
Instance Method Summary collapse
- #add_other_Id(type, val) ⇒ Object
- #add_otherId(other_id) ⇒ Object
- #add_value(name, value, attrs = {}) ⇒ Object
- #barcode ⇒ Object
-
#barcode=(val) ⇒ String
Convenience method to set the barcode.
-
#catkey ⇒ String
Convenience method to get the current catkey.
-
#catkey=(val) ⇒ String
Convenience method to set the catkey.
- #objectId ⇒ Object
- #other_ids=(values) ⇒ Object
- #otherId(type = nil) ⇒ Object
-
#previous_catkeys ⇒ Array
Convenience method to get the previous catkeys (will be an array).
-
#release_tags ⇒ Nokogiri::XML::NodeSet
Helper method to get the release tags as a nodeset.
- #remove_other_Id(type, val = nil) ⇒ Object
- #sourceId ⇒ Object (also: #source_id)
-
#sourceId=(value) ⇒ String, Nil
(also: #source_id=)
The same value, as per Ruby convention for assignment operators.
- #update_other_Id(type, new_val, val = nil) ⇒ Object
Methods included from SolrDocHelper
Class Method Details
.xml_template ⇒ Object
34 35 36 |
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 34 def self.xml_template Nokogiri::XML('<identityMetadata/>') end |
Instance Method Details
#add_other_Id(type, val) ⇒ Object
96 97 98 99 100 |
# 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_otherId(other_id) ⇒ Object
87 88 89 90 91 92 93 94 |
# 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_value(name, value, attrs = {}) ⇒ Object
38 39 40 41 |
# 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 ⇒ Object
148 149 150 |
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 148 def otherId(BARCODE_TYPE_ID).first end |
#barcode=(val) ⇒ String
Convenience method to set the barcode
155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 155 def (val) if val.blank? # if we are setting the barcode to blank, remove the node from XML remove_other_Id(BARCODE_TYPE_ID) elsif .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 ⇒ String
Convenience method to get the current catkey
120 121 122 |
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 120 def catkey otherId(CATKEY_TYPE_ID).first end |
#catkey=(val) ⇒ String
Convenience method to set the catkey
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# 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 ⇒ Object
43 44 45 |
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 43 def objectId find_by_terms(:objectId).text end |
#other_ids=(values) ⇒ Object
83 84 85 |
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 83 def other_ids=(values) values.each { |value| add_otherId(value) } end |
#otherId(type = nil) ⇒ Object
73 74 75 76 77 78 79 80 |
# 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 |
#previous_catkeys ⇒ Array
Convenience method to get the previous catkeys (will be an array)
144 145 146 |
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 144 def previous_catkeys otherId(PREVIOUS_CATKEY_TYPE_ID) end |
#release_tags ⇒ Nokogiri::XML::NodeSet
Helper method to get the release tags as a nodeset
169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/dor/datastreams/identity_metadata_ds.rb', line 169 def = ng_xml.xpath('//release') return_hash = {} .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) ⇒ Object
110 111 112 113 114 115 116 |
# 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 ⇒ Object Also known as: source_id
47 48 49 50 |
# 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 |
#sourceId=(value) ⇒ String, Nil Also known as: source_id=
The actual values assigned will have leading/trailing whitespace stripped.
Returns The same value, as per Ruby convention for assignment operators.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# 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 |
#update_other_Id(type, new_val, val = nil) ⇒ Object
102 103 104 105 106 107 108 |
# 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 |