ocfl 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1b78d16d5b12ad0e134c313c3c68b62c5d008534615d9e91f8d4acc0e4e46fbd
4
- data.tar.gz: 513c7bce75abd89c7516208b2320fb7fd302a32e2c1052ea636d069f02eef721
3
+ metadata.gz: 5b7d95d43fc87d3ce3d87f27aa4c89bf5692df05b2a144e31ba364beafa9d829
4
+ data.tar.gz: c04004d43c6147808882b8c126e218c655787469c5c0565f41714adb93cceb63
5
5
  SHA512:
6
- metadata.gz: 575c5cc29088328fb7622bbf66c82779751952b11c6a6950e8e7d2e73eb43800cd187a5b681b3f2762dc0e4fa1aad88a6c63211bae1b94bcdf5f8986189d5e86
7
- data.tar.gz: 05c1f6b0fd8819625b7fbc2209db416b2a84b6ae2383d18a1fefb5c963f3aa3e50322f1692dc009b45ec65c25e90d35c04b2911ea08b80ede22d41918158012c
6
+ metadata.gz: 408709f7faedf25af358673629d874844b915f6023ab842732f5625f17ae6903b3f39f9f7e4fa37a7ebfdae4824eab1f6b780af5ed2c5ecfcf7476555d484a13
7
+ data.tar.gz: bd5a83be543d91953d85f9ab712ebdd3f4f4c714c626afb4c92ee355351deec6e5a398fc29a087b1876fcaeebe4ae4328b2dc4a502201b491bf2b443307d678a
data/README.md CHANGED
@@ -5,15 +5,13 @@ This is an implementation of the Oxford Common File Layout (OCFL) for Ruby. See
5
5
 
6
6
  ## Installation
7
7
 
8
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
9
-
10
8
  Install the gem and add to the application's Gemfile by executing:
11
9
 
12
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
10
+ $ bundle add ocfl
13
11
 
14
12
  If bundler is not being used to manage dependencies, install the gem by executing:
15
13
 
16
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+ $ gem install ocfl
17
15
 
18
16
  ## Usage
19
17
 
@@ -42,6 +40,10 @@ directory.path("v2", "ocfl.rbs")
42
40
 
43
41
  directory.path(:head, "ocfl.rbs")
44
42
  # => <Pathname:/files/[object_root]/v2/content/ocfl.rbs>
43
+
44
+ new_version = directory.overwrite_current_version
45
+ new_version.copy_file('sig/ocfl.rbs')
46
+ new_version.save
45
47
  ```
46
48
 
47
49
  ## Development
@@ -17,7 +17,7 @@ module OCFL
17
17
 
18
18
  attr_reader :object_root, :errors
19
19
 
20
- delegate :head, to: :inventory
20
+ delegate :head, :versions, :manifest, to: :inventory
21
21
 
22
22
  def path(version, filename)
23
23
  version = head if version == :head
@@ -66,6 +66,10 @@ module OCFL
66
66
  DraftVersion.new(object_directory: self)
67
67
  end
68
68
 
69
+ def overwrite_current_version
70
+ DraftVersion.new(object_directory: self, overwrite_head: true)
71
+ end
72
+
69
73
  def exists?
70
74
  namaste_exists?
71
75
  end
@@ -7,13 +7,17 @@ module OCFL
7
7
  # A new OCFL version
8
8
  class DraftVersion
9
9
  # @params [Directory] object_directory
10
- def initialize(object_directory:)
10
+ def initialize(object_directory:, overwrite_head: false)
11
11
  @object_directory = object_directory
12
12
  @manifest = object_directory.inventory.manifest.dup
13
13
  @state = {}
14
+
15
+ number = object_directory.head.delete_prefix("v").to_i
16
+ @version_number = "v#{overwrite_head ? number : number + 1}"
17
+ @prepared_content = @prepared = overwrite_head
14
18
  end
15
19
 
16
- attr_reader :object_directory, :manifest, :state
20
+ attr_reader :object_directory, :manifest, :state, :version_number
17
21
 
18
22
  def move_file(incoming_path)
19
23
  prepare_content_directory
@@ -27,19 +31,22 @@ module OCFL
27
31
  FileUtils.cp(incoming_path, content_path)
28
32
  end
29
33
 
30
- # def copy_directory(incoming_path)
31
- # prepare_content_directory
32
- # Dir.foreach(incoming_path) do |file_name|
33
- # next if ['.', '..'].include?(file_name)
34
- # add(incoming_path)
35
- # FileUtils.cp(incoming_path, content_path)
36
- # end
37
- # end
34
+ # Copies files into the object and preserves their relative paths as logical directories in the object
35
+ def copy_recursive(incoming_path)
36
+ prepare_content_directory
37
+ incoming_path = incoming_path.delete_suffix("/")
38
+ Dir.glob("#{incoming_path}/**/*").reject { |fn| File.directory?(fn) }.each do |file|
39
+ logical_file_path = file.delete_prefix(incoming_path).delete_prefix("/")
40
+ add(file, logical_file_path:)
41
+ parent_dir = (content_path + logical_file_path).parent
42
+ FileUtils.mkdir_p(parent_dir)
43
+ FileUtils.cp(file, content_path + logical_file_path)
44
+ end
45
+ end
38
46
 
39
- def add(incoming_path)
47
+ def add(incoming_path, logical_file_path: File.basename(incoming_path))
40
48
  digest = Digest::SHA512.file(incoming_path).to_s
41
49
  version_content_path = content_path.relative_path_from(object_directory.object_root)
42
- logical_file_path = File.basename(incoming_path)
43
50
  file_path_relative_to_root = (version_content_path + logical_file_path).to_s
44
51
  @manifest[digest] = [file_path_relative_to_root]
45
52
  @state[digest] = [logical_file_path]
@@ -68,14 +75,25 @@ module OCFL
68
75
  object_directory.object_root + version_number
69
76
  end
70
77
 
71
- def version_number
72
- @version_number ||= "v#{object_directory.head.delete_prefix("v").to_i + 1}"
73
- end
74
-
75
78
  def build_inventory
76
79
  old_data = object_directory.inventory.data
77
- versions = old_data.versions.merge(version_number => Version.new(created: Time.now.utc.iso8601, state: @state))
78
- Inventory::InventoryStruct.new(old_data.to_h.merge(manifest:, head: version_number, versions:))
80
+ versions = versions(old_data.versions)
81
+ # Prune items from manifest if they are not part of any version
82
+
83
+ Inventory::InventoryStruct.new(old_data.to_h.merge(manifest: filtered_manifest(versions),
84
+ head: version_number, versions:))
85
+ end
86
+
87
+ # This gives the update list of versions. The old list plus this new one.
88
+ # @param [Hash] old_versions the versions prior to this one.
89
+ def versions(old_versions)
90
+ old_versions.merge(version_number => Version.new(created: Time.now.utc.iso8601, state: @state))
91
+ end
92
+
93
+ # The manifest after unused SHAs have been filtered out.
94
+ def filtered_manifest(versions)
95
+ shas_in_versions = versions.values.flat_map { |v| v.state.keys }.uniq
96
+ manifest.slice(*shas_in_versions)
79
97
  end
80
98
 
81
99
  def save
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.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ocfl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Coyne
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-12 00:00:00.000000000 Z
11
+ date: 2024-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport