Class: OCFL::Object

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

Overview

An OCFL Object is a group of one or more content files and administrative information ocfl.io/1.1/spec/#object-spec

Defined Under Namespace

Classes: FileNotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root:, identifier:, inventory: nil, content_directory: nil) ⇒ Object

Returns a new instance of Object.

Parameters:

  • identifier (String)

    an object identifier

  • root (Pathname, String)

    the path to the object root within the OCFL structure

  • inventory (Inventory, nil) (defaults to: nil)

    this is only passed in when creating a new version

  • content_directory (String, nil) (defaults to: nil)

    the directory to store versions in



13
14
15
16
17
18
19
20
# File 'lib/ocfl/object.rb', line 13

def initialize(root:, identifier:, inventory: nil, content_directory: nil)
  @identifier = identifier
  @root = Pathname.new(root)
  @content_directory = content_directory
  @version_inventory = {}
  @version_inventory_errors = {}
  @inventory = inventory
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



22
23
24
# File 'lib/ocfl/object.rb', line 22

def errors
  @errors
end

#identifierObject (readonly)

Returns the value of attribute identifier.



22
23
24
# File 'lib/ocfl/object.rb', line 22

def identifier
  @identifier
end

#rootObject (readonly)

Returns the value of attribute root.



22
23
24
# File 'lib/ocfl/object.rb', line 22

def root
  @root
end

Instance Method Details

#begin_new_versionObject

Start a completely new version



82
83
84
# File 'lib/ocfl/object.rb', line 82

def begin_new_version
  VersionBuilder.new(object: self, state:)
end

#exists?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/ocfl/object.rb', line 26

def exists?
  namaste_file.exist?
end

#head_directory_valid?Boolean

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/ocfl/object.rb', line 76

def head_directory_valid?
  InventoryValidator.new(directory: root / inventory.head).valid? &&
    !head_inventory.nil? # Ensures it could be loaded
end

#head_inventoryObject



52
53
54
# File 'lib/ocfl/object.rb', line 52

def head_inventory
  version_inventory(inventory.head)
end

#head_versionObject

Get a handle for the head version



87
88
89
# File 'lib/ocfl/object.rb', line 87

def head_version
  VersionBuilder.new(object: self, overwrite_head: true, state: head_inventory.state)
end

#inventoryObject



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

def inventory
  @inventory ||= begin
    maybe_inventory, inventory_loading_errors = load_or_initialize_inventory
    if maybe_inventory
      maybe_inventory
    else
      @errors = inventory_loading_errors
      puts @errors.messages.inspect
      nil
    end
  end
end

#namaste_fileObject



104
105
106
# File 'lib/ocfl/object.rb', line 104

def namaste_file
  root / "0=ocfl_object_1.1"
end

#overwrite_current_versionObject

Get a handle that will replace the existing head version



92
93
94
# File 'lib/ocfl/object.rb', line 92

def overwrite_current_version
  VersionBuilder.new(object: self, overwrite_head: true)
end

#path(filepath:, version: nil) ⇒ Object

Raises:



30
31
32
33
34
35
36
37
# File 'lib/ocfl/object.rb', line 30

def path(filepath:, version: nil)
  version ||= head
  relative_path = version_inventory(version).path(filepath)

  raise FileNotFound, "Path '#{filepath}' not found in #{version} inventory" if relative_path.nil?

  root / relative_path
end

#reloadObject



96
97
98
99
100
101
102
# File 'lib/ocfl/object.rb', line 96

def reload
  @version_inventory = {}
  @inventory = nil
  @errors = nil
  @version_inventory_errors = {}
  true
end

#valid?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
# File 'lib/ocfl/object.rb', line 69

def valid?
  InventoryValidator.new(directory: root).valid? &&
    exists? &&
    !inventory.nil? && # Ensures it could be loaded
    head_directory_valid?
end

#version_inventory(version) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ocfl/object.rb', line 56

def version_inventory(version)
  @version_inventory[version] ||= begin
    maybe_inventory, inventory_loading_errors = load_or_initialize_inventory(version:)
    if maybe_inventory
      maybe_inventory
    else
      @version_inventory_errors[version] = inventory_loading_errors
      puts @version_inventory_errors[version].messages.inspect
      nil
    end
  end
end