Class: Depix::Binary::Fields::CharField
- Inherits:
-
Field
- Object
- Field
- Depix::Binary::Fields::CharField
show all
- Defined in:
- lib/depix/binary/fields.rb
Overview
null-terminated string field with fixed padding
Instance Attribute Summary
Attributes inherited from Field
#desc, #length, #name, #req
Instance Method Summary
collapse
Methods inherited from Field
#consume!, #explain
Constructor Details
#initialize(opts = {}) ⇒ CharField
Returns a new instance of CharField.
207
208
209
|
# File 'lib/depix/binary/fields.rb', line 207
def initialize(opts = {})
super({:length => 1}.merge(opts))
end
|
Instance Method Details
#clean(v) ⇒ Object
215
216
217
218
219
220
|
# File 'lib/depix/binary/fields.rb', line 215
def clean(v)
v = pack(v.to_s).unpack(pattern)[0]
blanking?(v) ? nil : v
end
|
#pack(value) ⇒ Object
231
232
233
234
235
236
237
|
# File 'lib/depix/binary/fields.rb', line 231
def pack(value)
unless blanking?(value)
[value].pack(pattern)
else
0xFF.chr * length
end
end
|
#pattern ⇒ Object
211
212
213
|
# File 'lib/depix/binary/fields.rb', line 211
def pattern
"Z#{length}"
end
|
#rtype ⇒ Object
222
223
224
|
# File 'lib/depix/binary/fields.rb', line 222
def rtype
String
end
|
#validate!(value) ⇒ Object
226
227
228
229
|
# 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
|