Class: Arpie::PackBinaryType
- Inherits:
-
BinaryType
- Object
- BinaryType
- Arpie::PackBinaryType
- Defined in:
- lib/arpie/binary/pack_type.rb
Constant Summary
Constants included from Arpie
Instance Attribute Summary collapse
-
#pack_string ⇒ Object
readonly
Returns the value of attribute pack_string.
Class Method Summary collapse
Instance Method Summary collapse
- #binary_size(opts) ⇒ Object
- #from(binary, opts) ⇒ Object
-
#initialize(pack_string, force_opts = {}) ⇒ PackBinaryType
constructor
A new instance of PackBinaryType.
- #to(object, opts) ⇒ Object
Methods inherited from BinaryType
Methods included from Arpie
Constructor Details
#initialize(pack_string, force_opts = {}) ⇒ PackBinaryType
Returns a new instance of PackBinaryType.
43 44 45 46 |
# File 'lib/arpie/binary/pack_type.rb', line 43 def initialize pack_string, force_opts = {} @pack_string = pack_string @force_opts = force_opts end |
Instance Attribute Details
#pack_string ⇒ Object (readonly)
Returns the value of attribute pack_string.
3 4 5 |
# File 'lib/arpie/binary/pack_type.rb', line 3 def pack_string @pack_string end |
Class Method Details
.length_of(format) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/arpie/binary/pack_type.rb', line 10 def self.length_of format length = 0 format.scan(/(\S_?)\s*(\d*)/).each do |directive, count| count = count.to_i count = 1 if count == 0 length += case directive when 'A', 'a', 'C', 'c', 'Z', 'x' count when 'B', 'b' (count / 8.0).ceil when 'D', 'd', 'E', 'G' count * 8 when 'e', 'F', 'f', 'g' count * 4 when 'H', 'h' (count / 2.0).ceil when 'I', 'i', 'L', 'l', 'N', 'V' count * 4 when 'n', 'S', 's', 'v' count * 2 when 'Q', 'q' count * 8 when 'X' count * -1 else raise ArgumentError, "#{directive} is not supported" end end length end |
Instance Method Details
#binary_size(opts) ⇒ Object
5 6 7 8 |
# File 'lib/arpie/binary/pack_type.rb', line 5 def binary_size opts opts = @force_opts.merge(opts || {}) PackBinaryType.length_of(@pack_string + (opts[:length] || 1).to_s) end |
#from(binary, opts) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/arpie/binary/pack_type.rb', line 48 def from binary, opts opts = @force_opts.merge(opts || {}) binary && binary.size >= binary_size(opts) or incomplete! len = opts[:length] || 1 pack_string = @pack_string + len.to_s value = binary.unpack(pack_string)[0] value += opts[:mod] if opts[:mod] check_limit value, opts[:limit] [value, binary_size(opts)] end |
#to(object, opts) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/arpie/binary/pack_type.rb', line 60 def to object, opts opts = @force_opts.merge(opts || {}) object.nil? and bogon! nil,"nil object given" object -= opts[:mod] if opts[:mod] len = opts[:length] || 1 pack_string = @pack_string + len.to_s [object].pack(pack_string) end |