Class: Omnizip::Metadata::EntryMetadata

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

Overview

Model for file entry metadata Wraps CentralDirectoryHeader with a cleaner metadata API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry) ⇒ EntryMetadata

Initialize metadata for an entry

Parameters:



12
13
14
15
# File 'lib/omnizip/metadata/entry_metadata.rb', line 12

def initialize(entry)
  @entry = entry
  @modified = false
end

Instance Attribute Details

#entryObject (readonly)

Returns the value of attribute entry.



8
9
10
# File 'lib/omnizip/metadata/entry_metadata.rb', line 8

def entry
  @entry
end

Instance Method Details

#attributesInteger

Get external attributes

Returns:

  • (Integer)

    External attributes



73
74
75
# File 'lib/omnizip/metadata/entry_metadata.rb', line 73

def attributes
  entry.header.external_attributes
end

#attributes=(value) ⇒ Object

Set external attributes

Parameters:

  • value (Integer, Symbol)

    External attributes or symbol like :readonly



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/omnizip/metadata/entry_metadata.rb', line 79

def attributes=(value)
  case value
  when Integer
    entry.header.external_attributes = value
  when Symbol
    set_attribute_flag(value)
  else
    raise ArgumentError, "attributes must be Integer or Symbol"
  end
  @modified = true
end

#commentString

Get entry comment

Returns:

  • (String)

    Entry comment



19
20
21
# File 'lib/omnizip/metadata/entry_metadata.rb', line 19

def comment
  entry.comment
end

#comment=(value) ⇒ Object

Set entry comment

Parameters:

  • value (String)

    New comment



25
26
27
28
# File 'lib/omnizip/metadata/entry_metadata.rb', line 25

def comment=(value)
  entry.comment = value.to_s
  @modified = true
end

#modified?Boolean

Check if metadata has been modified

Returns:

  • (Boolean)

    True if modified



93
94
95
# File 'lib/omnizip/metadata/entry_metadata.rb', line 93

def modified?
  @modified
end

#mtimeTime

Get modification time

Returns:

  • (Time)

    Modification time



32
33
34
# File 'lib/omnizip/metadata/entry_metadata.rb', line 32

def mtime
  entry.time
end

#mtime=(value) ⇒ Object

Set modification time

Parameters:

  • value (Time)

    New modification time



38
39
40
41
42
43
44
45
46
47
# File 'lib/omnizip/metadata/entry_metadata.rb', line 38

def mtime=(value)
  unless value.is_a?(Time)
    raise ArgumentError,
          "mtime must be a Time object"
  end

  entry.header.last_mod_time = dos_time(value)
  entry.header.last_mod_date = dos_date(value)
  @modified = true
end

#reset_modifiedObject

Reset modified flag



98
99
100
# File 'lib/omnizip/metadata/entry_metadata.rb', line 98

def reset_modified
  @modified = false
end

#to_hHash

Get all metadata as a hash

Returns:

  • (Hash)

    Metadata hash



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/omnizip/metadata/entry_metadata.rb', line 104

def to_h
  {
    name: entry.name,
    comment: comment,
    mtime: mtime,
    unix_permissions: unix_permissions,
    size: entry.size,
    compressed_size: entry.compressed_size,
    crc: entry.crc,
    directory: entry.directory?,
  }
end

#unix_permissionsInteger

Get Unix permissions

Returns:

  • (Integer)

    Unix permissions (e.g., 0644)



51
52
53
# File 'lib/omnizip/metadata/entry_metadata.rb', line 51

def unix_permissions
  entry.unix_perms
end

#unix_permissions=(perms) ⇒ Object

Set Unix permissions

Parameters:

  • perms (Integer)

    Unix permissions (e.g., 0644, 0755)



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/omnizip/metadata/entry_metadata.rb', line 57

def unix_permissions=(perms)
  unless perms.is_a?(Integer)
    raise ArgumentError,
          "permissions must be an integer"
  end
  unless (0..0o777).cover?(perms)
    raise ArgumentError,
          "permissions out of range"
  end

  entry.unix_perms = perms
  @modified = true
end