Class: Valkyrie::Storage::VersionedDisk::VersionId

Inherits:
Object
  • Object
show all
Defined in:
lib/valkyrie/storage/versioned_disk.rb

Overview

A small value class that holds a version id and methods for knowing things about it. Examples of version ids in this adapter:

* "versiondisk://te/st/test/v-current-filename.jpg" (never actually saved this way on disk, just used as a reference)
* "versiondisk://te/st/test/v-1694195675462560794-filename.jpg" (this timestamped form would be saved on disk)
* "versiondisk://te/st/test/v-1694195675462560794-deletionmarker-filename.jpg" (this file is saved on disk but empty)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ VersionId

Returns a new instance of VersionId.



134
135
136
# File 'lib/valkyrie/storage/versioned_disk.rb', line 134

def initialize(id)
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



133
134
135
# File 'lib/valkyrie/storage/versioned_disk.rb', line 133

def id
  @id
end

Instance Method Details

#current?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/valkyrie/storage/versioned_disk.rb', line 167

def current?
  version_files.first.id == id
end

#current_reference_idObject



142
143
144
# File 'lib/valkyrie/storage/versioned_disk.rb', line 142

def current_reference_id
  self.class.new(Valkyrie::ID.new(string_id.gsub(version, "current")))
end

#deletion_marker?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/valkyrie/storage/versioned_disk.rb', line 163

def deletion_marker?
  string_id.include?("deletionmarker")
end

#file_pathObject



152
153
154
# File 'lib/valkyrie/storage/versioned_disk.rb', line 152

def file_path
  @file_path ||= string_id.gsub(/^#{Regexp.escape(protocol)}/, '')
end

#filenameObject



184
185
186
# File 'lib/valkyrie/storage/versioned_disk.rb', line 184

def filename
  string_id.split("v-").last.split("-", 2).last.gsub("deletionmarker-", "")
end

#protocolObject



138
139
140
# File 'lib/valkyrie/storage/versioned_disk.rb', line 138

def protocol
  PROTOCOL
end

#reference?Boolean

Returns Whether this id is referential (e.g. “current”) or absolute (e.g. a timestamp).

Returns:

  • (Boolean)

    Whether this id is referential (e.g. “current”) or absolute (e.g. a timestamp)



172
173
174
# File 'lib/valkyrie/storage/versioned_disk.rb', line 172

def reference?
  version == "current"
end

#resolve_currentVersionID

Returns the version_id for the current file.

Returns:

  • (VersionID)

    the version_id for the current file



147
148
149
150
# File 'lib/valkyrie/storage/versioned_disk.rb', line 147

def resolve_current
  return self unless reference?
  version_files.first
end

#string_idObject



188
189
190
# File 'lib/valkyrie/storage/versioned_disk.rb', line 188

def string_id
  id.to_s
end

#versionObject



180
181
182
# File 'lib/valkyrie/storage/versioned_disk.rb', line 180

def version
  string_id.split("v-").last.split("-", 2).first
end

#version_filesObject



156
157
158
159
160
161
# File 'lib/valkyrie/storage/versioned_disk.rb', line 156

def version_files
  root = Pathname.new(file_path)
  root.parent.children.select { |file| file.basename.to_s.end_with?(filename) }.sort.reverse.map do |file|
    VersionId.new(Valkyrie::ID.new("#{protocol}#{file}"))
  end
end

#versioned?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/valkyrie/storage/versioned_disk.rb', line 176

def versioned?
  string_id.include?("v-")
end