Class: PacketGen::Types::Array Abstract

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Fieldable, LengthFrom
Defined in:
lib/packetgen/types/array.rb

Overview

This class is 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 Array.set_of.

#real_type

Subclasses should define private method #real_type if Array.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 Array.set_of.

Default behaviour of this method is to return argument's class.

Author:

  • Sylvain Daubert

Constant Summary collapse

HUMAN_SEPARATOR =

Separator used in #to_human. May be ovverriden by subclasses

','

Constants included from LengthFrom

LengthFrom::MAX_SZ_TO_READ

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LengthFrom

#initialize_length_from, #read_with_length_from, #sz_to_read

Methods included from Fieldable

#format_inspect, #type_name

Constructor Details

#initialize(options = {}) ⇒ Array

Returns a new instance of Array.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • counter (Int)

    Int object used as a counter for this set


84
85
86
87
88
# File 'lib/packetgen/types/array.rb', line 84

def initialize(options={})
  @counter = options[:counter]
  @array = []
  initialize_length_from(options)
end

Class Method Details

.set_of(klass) ⇒ void

This method returns an undefined value.

Define type of objects in set. Used by #read and #push.

Parameters:

  • klass (Class)

76
77
78
# File 'lib/packetgen/types/array.rb', line 76

def set_of(klass)
  @klass = klass
end

.set_of_klassClass

Get class set with set_of.

Returns:

  • (Class)

Since:

  • 3.0.0


69
70
71
# File 'lib/packetgen/types/array.rb', line 69

def set_of_klass
  @klass
end

Instance Method Details

#<<(obj) ⇒ Array

This method is 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

Parameters:

  • obj (Object)

    type depends on subclass

Returns:


151
152
153
154
155
# File 'lib/packetgen/types/array.rb', line 151

def <<(obj)
  push obj
  @counter&.read(@counter.to_i + 1)
  self
end

#==(other) ⇒ Object


96
97
98
99
100
101
102
103
# File 'lib/packetgen/types/array.rb', line 96

def ==(other)
  @array == case other
            when Array
              other.to_a
            else
              other
            end
end

#[](index) ⇒ Object

Return the element at index.

Parameters:

  • index (integer)

Returns:

  • (Object)

57
# File 'lib/packetgen/types/array.rb', line 57

def_delegators :@array, :[], :clear, :each, :empty?, :first, :last, :size

#clearvoid

This method returns an undefined value.

Clear array.


57
# File 'lib/packetgen/types/array.rb', line 57

def_delegators :@array, :[], :clear, :each, :empty?, :first, :last, :size

#clear!void

This method returns an undefined value.

Clear array. Reset associated counter, if any.


107
108
109
110
# File 'lib/packetgen/types/array.rb', line 107

def clear!
  @array.clear
  @counter&.read(0)
end

#delete(obj) ⇒ Object

Delete an object from this array. Update associated counter if any

Parameters:

  • obj (Object)

Returns:

  • (Object)

    deleted object


115
116
117
118
119
# 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_at(index) ⇒ Object?

Delete element at index.

Parameters:

  • index (Integer)

Returns:

  • (Object, nil)

    deleted object


124
125
126
127
128
# 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

#eachArray

Calls the given block once for each element in self, passing that element as a parameter. Returns the array itself.

Returns:


57
# File 'lib/packetgen/types/array.rb', line 57

def_delegators :@array, :[], :clear, :each, :empty?, :first, :last, :size

#empty?Booelan

Return true if contains no element.

Returns:

  • (Booelan)

57
# File 'lib/packetgen/types/array.rb', line 57

def_delegators :@array, :[], :clear, :each, :empty?, :first, :last, :size

#firstObject

Return first element

Returns:

  • (Object)

57
# File 'lib/packetgen/types/array.rb', line 57

def_delegators :@array, :[], :clear, :each, :empty?, :first, :last, :size

#initialize_copy(_other) ⇒ Object

Initialize array for copy:

  • duplicate internal array.


92
93
94
# File 'lib/packetgen/types/array.rb', line 92

def initialize_copy(_other)
  @array = @array.dup
end

#lastObject

Return last element.

Returns:

  • (Object)

57
# File 'lib/packetgen/types/array.rb', line 57

def_delegators :@array, :[], :clear, :each, :empty?, :first, :last, :size

#push(obj) ⇒ Array

This method is abstract.

depend on private method #record_from_hash which should be declared by subclasses.

Add an object to this array

Parameters:

  • obj (Object)

    type depends on subclass

Returns:


135
136
137
138
139
140
141
142
143
144
# 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

#read(str) ⇒ self

Populate object from a string

Parameters:

Returns:

  • (self)

162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/packetgen/types/array.rb', line 162

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

#sizeInteger Also known as: length

Get number of element in array

Returns:

  • (Integer)

57
# File 'lib/packetgen/types/array.rb', line 57

def_delegators :@array, :[], :clear, :each, :empty?, :first, :last, :size

#szInteger

Get size in bytes

Returns:

  • (Integer)

180
181
182
# File 'lib/packetgen/types/array.rb', line 180

def sz
  to_s.size
end

#to_a::Array

Return an Array

Returns:

  • (::Array)

186
187
188
# File 'lib/packetgen/types/array.rb', line 186

def to_a
  @array
end

#to_humanString

Get a human readable string

Returns:


198
199
200
# File 'lib/packetgen/types/array.rb', line 198

def to_human
  @array.map(&:to_human).join(self.class::HUMAN_SEPARATOR)
end

#to_sString

Get binary string

Returns:


192
193
194
# File 'lib/packetgen/types/array.rb', line 192

def to_s
  @array.map(&:to_s).join
end