ocfl 0.7.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 04033b37955d5ee76f304558abce0d860317fbd019e4a84ac033e2e4b4a89b49
4
- data.tar.gz: 24aa1a8705a985d88900f30fd59c2e19be305edb3b710293330cfb6607fb6e46
3
+ metadata.gz: c21578c64e68d5bdab7fcea3b08bf8210a967a52d0d6b0f2ecdc203a4f2eaca4
4
+ data.tar.gz: 48503dd0f9011c88bbf20bc6adf073d2b1ff422ac1ed06063c332f7c3bbf619e
5
5
  SHA512:
6
- metadata.gz: ae4c04c535047ff07f7123787cc08efa6cf2f56c5bf53f2a21366bf81f0474dfcbc35c48802c52c920c1a36d8f1bd77bc7558c4f0ac75e17afba0c131fd9dd45
7
- data.tar.gz: cf9d0d5037ecb46946c9f8eab7407097cc0330f26d45566077ce83e61371cf1ebf8d906e13ee8ee003b40f9e2fb6b11b31d70e6de194a3f895503ccfc3947466
6
+ metadata.gz: 74169c05ca7d91082db0198d252234956c7a47139d57419f5be14e8e23168af39e75b9d9e78d397bd1b65fbc688834ee84cd1456d452795163c777c6c7da15c7
7
+ data.tar.gz: acd185ad8a09d1b016d9c6867d5b4b68005fbe1227abe8848912f917c3d71f4d535cd1847cc4422606fec0d5fc01b9dbf59b6b613c4e5701dfca1703ebae79dc
data/.rubocop.yml CHANGED
@@ -22,6 +22,9 @@ Metrics/BlockLength:
22
22
  - describe
23
23
  - context
24
24
 
25
+ Metrics/ClassLength:
26
+ Max: 110
27
+
25
28
  RSpec/MultipleExpectations:
26
29
  Enabled: false
27
30
 
data/README.md CHANGED
@@ -20,7 +20,7 @@ directory = OCFL::Object::Directory.new(object_root: '/files/[object_root]')
20
20
  directory.exists?
21
21
  # => false
22
22
  builder = OCFL::Object::DirectoryBuilder.new(object_root: 'spec/abc123', id: 'http://example.com/abc123')
23
- builder.copy_file('sig/ocfl.rbs')
23
+ builder.copy_file('sig/ocfl.rbs', destination_path: 'ocfl/types/generated.rbs')
24
24
 
25
25
  directory = builder.save
26
26
  directory.exists?
@@ -43,10 +43,10 @@ directory.head
43
43
  # => 'v2'
44
44
  ```
45
45
 
46
- #### Re-open the existing head version
46
+ #### Modify the existing head version
47
47
  ```
48
- new_version = directory.reopen_head_version
49
- new_version.delete_file('cb6c8557fc724c636929775212c5194984d68cb1508a1')
48
+ new_version = directory.head_version
49
+ new_version.delete_file('sample.txt')
50
50
  new_version.copy_file('sig/ocfl.rbs')
51
51
  new_version.save
