Module: PacketGen::Types::LengthFrom
Overview
This module is a mixin adding length_from
capacity to a type. length_from
capacity is the capacity, for a type, to gets its length from another object.
Constant Summary collapse
- MAX_SZ_TO_READ =
Max value returned by #sz_to_read.
65_535
Instance Method Summary collapse
-
#initialize_length_from(options) ⇒ void
Initialize length from capacity.
-
#read_with_length_from(str) ⇒ String
Return a substring from
str
of length given in another object. -
#sz_to_read ⇒ Integer
Size to read, from length_from.
Instance Method Details
#initialize_length_from(options) ⇒ void
This method returns an undefined value.
Initialize length from capacity. Should be call by extensed object’s initialize.
27 28 29 |
# File 'lib/packetgen/types/length_from.rb', line 27 def initialize_length_from() @length_from = [:length_from] end |
#read_with_length_from(str) ⇒ String
Return a substring from str
of length given in another object.
34 35 36 37 |
# File 'lib/packetgen/types/length_from.rb', line 34 def read_with_length_from(str) s = PacketGen.force_binary(str.to_s) s[0, sz_to_read] end |
#sz_to_read ⇒ Integer
Size to read, from length_from
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/packetgen/types/length_from.rb', line 41 def sz_to_read len = case @length_from when Types::Int @length_from.to_i when Proc @length_from.call else MAX_SZ_TO_READ end [0, len].max end |