Class: Depix::CharField

Inherits:
Field
  • Object
show all
Defined in:
lib/depix/dict.rb

Constant Summary collapse

BLANK =
"\0"
BLANKING_VALUES =
[0x00.chr, 0xFF.chr]
BLANKING_PATTERNS =
BLANKING_VALUES.inject([]) do | p, char |
  p << /^([#{char}]+)/ << /([#{char}]+)$/mu
end

Instance Attribute Summary

Attributes inherited from Field

#desc, #length, #name, #req

Instance Method Summary collapse

Methods inherited from Field

#consume!, emit_char, emit_r32, emit_u16, emit_u32, emit_u8, #explain, #initialize

Constructor Details

This class inherits a constructor from Depix::Field

Instance Method Details

#clean(v) ⇒ Object



231
232
233
234
235
236
237
238
# File 'lib/depix/dict.rb', line 231

def clean(v)
  if v == BLANK
    nil
  else
    2.times { BLANKING_PATTERNS.each{|p| v.gsub!(p, '')} }
    v.empty? ? nil : v
  end
end

#pack(value) ⇒ Object



249
250
251
# File 'lib/depix/dict.rb', line 249

def pack(value)
  value.ljust(length, "\000") rescue ("\000" * length)
end

#patternObject



227
228
229
# File 'lib/depix/dict.rb', line 227

def pattern
  "A#{(length || 1).to_i}"
end

#rtypeObject



240
241
242
# File 'lib/depix/dict.rb', line 240

def rtype
  String
end

#validate!(value) ⇒ Object



244
245
246
247
# File 'lib/depix/dict.rb', line 244

def validate!(value)
  super(value)
  raise "#{value} overflows the #{length} bytes allocated" if !value.nil? && value.length > length
end