52
52
  ```
@@ -64,6 +64,10 @@ new_version.save
64
64
  directory.versions['v2'].file_names
65
65
  # => ["ocfl.rbs"]
66
66
 
67
+ # Or on the head version
68
+ directory.head_version.file_names
69
+ # => ["ocfl.rbs"]
70
+
67
71
  # Get the path of a file in a given version
68
72
  directory.path(filepath: "ocfl.rbs", version: "v2")
69
73
  # => <Pathname:/files/[object_root]/v2/content/ocfl.rbs>
@@ -70,7 +70,7 @@ module OCFL
70
70
  end
71
71
 
72
72
  # Get a handle for the head version
73
- def reopen_head_version
73
+ def head_version
74
74
  DraftVersion.new(object_directory: self, overwrite_head: true, state: head_inventory.state)
75
75
  end
76
76
 
@@ -33,12 +33,12 @@ module OCFL
33
33
 
34
34
  def create_object_directory
35
35
  FileUtils.mkdir_p(object_root)
36
+ FileUtils.touch(object_directory.namaste_file) unless File.exist?(object_directory.namaste_file)
36
37
  end
37
38
 
38
39
  # @return [Directory]
39
40
  def save
40
- FileUtils.mkdir_p(object_root)
41
- FileUtils.touch(object_directory.namaste_file)
41
+ create_object_directory
42
42
  write_inventory
43
43
  object_directory
44
44
  end
@@ -17,19 +17,30 @@ module OCFL
17
17
 
18
18
  attr_reader :object_directory, :manifest, :state, :version_number
19
19
 
20
+ delegate :file_names, to: :to_version_struct
21
+
20
22
  def move_file(incoming_path)
21
23
  prepare_content_directory
22
- add(incoming_path)
24
+ already_stored = add(incoming_path)
25
+ return if already_stored
26
+
23
27
  FileUtils.mv(incoming_path, content_path)
24
28
  end
25
29
 
26
30
  def copy_file(incoming_path, destination_path: "")
27
31
  prepare_content_directory
28
- copy_one(File.basename(incoming_path), incoming_path, destination_path)
32
+ copy_one(destination_path.presence || File.basename(incoming_path), incoming_path)
33
+ end
34
+
35
+ def digest_for_filename(filename)
36
+ state.find { |_, filenames| filenames.include?(filename) }&.first
29
37
  end
30
38
 
31
39
  # Note, this only removes the file from this version. Previous versions may still use it.
32
- def delete_file(sha512_digest)
40
+ def delete_file(filename)
41
+ sha512_digest = digest_for_filename(filename)
42
+ raise "Unknown file: #{filename}" unless sha512_digest
43
+
33
44
  state.delete(sha512_digest)
34
45
  # If the manifest points at the current content directory, then we can delete it.
35
46
  file_paths = manifest[sha512_digest]
@@ -43,7 +54,10 @@ module OCFL
43
54
  prepare_content_directory
44
55
  incoming_path = incoming_path.delete_suffix("/")
45
56
  Dir.glob("#{incoming_path}/**/*").reject { |fn| File.directory?(fn) }.each do |file|
46
- copy_one(file.delete_prefix(incoming_path).delete_prefix("/"), file, destination_path)
57
+ logical_file_path = file.delete_prefix(incoming_path).delete_prefix("/")
58
+ logical_file_path = File.join(destination_path, logical_file_path) unless destination_path.empty?
59
+
60
+ copy_one(logical_file_path, file)
47
61
  end
48
62
  end
49
63
 
@@ -53,6 +67,10 @@ module OCFL
53
67
  object_directory.reload
54
68
  end
55
69
 
70
+ def to_version_struct
71
+ Version.new(state:, created: Time.now.utc.iso8601)
72
+ end
73
+
56
74
  private
57
75
 
58
76
  def write_inventory(inventory)
@@ -61,20 +79,29 @@ module OCFL
61
79
  FileUtils.cp(path / "inventory.json.sha512", object_directory.object_root)
62
80
  end
63
81
 
64
- def copy_one(logical_file_path, incoming_path, destination_path)
65
- logical_file_path = File.join(destination_path, logical_file_path) unless destination_path.empty?
66
- add(incoming_path, logical_file_path:)
82
+ # @param [String] logical_file_path where we're going to store the file (e.g. 'object/directory_builder_spec.rb')
83
+ # @param [String] incoming_path where's this file from (e.g. 'spec/ocfl/object/directory_builder_spec.rb')
84
+ def copy_one(logical_file_path, incoming_path)
85
+ already_stored = add(incoming_path, logical_file_path:)
86
+ return if already_stored
87
+
67
88
  parent_dir = (content_path / logical_file_path).parent
68
89
  FileUtils.mkdir_p(parent_dir) unless parent_dir == content_path
69
90
  FileUtils.cp(incoming_path, content_path / logical_file_path)
70
91
  end
71
92
 
93
+ # @return [Boolean] true if the file already existed in this object. If false, the object must be
94
+ # moved to the content directory.
72
95
  def add(incoming_path, logical_file_path: File.basename(incoming_path))
73
96
  digest = Digest::SHA512.file(incoming_path).to_s
74
97
  version_content_path = content_path.relative_path_from(object_directory.object_root)
75
98
  file_path_relative_to_root = (version_content_path / logical_file_path).to_s
76
- @manifest[digest] = [file_path_relative_to_root]
77
- @state[digest] = [logical_file_path]
99
+ result = @manifest.key?(digest)
100
+ @manifest[digest] ||= []
101
+ @state[digest] ||= []
102
+ @manifest[digest].push(file_path_relative_to_root)
103
+ @state[digest].push(logical_file_path)
104
+ result
78
105
  end
79
106
 
80
107
  def prepare_content_directory
@@ -112,7 +139,7 @@ module OCFL
112
139
  # This gives the update list of versions. The old list plus this new one.
113
140
  # @param [Hash] old_versions the versions prior to this one.
114
141
  def versions(old_versions)
115
- old_versions.merge(version_number => Version.new(created: Time.now.utc.iso8601, state: @state))
142
+ old_versions.merge(version_number => to_version_struct)
116
143
  end
117
144
 
118
145
  # The manifest after unused SHAs have been filtered out.
data/lib/ocfl/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OCFL
4
- VERSION = "0.7.0"
4
+ VERSION = "0.8.1"
5
5
  end
data/lib/ocfl.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require "zeitwerk"
4
4
  require "active_support"
5
5
  require "active_support/core_ext/module/delegation"
6
+ require "active_support/core_ext/object/blank"
6
7
  require "digest"
7
8
  require "dry/monads"
8
9
  require "dry-schema"
data/tmp/.keep ADDED
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ocfl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Coyne
@@ -103,6 +103,7 @@ files:
103
103
  - lib/ocfl/object/version.rb
104
104
  - lib/ocfl/version.rb
105
105
  - sig/ocfl.rbs
106
+ - tmp/.keep
106
107
  homepage: https://github.com/sul-dlss/ocfl-rb
107
108
  licenses: []
108
109
  metadata: