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.



127
128
129
# File 'lib/valkyrie/storage/versioned_disk.rb', line 127

def initialize(id)
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



126
127
128
# File 'lib/valkyrie/storage/versioned_disk.rb', line 126

def id
  @id
end

Instance Method Details

#current?Boolean

Returns:

  • (Boolean)


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

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

#current_reference_idObject



131
132
133
# File 'lib/valkyrie/storage/versioned_disk.rb', line 131

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

#deletion_marker?Boolean

Returns:

  • (Boolean)


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

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

#file_pathObject



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

def file_path
  @file_path ||= string_id.gsub(/^versiondisk:\/\//, '')
end

#filenameObject



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

def filename
  string_id.split("v-").last.split("-", 2).last.gsub("deletionmarker-", "")
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)



161
162
163
# File 'lib/valkyrie/storage/versioned_disk.rb', line 161

def reference?
  version == "current"
end

#resolve_currentVersionID

Returns the version_id for the current file.

Returns:

  • (VersionID)

    the version_id for the current file



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

def resolve_current
  return self unless reference?
  version_files.first
end

#string_idObject



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

def string_id
  id.to_s
end

#versionObject



169
170
171
# File 'lib/valkyrie/storage/versioned_disk.rb', line 169

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

#version_filesObject



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

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("versiondisk://#{file}"))
  end
end

#versioned?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/valkyrie/storage/versioned_disk.rb', line 165

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