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

Instance Method Details

#adapt_to_cmodelObject

Override ActiveFedora::Core#adapt_to_cmodel (used with associations, among other places) to preferentially use the objectType asserted in the identityMetadata.



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/dor/models/concerns/identifiable.rb', line 148

def adapt_to_cmodel
  object_type = .objectType.first
  object_class = Dor.registered_classes[object_type]

  if object_class
    instance_of?(object_class) ? self : adapt_to(object_class)
  else
    if ActiveFedora::VERSION < '8'
      result = super
      if result.class == Dor::Abstract
        adapt_to(Dor::Item)
      else
        result
      end
    else
      begin
        super
      rescue ActiveFedora::ModelNotAsserted
        adapt_to(Dor::Item)
      end
    end
  end
end

#add_other_Id(type, val) ⇒ Object



88
89
90
91
92
# File 'lib/dor/models/concerns/identifiable.rb', line 88

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_tag(tag) ⇒ Object

Add an administrative tag to an item, you will need to seperately save the item to write it to fedora

Parameters:

  • tag (string)

    The tag you wish to add



112
113
114
# File 'lib/dor/models/concerns/identifiable.rb', line 112

def add_tag(tag)
  TagService.add(self, tag)
end

#catkeyString

Convenience method to get the current catkey

Returns:

  • (String)

    current catkey value (or nil if none found)



60
61
62
# File 'lib/dor/models/concerns/identifiable.rb', line 60

def catkey
  .otherId(CATKEY_TYPE_ID).first
end

#catkey=(val) ⇒ String

Convenience method to set the catkey

Parameters:

  • val (String)

    the new source identifier

Returns:

  • (String)

    same value, as per Ruby assignment convention



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dor/models/concerns/identifiable.rb', line 67

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

#content_type_tagObject

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
   = tags.select { |tag| tag.include?('Process : Content Type') }
  .size == 1 ? [0].split(':').last.strip : ''
end

#druid_regexRegex

a regex that can be used to identify a full druid with prefix (e.g. druid:oo000oo0001)

Returns:

  • (Regex)

    a regular expression to identify a full druid



135
136
137
# File 'lib/dor/models/concerns/identifiable.rb', line 135

def druid_regex
  /druid:#{pid_regex}/
end

#pid_regexRegex

a regex that can be used to identify the last part of a druid (e.g. oo000oo0001)

Returns:

  • (Regex)

    a regular expression to identify the ID part of the druid



129
130
131
# File 'lib/dor/models/concerns/identifiable.rb', line 129

def pid_regex
  /[a-zA-Z]{2}[0-9]{3}[a-zA-Z]{2}[0-9]{4}/
end

#previous_catkeysArray

Convenience method to get the previous catkeys (will be an array)

Returns:

  • (Array)

    previous catkey values (empty array if none found)



84
85
86
# File 'lib/dor/models/concerns/identifiable.rb', line 84

def previous_catkeys
  .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

Returns:

  • (String)

    the druid sans the druid: or if there was no druid: prefix, the entire string you passed



141
142
143
144
# File 'lib/dor/models/concerns/identifiable.rb', line 141

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



102
103
104
105
106
107
108
# File 'lib/dor/models/concerns/identifiable.rb', line 102

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

#remove_tag(tag) ⇒ Object



117
118
119
# File 'lib/dor/models/concerns/identifiable.rb', line 117

def remove_tag(tag)
  TagService.remove(self, tag)
end

#source_idObject

Convenience method



44
45
46
# File 'lib/dor/models/concerns/identifiable.rb', line 44

def source_id
  .sourceId
end

#source_id=(source_id) ⇒ String Also known as: set_source_id

Convenience method

Parameters:

  • source_id (String)

    the new source identifier

Returns:

  • (String)

    same value, as per Ruby assignment convention

Raises:

  • (ArgumentError)

    see IdentityMetadataDS for logic



52
53
54
# File 'lib/dor/models/concerns/identifiable.rb', line 52

def source_id=(source_id)
  .sourceId = source_id
end

#tagsObject

helper method to get the tags as an array



33
34
35
# File 'lib/dor/models/concerns/identifiable.rb', line 33

def tags
  .tag
end

#update_other_Id(type, new_val, val = nil) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/dor/models/concerns/identifiable.rb', line 94

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

#update_tag(old_tag, new_tag) ⇒ Object



122
123
124
# File 'lib/dor/models/concerns/identifiable.rb', line 122

def update_tag(old_tag, new_tag)
  TagService.update(self, old_tag, new_tag)
end