Class: Depix::Binary::Fields::ArrayField
- Inherits:
-
Field
- Object
- Field
- Depix::Binary::Fields::ArrayField
show all
- Defined in:
- lib/depix/binary/fields.rb
Overview
Wrapper for an array structure
Instance Attribute Summary collapse
Attributes inherited from Field
#desc, #name, #req
Instance Method Summary
collapse
Methods inherited from Field
#clean, #initialize
Instance Attribute Details
#members ⇒ Object
Returns the value of attribute members.
266
267
268
|
# File 'lib/depix/binary/fields.rb', line 266
def members
@members
end
|
Instance Method Details
#consume!(stack) ⇒ Object
277
278
279
|
# File 'lib/depix/binary/fields.rb', line 277
def consume!(stack)
members.map{|m| m.consume!(stack)}
end
|
#explain ⇒ Object
285
286
287
288
289
290
|
# File 'lib/depix/binary/fields.rb', line 285
def explain
return 'Empty array' if (!members || members.empty?)
tpl = "(Array of %d %s fields)" % [ members.length, members[0].rtype]
r = (req? ? "- required" : nil)
[tpl, desc, r].compact.join(' ')
end
|
#length ⇒ Object
269
270
271
|
# File 'lib/depix/binary/fields.rb', line 269
def length
members.inject(0){|_, s| _ + s.length }
end
|
#pack(values) ⇒ Object
300
301
302
303
304
305
306
307
308
309
310
311
|
# File 'lib/depix/binary/fields.rb', line 300
def pack(values)
members.zip(values).map do |m, v|
if !m.req? && v.nil?
raise "#{m} needs to provide length" unless m.length
"\377" * m.length
else
v.respond_to?(:pack) ? v.pack : m.pack(v)
end
end.join
end
|
#pattern ⇒ Object
273
274
275
|
# File 'lib/depix/binary/fields.rb', line 273
def pattern
members.inject(''){|_, s| _ + s.pattern }
end
|
#rtype ⇒ Object
281
282
283
|
# File 'lib/depix/binary/fields.rb', line 281
def rtype
Array
end
|
#validate!(array) ⇒ Object
292
293
294
295
296
297
298
|
# File 'lib/depix/binary/fields.rb', line 292
def validate!(array)
raise "This value would overflow, #{array.length} elements passed but only #{members.length} fit" unless array.length <= members.length
raise "This value is required, but the array is empty" if req? && array.empty?
array.zip(members).map do | v, m |
m.validate!(v) unless (v.nil? && !m.req?)
end
end
|