class PacketGen::Types::Array
@abstract Base class to define set of {Fields} subclasses.
record_from_hash
¶ ↑
Subclasses should define private method #record_from_hash
. This method is called by {#push} to add an object to the set.
A default method is defined by {Array}: it calls constructor of class defined by {.set_of}.
real_type
¶ ↑
Subclasses should define private method #real_type
if {.set_of} type may be subclassed. This method should return real class to use. It takes an only argument, which is of type given by {.set_of}.
Default behaviour of this method is to return argument's class.
@author Sylvain Daubert
Constants
- HUMAN_SEPARATOR
Separator used in {#to_human}. May be ovverriden by subclasses
Public Class Methods
@param [Hash] options @option options [Int] counter Int
object used as a counter for this set
# File lib/packetgen/types/array.rb, line 84 def initialize(options={}) @counter = options[:counter] @array = [] initialize_length_from(options) end
Define type of objects in set. Used by {#read} and {#push}. @param [Class] klass @return [void]
# File lib/packetgen/types/array.rb, line 76 def set_of(klass) @klass = klass end
Get class set with {.set_of}. @return [Class] @since 3.0.0
# File lib/packetgen/types/array.rb, line 69 def set_of_klass @klass end
Public Instance Methods
@abstract depend on private method #record_from_hash
which should be
declared by subclasses.
Add an object to this array, and increment associated counter, if any @param [Object] obj type depends on subclass @return [Array] self
# File lib/packetgen/types/array.rb, line 151 def <<(obj) push obj @counter&.read(@counter.to_i + 1) self end
# File lib/packetgen/types/array.rb, line 96 def ==(other) @array == case other when Array other.to_a else other end end
Clear array. Reset associated counter, if any. @return [void]
# File lib/packetgen/types/array.rb, line 107 def clear! @array.clear @counter&.read(0) end
Delete an object from this array. Update associated counter if any @param [Object] obj @return [Object] deleted object
# File lib/packetgen/types/array.rb, line 115 def delete(obj) deleted = @array.delete(obj) @counter.read(@counter.to_i - 1) if @counter && deleted deleted end
Delete element at index
. @param [Integer] index @return [Object,nil] deleted object
# File lib/packetgen/types/array.rb, line 124 def delete_at(index) deleted = @array.delete_at(index) @counter.read(@counter.to_i - 1) if @counter && deleted deleted end
Initialize array for copy:
-
duplicate internal array.
# File lib/packetgen/types/array.rb, line 92 def initialize_copy(_other) @array = @array.dup end
@abstract depend on private method #record_from_hash
which should be
declared by subclasses.
Add an object to this array @param [Object] obj type depends on subclass @return [Array] self
# File lib/packetgen/types/array.rb, line 135 def push(obj) obj = case obj when Hash record_from_hash obj else obj end @array << obj self end
Populate object from a string @param [String] str @return [self]
# File lib/packetgen/types/array.rb, line 160 def read(str) clear return self if str.nil? return self if @counter&.to_i&.zero? str = read_with_length_from(str) until str.empty? obj = create_object_from_str(str) @array << obj str.slice!(0, obj.sz) break if @counter && self.size == @counter.to_i end self end
Get size in bytes @return [Integer]
# File lib/packetgen/types/array.rb, line 177 def sz to_s.size end
Return an Array
@return [::Array]
# File lib/packetgen/types/array.rb, line 183 def to_a @array end
Get a human readable string @return [String]
# File lib/packetgen/types/array.rb, line 195 def to_human @array.map(&:to_human).join(self.class::HUMAN_SEPARATOR) end
Get binary string @return [String]
# File lib/packetgen/types/array.rb, line 189 def to_s @array.map(&:to_s).join end
Private Instance Methods
# File lib/packetgen/types/array.rb, line 214 def create_object_from_str(str) klass = self.class.set_of_klass obj = klass.new.read(str) real_klass = real_type(obj) if real_klass == klass obj else real_klass.new.read(str) end end
# File lib/packetgen/types/array.rb, line 210 def real_type(obj) obj.class end
# File lib/packetgen/types/array.rb, line 201 def record_from_hash(hsh) obj_klass = self.class.set_of_klass raise NotImplementedError, 'class should define #record_from_hash or declare type of elements in set with .set_of' unless obj_klass obj = obj_klass.new(hsh) if obj_klass klass = real_type(obj) klass == obj_klass ? obj : klass.new(hsh) end