class Tar::Schema

Public Class Methods

new(&block) click to toggle source
# 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

Public Instance Methods

clear(record, field_name) click to toggle source
# 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_names() click to toggle source
# File lib/tar/schema.rb, line 14
def field_names
  @fields.keys
end
octal_number(name, size) click to toggle source
# File lib/tar/schema.rb, line 36
def octal_number(name, size)
  add_field name, OctalNumber, size
end
parse(record) click to toggle source
# 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) click to toggle source
# File lib/tar/schema.rb, line 32
def string(name, size)
  add_field name, String, size
end
timestamp(name, size) click to toggle source
# File lib/tar/schema.rb, line 40
def timestamp(name, size)
  add_field name, Timestamp, size
end

Private Instance Methods

add_field(name, type, size) click to toggle source
# File lib/tar/schema.rb, line 46
def add_field(name, type, size)
  @fields[name] = type.new(size, @offset)
  @offset += size
end