Class: Dor::VersionMetadataDS

Inherits:
ActiveFedora::OmDatastream
  • Object
show all
Defined in:
lib/dor/datastreams/version_metadata_ds.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.xml_templateObject

Default EventsDS xml


65
66
67
68
69
70
71
72
73
74
# File 'lib/dor/datastreams/version_metadata_ds.rb', line 65

def self.xml_template
  builder = Nokogiri::XML::Builder.new do |xml|
    xml. {
      xml.version(:versionId => '1', :tag => '1.0.0') {
        xml.description 'Initial Version'
      }
    }
  end
  return builder.doc
end

Instance Method Details

#current_descriptionString


193
194
195
196
197
198
199
# File 'lib/dor/datastreams/version_metadata_ds.rb', line 193

def current_description
  desc_node=current_version_node.at_xpath('description')
  if desc_node
    return desc_node.content
  end
  ''
end

#current_tagString


170
171
172
# File 'lib/dor/datastreams/version_metadata_ds.rb', line 170

def current_tag
  current_version_node[:tag].to_s  
end

#current_version_closeable?Boolean


160
161
162
163
164
165
166
167
# File 'lib/dor/datastreams/version_metadata_ds.rb', line 160

def current_version_closeable?
  current = current_version_node
  if(current[:tag] && current.at_xpath('description'))
    return true
  else
    return false
  end
end

#current_version_idString


111
112
113
114
115
116
117
118
# File 'lib/dor/datastreams/version_metadata_ds.rb', line 111

def current_version_id
  current_version=current_version_node
  if current_version.nil?
    return '1'
  else
    current_version[:versionId].to_s
  end
end

#description_for_version(versionId) ⇒ String


183
184
185
186
187
188
189
190
# File 'lib/dor/datastreams/version_metadata_ds.rb', line 183

def description_for_version(versionId)
  nodes=self.ng_xml.search('//version[@versionId=\''+versionId+'\']')
  if nodes.length == 1 and  nodes.first.at_xpath('description')
    nodes.first.at_xpath('description').content.to_s
  else
    ''
  end
end

#ensure_non_versionableObject


76
77
78
# File 'lib/dor/datastreams/version_metadata_ds.rb', line 76

def ensure_non_versionable
  self.versionable = "false"
end

#increment_version(description = nil, significance = nil) ⇒ Object


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dor/datastreams/version_metadata_ds.rb', line 83

def increment_version(description = nil, significance = nil)
  if( find_by_terms(:version).size == 0)
    v = ng_xml.create_element "version",
    :versionId => '1', :tag => '1.0.0'
    d = ng_xml.create_element "description", "Initial Version"
    ng_xml.root['objectId'] = pid
    ng_xml.root.add_child(v)
    v.add_child d
  else
    current = current_version_node
    current_id = current[:versionId].to_i
    current_tag = VersionTag.parse(current[:tag])

    v = ng_xml.create_element "version", :versionId => (current_id + 1).to_s
    if(significance && current_tag)
      v[:tag] = current_tag.increment(significance).to_s
    end
    ng_xml.root['objectId'] = pid
    ng_xml.root.add_child(v)

    if(description)
      d = ng_xml.create_element "description", description
      v.add_child d
    end
  end
end

#tag_for_version(versionId) ⇒ Object


174
175
176
177
178
179
180
181
# File 'lib/dor/datastreams/version_metadata_ds.rb', line 174

def tag_for_version(versionId)
  nodes=self.ng_xml.search('//version[@versionId=\''+versionId+'\']')
  if nodes.length == 1
    nodes.first['tag'].to_s
  else
    ''
  end
end

#update_current_version(opts = {}) ⇒ Object

Options Hash (opts):

  • :description (String)

    describes the version change

  • :significance (Symbol)

    which part of the version tag to increment :major, :minor, :admin (see VersionTag#increment)


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/dor/datastreams/version_metadata_ds.rb', line 124

def update_current_version(opts = {})
  ng_xml.root['objectId'] = pid
  return if find_by_terms(:version).size == 1
  return if opts.empty?
  current = current_version_node
  if(opts.include? :description)
    d = current.at_xpath('description')
    if(d)
      d.content = opts[:description]
    else
      d_node = ng_xml.create_element "description", opts[:description]
      current.add_child d_node
    end
  end
  if(opts.include? :significance)
    # tricky because if there is no tag, we have to find the newest
    if(current[:tag].nil?)
      current[:tag] = newest_tag.increment(opts[:significance]).to_s
    else
      # get rid of the current tag
      tags = find_by_terms(:version, :tag)
      sorted_tags = tags.map{|t| VersionTag.parse(t.value)}.sort
      current_tag = sorted_tags[sorted_tags.length - 2]           # Get the second greatest tag since we are dropping the current, greatest
      current[:tag] = current_tag.increment(opts[:significance]).to_s
    end

  end
  self.content = ng_xml.to_s
end