class Depix::Binary::Fields::CharField

null-terminated string field with fixed padding

Public Class Methods

new(opts = {}) click to toggle source
Calls superclass method Depix::Binary::Fields::Field::new
# File lib/depix/binary/fields.rb, line 207
def initialize(opts = {})
  super({:length => 1}.merge(opts))
end

Public Instance Methods

clean(v) click to toggle source
# File lib/depix/binary/fields.rb, line 215
def clean(v)
  # Use the pack->unpack trick to remove null-termination
  v = pack(v.to_s).unpack(pattern)[0]
  # Blanked fields are 0xFF all the way
  blanking?(v) ? nil : v
end
pack(value) click to toggle source
# File lib/depix/binary/fields.rb, line 231
def pack(value)
  unless blanking?(value)
    [value].pack(pattern)
  else
    0xFF.chr * length
  end
end
pattern() click to toggle source
# File lib/depix/binary/fields.rb, line 211
def pattern
  "Z#{length}"
end
rtype() click to toggle source
# File lib/depix/binary/fields.rb, line 222
def rtype
  String
end
validate!(value) click to toggle source
Calls superclass method Depix::Binary::Fields::Field#validate!
# File lib/depix/binary/fields.rb, line 226
def validate!(value)
  super(value)
  raise "#{value} overflows the #{length} bytes allocated" if !value.nil? && value.length > length
end