Class: Omnizip::Metadata::MetadataEditor
- Inherits:
-
Object
- Object
- Omnizip::Metadata::MetadataEditor
- Defined in:
- lib/omnizip/metadata/metadata_editor.rb
Overview
Coordinates metadata editing operations
Instance Attribute Summary collapse
-
#archive ⇒ Object
readonly
Returns the value of attribute archive.
-
#validator ⇒ Object
readonly
Returns the value of attribute validator.
Instance Method Summary collapse
-
#clear_changes ⇒ Object
Clear pending changes.
-
#commit ⇒ Object
Commit all changes to archive This marks the archive as modified and saves on close.
-
#initialize(archive) ⇒ MetadataEditor
constructor
Initialize metadata editor.
-
#modified? ⇒ Boolean
Check if there are pending changes.
-
#normalize_permissions(file_perms: 0o644, dir_perms: 0o755) ⇒ Object
Normalize permissions (set sensible defaults).
-
#pending_changes ⇒ Array<Hash>
Get list of pending changes.
-
#preserve_relative_timestamps(base_time = Time.now) ⇒ Object
Update timestamps to preserve relative ordering Useful for maintaining build timestamps while updating absolute times.
-
#set_all_timestamps(time, &filter) ⇒ Object
Set timestamps for all entries.
-
#set_comment_matching(pattern, comment) ⇒ Object
Set comment for entries matching a pattern.
-
#set_permissions_matching(pattern, perms) ⇒ Object
Set permissions for entries matching a pattern.
-
#strip_comments ⇒ Object
Strip all comments from entries and archive.
Constructor Details
#initialize(archive) ⇒ MetadataEditor
Initialize metadata editor
11 12 13 14 15 |
# File 'lib/omnizip/metadata/metadata_editor.rb', line 11 def initialize(archive) @archive = archive @validator = MetadataValidator.new @changes = [] end |
Instance Attribute Details
#archive ⇒ Object (readonly)
Returns the value of attribute archive.
7 8 9 |
# File 'lib/omnizip/metadata/metadata_editor.rb', line 7 def archive @archive end |
#validator ⇒ Object (readonly)
Returns the value of attribute validator.
7 8 9 |
# File 'lib/omnizip/metadata/metadata_editor.rb', line 7 def validator @validator end |
Instance Method Details
#clear_changes ⇒ Object
Clear pending changes
153 154 155 156 |
# File 'lib/omnizip/metadata/metadata_editor.rb', line 153 def clear_changes @changes = [] self end |
#commit ⇒ Object
Commit all changes to archive This marks the archive as modified and saves on close
160 161 162 163 164 165 166 |
# File 'lib/omnizip/metadata/metadata_editor.rb', line 160 def commit # Changes are already applied to entries # Just mark archive as modified archive.modified! if modified? @changes = [] self end |
#modified? ⇒ Boolean
Check if there are pending changes
148 149 150 |
# File 'lib/omnizip/metadata/metadata_editor.rb', line 148 def modified? !@changes.empty? end |
#normalize_permissions(file_perms: 0o644, dir_perms: 0o755) ⇒ Object
Normalize permissions (set sensible defaults)
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/omnizip/metadata/metadata_editor.rb', line 39 def (file_perms: 0o644, dir_perms: 0o755) validator.(file_perms) validator.(dir_perms) archive.entries.each do |entry| = EntryMetadata.new(entry) perms = entry.directory? ? dir_perms : file_perms . = perms @changes << { entry: entry, field: :permissions, value: perms } end self end |
#pending_changes ⇒ Array<Hash>
Get list of pending changes
142 143 144 |
# File 'lib/omnizip/metadata/metadata_editor.rb', line 142 def pending_changes @changes end |
#preserve_relative_timestamps(base_time = Time.now) ⇒ Object
Update timestamps to preserve relative ordering Useful for maintaining build timestamps while updating absolute times
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/omnizip/metadata/metadata_editor.rb', line 118 def (base_time = Time.now) validator.validate_time(base_time) # Find earliest timestamp earliest = archive.entries.map(&:time).min return self unless earliest # Calculate offset offset = base_time.to_i - earliest.to_i archive.entries.each do |entry| next if entry.directory? new_time = Time.at(entry.time.to_i + offset) = EntryMetadata.new(entry) .mtime = new_time @changes << { entry: entry, field: :mtime, value: new_time } end self end |
#set_all_timestamps(time, &filter) ⇒ Object
Set timestamps for all entries
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/omnizip/metadata/metadata_editor.rb', line 20 def (time, &filter) validator.validate_time(time) entries = filter ? archive.entries.select(&filter) : archive.entries entries.each do |entry| next if entry.directory? = EntryMetadata.new(entry) .mtime = time @changes << { entry: entry, field: :mtime, value: time } end self end |
#set_comment_matching(pattern, comment) ⇒ Object
Set comment for entries matching a pattern
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/omnizip/metadata/metadata_editor.rb', line 70 def set_comment_matching(pattern, comment) validator.validate_comment(comment) matching_entries = if pattern.is_a?(Regexp) archive.entries.select { |e| e.name =~ pattern } else archive.entries.select do |e| ::File.fnmatch(pattern, e.name) end end matching_entries.each do |entry| = EntryMetadata.new(entry) .comment = comment @changes << { entry: entry, field: :comment, value: comment } end self end |
#set_permissions_matching(pattern, perms) ⇒ Object
Set permissions for entries matching a pattern
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/omnizip/metadata/metadata_editor.rb', line 93 def (pattern, perms) validator.(perms) matching_entries = if pattern.is_a?(Regexp) archive.entries.select { |e| e.name =~ pattern } else archive.entries.select do |e| ::File.fnmatch(pattern, e.name) end end matching_entries.each do |entry| next if entry.directory? = EntryMetadata.new(entry) . = perms @changes << { entry: entry, field: :permissions, value: perms } end self end |
#strip_comments ⇒ Object
Strip all comments from entries and archive
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/omnizip/metadata/metadata_editor.rb', line 54 def strip_comments archive.entries.each do |entry| = EntryMetadata.new(entry) .comment = "" @changes << { entry: entry, field: :comment, value: "" } end archive.comment = "" @changes << { entry: :archive, field: :comment, value: "" } self end |