Class: Omnizip::Metadata::MetadataEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/metadata/metadata_editor.rb

Overview

Coordinates metadata editing operations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(archive) ⇒ MetadataEditor

Initialize metadata editor

Parameters:



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

#archiveObject (readonly)

Returns the value of attribute archive.



7
8
9
# File 'lib/omnizip/metadata/metadata_editor.rb', line 7

def archive
  @archive
end

#validatorObject (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_changesObject

Clear pending changes



153
154
155
156
# File 'lib/omnizip/metadata/metadata_editor.rb', line 153

def clear_changes
  @changes = []
  self
end

#commitObject

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

Returns:

  • (Boolean)

    True if there are 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)

Parameters:

  • file_perms (Integer) (defaults to: 0o644)

    Permissions for files (default: 0644)

  • dir_perms (Integer) (defaults to: 0o755)

    Permissions for directories (default: 0755)



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/omnizip/metadata/metadata_editor.rb', line 39

def normalize_permissions(file_perms: 0o644, dir_perms: 0o755)
  validator.validate_permissions(file_perms)
  validator.validate_permissions(dir_perms)

  archive.entries.each do |entry|
     = EntryMetadata.new(entry)
    perms = entry.directory? ? dir_perms : file_perms
    .unix_permissions = perms
    @changes << { entry: entry, field: :permissions, value: perms }
  end

  self
end

#pending_changesArray<Hash>

Get list of pending changes

Returns:

  • (Array<Hash>)

    List of 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

Parameters:

  • base_time (Time) (defaults to: Time.now)

    Base time for earliest file



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 preserve_relative_timestamps(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

Parameters:

  • time (Time)

    Time to set

  • filter (Proc)

    Optional filter to select 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 set_all_timestamps(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

Parameters:

  • pattern (String, Regexp)

    Pattern to match

  • comment (String)

    Comment to set



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

Parameters:

  • pattern (String, Regexp)

    Pattern to match

  • perms (Integer)

    Permissions to set



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 set_permissions_matching(pattern, perms)
  validator.validate_permissions(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)
    .unix_permissions = perms
    @changes << { entry: entry, field: :permissions, value: perms }
  end

  self
end

#strip_commentsObject

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