Module: Dor::Identifiable
- Extended by:
- ActiveSupport::Concern
- Included in:
- Abstract
- Defined in:
- lib/dor/models/concerns/identifiable.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- CATKEY_TYPE_ID =
ids for previous and current catkeys
'catkey'
- PREVIOUS_CATKEY_TYPE_ID =
'previous_catkey'
Instance Method Summary collapse
-
#adapt_to_cmodel ⇒ Object
Override ActiveFedora::Core#adapt_to_cmodel (used with associations, among other places) to preferentially use the objectType asserted in the identityMetadata.
- #add_other_Id(type, val) ⇒ Object
-
#add_tag(tag) ⇒ Object
Add an administrative tag to an item, you will need to seperately save the item to write it to fedora.
-
#catkey ⇒ String
Convenience method to get the current catkey.
-
#catkey=(val) ⇒ String
Convenience method to set the catkey.
-
#content_type_tag ⇒ Object
helper method to get just the content type tag.
-
#druid_regex ⇒ Regex
a regex that can be used to identify a full druid with prefix (e.g. druid:oo000oo0001).
-
#normalize_tag(tag_str) ⇒ Object
take a tag string and return a normalized tag string.
-
#normalize_tag_arr(tag_arr) ⇒ Object
turn a tag array back into a tag string with a standard format.
-
#pid_regex ⇒ Regex
a regex that can be used to identify the last part of a druid (e.g. oo000oo0001).
-
#previous_catkeys ⇒ Array
Convenience method to get the previous catkeys (will be an array).
-
#remove_druid_prefix(druid = id) ⇒ String
Since purl does not use the druid: prefix but much of dor does, use this function to strip the druid: if needed.
- #remove_other_Id(type, val = nil) ⇒ Object
- #remove_tag(tag) ⇒ Object
-
#source_id ⇒ Object
Convenience method.
-
#source_id=(source_id) ⇒ String
(also: #set_source_id)
Convenience method.
-
#split_tag_to_arr(tag_str) ⇒ Object
turns a tag string into an array with one element per tag part.
-
#tags ⇒ Object
helper method to get the tags as an array.
- #update_other_Id(type, new_val, val = nil) ⇒ Object
- #update_tag(old_tag, new_tag) ⇒ Object
-
#validate_and_normalize_tag(tag_str, existing_tag_list) ⇒ Object
take a proposed tag string and a list of the existing tags for the object being edited.
-
#validate_tag_format(tag_str) ⇒ Array
Ensure that an administrative tag meets the proper mininum format.
Instance Method Details
#adapt_to_cmodel ⇒ Object
Override ActiveFedora::Core#adapt_to_cmodel (used with associations, among other places) to preferentially use the objectType asserted in the identityMetadata.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/dor/models/concerns/identifiable.rb', line 208 def adapt_to_cmodel object_type = identityMetadata.objectType.first object_class = Dor.registered_classes[object_type] if object_class self.instance_of?(object_class) ? self : self.adapt_to(object_class) else if ActiveFedora::VERSION < '8' result = super if result.class == Dor::Abstract self.adapt_to(Dor::Item) else result end else begin super rescue ActiveFedora::ModelNotAsserted self.adapt_to(Dor::Item) end end end end |
#add_other_Id(type, val) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/dor/models/concerns/identifiable.rb', line 89 def add_other_Id(type, val) if identityMetadata.otherId(type).length > 0 raise 'There is an existing entry for ' + type + ', consider using update_other_Id().' end identityMetadata.add_otherId(type + ':' + val) end |
#add_tag(tag) ⇒ Object
Add an administrative tag to an item, you will need to seperately save the item to write it to fedora
163 164 165 166 167 |
# File 'lib/dor/models/concerns/identifiable.rb', line 163 def add_tag(tag) = identityMetadata normalized_tag = validate_and_normalize_tag(tag, .) .add_value(:tag, normalized_tag) end |
#catkey ⇒ String
Convenience method to get the current catkey
60 61 62 |
# File 'lib/dor/models/concerns/identifiable.rb', line 60 def catkey identityMetadata.otherId(CATKEY_TYPE_ID).first end |
#catkey=(val) ⇒ String
Convenience method to set the catkey
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/dor/models/concerns/identifiable.rb', line 67 def catkey=(val) if val != catkey && !catkey.blank? # if there was already a catkey in the record, store that in the "previous" spot (assuming there is no change) identityMetadata.add_otherId("#{PREVIOUS_CATKEY_TYPE_ID}:#{catkey}") end 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 |
#content_type_tag ⇒ Object
helper method to get just the content type tag
38 39 40 41 |
# File 'lib/dor/models/concerns/identifiable.rb', line 38 def content_type_tag content_tag = .select { |tag| tag.include?('Process : Content Type') } content_tag.size == 1 ? content_tag[0].split(':').last.strip : '' end |
#druid_regex ⇒ Regex
a regex that can be used to identify a full druid with prefix (e.g. druid:oo000oo0001)
195 196 197 |
# File 'lib/dor/models/concerns/identifiable.rb', line 195 def druid_regex /druid:#{pid_regex}/ end |
#normalize_tag(tag_str) ⇒ Object
take a tag string and return a normalized tag string
125 126 127 |
# File 'lib/dor/models/concerns/identifiable.rb', line 125 def normalize_tag(tag_str) normalize_tag_arr(split_tag_to_arr(tag_str)) end |
#normalize_tag_arr(tag_arr) ⇒ Object
turn a tag array back into a tag string with a standard format
120 121 122 |
# File 'lib/dor/models/concerns/identifiable.rb', line 120 def normalize_tag_arr(tag_arr) tag_arr.join(' : ') end |
#pid_regex ⇒ Regex
a regex that can be used to identify the last part of a druid (e.g. oo000oo0001)
189 190 191 |
# File 'lib/dor/models/concerns/identifiable.rb', line 189 def pid_regex /[a-zA-Z]{2}[0-9]{3}[a-zA-Z]{2}[0-9]{4}/ end |
#previous_catkeys ⇒ Array
Convenience method to get the previous catkeys (will be an array)
85 86 87 |
# File 'lib/dor/models/concerns/identifiable.rb', line 85 def previous_catkeys identityMetadata.otherId(PREVIOUS_CATKEY_TYPE_ID) end |
#remove_druid_prefix(druid = id) ⇒ String
Since purl does not use the druid: prefix but much of dor does, use this function to strip the druid: if needed
201 202 203 204 |
# File 'lib/dor/models/concerns/identifiable.rb', line 201 def remove_druid_prefix(druid = id) result = druid.match(/#{pid_regex}/) result.nil? ? druid : result[0] # if no matches, return the string passed in, otherwise return the match end |
#remove_other_Id(type, val = nil) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/dor/models/concerns/identifiable.rb', line 105 def remove_other_Id(type, val = nil) identityMetadata.ng_xml.search('//otherId[@name=\'' + type + '\']') .select { |node| val.nil? || node.content == val } .each { identityMetadata.ng_xml_will_change! } .each(&:remove) .any? end |
#remove_tag(tag) ⇒ Object
169 170 171 172 173 174 175 176 |
# File 'lib/dor/models/concerns/identifiable.rb', line 169 def remove_tag(tag) normtag = normalize_tag(tag) identityMetadata.ng_xml.search('//tag') .select { |node| normalize_tag(node.content) == normtag } .each { identityMetadata.ng_xml_will_change! } .each(&:remove) .any? end |
#source_id ⇒ Object
Convenience method
44 45 46 |
# File 'lib/dor/models/concerns/identifiable.rb', line 44 def source_id identityMetadata.sourceId end |
#source_id=(source_id) ⇒ String Also known as: set_source_id
Convenience method
52 53 54 |
# File 'lib/dor/models/concerns/identifiable.rb', line 52 def source_id=(source_id) identityMetadata.sourceId = source_id end |
#split_tag_to_arr(tag_str) ⇒ Object
turns a tag string into an array with one element per tag part. split on “:”, disregard leading and trailing whitespace on tokens.
115 116 117 |
# File 'lib/dor/models/concerns/identifiable.rb', line 115 def split_tag_to_arr(tag_str) tag_str.split(':').map { |str| str.strip } end |
#tags ⇒ Object
helper method to get the tags as an array
33 34 35 |
# File 'lib/dor/models/concerns/identifiable.rb', line 33 def identityMetadata.tag end |
#update_other_Id(type, new_val, val = nil) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/dor/models/concerns/identifiable.rb', line 97 def update_other_Id(type, new_val, val = nil) identityMetadata.ng_xml.search('//otherId[@name=\'' + type + '\']') .select { |node| val.nil? || node.content == val } .each { identityMetadata.ng_xml_will_change! } .each { |node| node.content = new_val } .any? end |
#update_tag(old_tag, new_tag) ⇒ Object
178 179 180 181 182 183 184 185 |
# File 'lib/dor/models/concerns/identifiable.rb', line 178 def update_tag(old_tag, new_tag) normtag = normalize_tag(old_tag) identityMetadata.ng_xml.search('//tag') .select { |node| normalize_tag(node.content) == normtag } .each { identityMetadata.ng_xml_will_change! } .each { |node| node.content = normalize_tag(new_tag) } .any? end |
#validate_and_normalize_tag(tag_str, existing_tag_list) ⇒ Object
take a proposed tag string and a list of the existing tags for the object being edited. if the proposed tag is valid, return it in normalized form. if not, raise an exception with an explanatory message.
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/dor/models/concerns/identifiable.rb', line 132 def validate_and_normalize_tag(tag_str, existing_tag_list) tag_arr = validate_tag_format(tag_str) # note that the comparison for duplicate tags is case-insensitive, but we don't change case as part of the normalized version # we return, because we want to preserve the user's intended case. normalized_tag = normalize_tag_arr(tag_arr) dupe_existing_tag = existing_tag_list.detect { |existing_tag| normalize_tag(existing_tag).casecmp(normalized_tag) == 0 } if dupe_existing_tag raise "An existing tag (#{dupe_existing_tag}) is the same, consider using update_tag?" end normalized_tag end |
#validate_tag_format(tag_str) ⇒ Array
Ensure that an administrative tag meets the proper mininum format
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/dor/models/concerns/identifiable.rb', line 149 def validate_tag_format(tag_str) tag_arr = split_tag_to_arr(tag_str) if tag_arr.length < 2 raise ArgumentError, "Invalid tag structure: tag '#{tag_str}' must have at least 2 elements" end if tag_arr.detect { |str| str.empty? } raise ArgumentError, "Invalid tag structure: tag '#{tag_str}' contains empty elements" end tag_arr end |