Class: MiniTarball::HeaderFields

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_tarball/header_fields.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header) ⇒ HeaderFields

Returns a new instance of HeaderFields.



9
10
11
12
# File 'lib/mini_tarball/header_fields.rb', line 9

def initialize(header)
  @header = header
  @values_by_field = {}
end

Class Method Details

.pack_formatObject



5
6
7
# File 'lib/mini_tarball/header_fields.rb', line 5

def self.pack_format
  @pack_format ||= Header::FIELDS.values.map { |field| "a#{field[:length]}" }.join("")
end

Instance Method Details

#set_value(name, value) ⇒ Object

:reek:DuplicateMethodCall



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mini_tarball/header_fields.rb', line 25

def set_value(name, value)
  field = Header::FIELDS[name]

  case field[:type]
  when :number
    @values_by_field[name] = HeaderFormatter.format_number(value, field[:length])
  when :mode
    @values_by_field[name] = HeaderFormatter.format_permissions(value, field[:length])
  when :checksum
    @values_by_field[name] = HeaderFormatter.format_checksum(value)
  else
    @values_by_field[name] = value
  end
end

#to_binaryObject



14
15
16
17
18
19
20
21
22
# File 'lib/mini_tarball/header_fields.rb', line 14

def to_binary
  Header::FIELDS.each_key do |name|
    value = @header.value_of(name)
    set_value(name, value)
  end

  update_checksum
  HeaderFormatter.zero_pad(encode_fields)
end

#update_checksumObject



40
41
42
43
# File 'lib/mini_tarball/header_fields.rb', line 40

def update_checksum
  checksum = encode_fields.unpack("C*").sum
  set_value(:checksum, checksum)
end