Class: OCFL::VersionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ocfl/version_builder.rb

Overview

Build a new version

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object:, overwrite_head: false, state: {}) ⇒ VersionBuilder

Returns a new instance of VersionBuilder.



7
8
9
10
11
12
13
14
15
# File 'lib/ocfl/version_builder.rb', line 7

def initialize(object:, overwrite_head: false, state: {})
  @object = object
  @manifest = object.inventory.manifest.dup
  @state = state

  number = object.head.delete_prefix("v").to_i
  @version_number = "v#{overwrite_head ? number : number + 1}"
  @prepared_content = @prepared = overwrite_head
end

Instance Attribute Details

#manifestObject (readonly)

Returns the value of attribute manifest.



17
18
19
# File 'lib/ocfl/version_builder.rb', line 17

def manifest
  @manifest
end

#objectObject (readonly)

Returns the value of attribute object.



17
18
19
# File 'lib/ocfl/version_builder.rb', line 17

def object
  @object
end

#stateObject (readonly)

Returns the value of attribute state.



17
18
19
# File 'lib/ocfl/version_builder.rb', line 17

def state
  @state
end

#version_numberObject (readonly)

Returns the value of attribute version_number.



17
18
19
# File 'lib/ocfl/version_builder.rb', line 17

def version_number
  @version_number
end

Instance Method Details

#copy_file(incoming_path, destination_path: "") ⇒ Object



29
30
31
32
# File 'lib/ocfl/version_builder.rb', line 29

def copy_file(incoming_path, destination_path: "")
  prepare_content_directory
  copy_one(destination_path.presence || File.basename(incoming_path), incoming_path)
end

#copy_recursive(incoming_path, destination_path: "") ⇒ Object

Copies files into the object and preserves their relative paths as logical directories in the object



52
53
54
55
56
57
58
59
60
61
# File 'lib/ocfl/version_builder.rb', line 52

def copy_recursive(incoming_path, destination_path: "")
  prepare_content_directory
  incoming_path = incoming_path.delete_suffix("/")
  Dir.glob("#{incoming_path}/**/*").reject { |fn| File.directory?(fn) }.each do |file|
    logical_file_path = file.delete_prefix(incoming_path).delete_prefix("/")
    logical_file_path = File.join(destination_path, logical_file_path) unless destination_path.empty?

    copy_one(logical_file_path, file)
  end
end

#delete_file(filename) ⇒ Object

Note, this only removes the file from this version. Previous versions may still use it.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ocfl/version_builder.rb', line 39

def delete_file(filename)
  sha512_digest = digest_for_filename(filename)
  raise "Unknown file: #{filename}" unless sha512_digest

  state.delete(sha512_digest)
  # If the manifest points at the current content directory, then we can delete it.
  file_paths = manifest[sha512_digest]
  return unless file_paths.all? { |path| path.start_with?("#{version_number}/") }

  File.unlink (object.root + file_paths.first).to_s
end

#digest_for_filename(filename) ⇒ Object



34
35
36
# File 'lib/ocfl/version_builder.rb', line 34

def digest_for_filename(filename)
  state.find { |_, filenames| filenames.include?(filename) }&.first
end

#move_file(incoming_path) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/ocfl/version_builder.rb', line 21

def move_file(incoming_path)
  prepare_content_directory
  already_stored = add(incoming_path)
  return if already_stored

  FileUtils.mv(incoming_path, content_path)
end

#saveObject



63
64
65
66
67
# File 'lib/ocfl/version_builder.rb', line 63

def save
  prepare_directory # only necessary if the version has no new content (deletes only)
  write_inventory(build_inventory)
  object.reload
end