Class: Omnizip::Metadata::EntryMetadata

Inherits:
Object
  • Object
show all
Includes:
Entry
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:



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

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

Instance Attribute Details

#entryObject (readonly)

Returns the value of attribute entry.



10
11
12
# File 'lib/omnizip/metadata/entry_metadata.rb', line 10

def entry
  @entry
end

Instance Method Details

#attributesInteger

Get external attributes

Returns:

  • (Integer)

    External attributes



80
81
82
# File 'lib/omnizip/metadata/entry_metadata.rb', line 80

def attributes
  entry.header.external_attributes
end

#attributes=(value) ⇒ Object

Set external attributes

Parameters:

  • value (Integer, Symbol)

    External attributes or symbol like :readonly



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/omnizip/metadata/entry_metadata.rb', line 86

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



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

def comment
  entry.comment
end

#comment=(value) ⇒ Object

Set entry comment

Parameters:

  • value (String)

    New comment



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

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

#entry_directory?Boolean

Returns:

  • (Boolean)


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

def entry_directory? = entry.directory?

#entry_mtimeObject



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

def entry_mtime = entry.time

#entry_nameObject



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

def entry_name = entry.name

#entry_sizeObject



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

def entry_size = entry.size

#modified?Boolean

Check if metadata has been modified

Returns:

  • (Boolean)

    True if modified



100
101
102
# File 'lib/omnizip/metadata/entry_metadata.rb', line 100

def modified?
  @modified
end

#mtimeTime

Get modification time

Returns:

  • (Time)

    Modification time



39
40
41
# File 'lib/omnizip/metadata/entry_metadata.rb', line 39

def mtime
  entry.time
end

#mtime=(value) ⇒ Object

Set modification time

Parameters:

  • value (Time)

    New modification time



45
46
47
48
49
50
51
52
53
54
# File 'lib/omnizip/metadata/entry_metadata.rb', line 45

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



105
106
107
# File 'lib/omnizip/metadata/entry_metadata.rb', line 105

def reset_modified
  @modified = false
end

#to_hHash

Get all metadata as a hash

Returns:

  • (Hash)

    Metadata hash



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/omnizip/metadata/entry_metadata.rb', line 111

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)



58
59
60
# File 'lib/omnizip/metadata/entry_metadata.rb', line 58

def unix_permissions
  entry.unix_perms
end

#unix_permissions=(perms) ⇒ Object

Set Unix permissions

Parameters:

  • perms (Integer)

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



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/omnizip/metadata/entry_metadata.rb', line 64

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