Class: Tar::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/tar/schema.rb

Defined Under Namespace

Classes: FieldType, OctalNumber, String, Timestamp

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Schema

Returns a new instance of Schema.



5
6
7
8
9
10
11
12
# File 'lib/tar/schema.rb', line 5

def initialize(&block)
  @fields = {}
  @offset = 0
  instance_eval(&block)
  @fields.freeze

  @unpack_format = @fields.values.map(&:unpack_format).join.freeze
end

Instance Method Details

#clear(record, field_name) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/tar/schema.rb', line 18

def clear(record, field_name)
  field = @fields.fetch(field_name)

  record.dup.tap { |new_record|
    new_record[field.offset, field.size] = " " * field.size
  }
end

#field_namesObject



14
15
16
# File 'lib/tar/schema.rb', line 14

def field_names
  @fields.keys
end

#octal_number(name, size) ⇒ Object



36
37
38
# File 'lib/tar/schema.rb', line 36

def octal_number(name, size)
  add_field name, OctalNumber, size
end

#parse(record) ⇒ Object



26
27
28
29
30
# File 'lib/tar/schema.rb', line 26

def parse(record)
  @fields.zip(record.unpack(@unpack_format))
         .map { |(name, type), value| [name, type.parse(value)] }
         .to_h
end

#string(name, size) ⇒ Object



32
33
34
# File 'lib/tar/schema.rb', line 32

def string(name, size)
  add_field name, String, size
end

#timestamp(name, size) ⇒ Object



40
41
42
# File 'lib/tar/schema.rb', line 40

def timestamp(name, size)
  add_field name, Timestamp, size
end