Class: PacketGen::Types::Array Abstract

Inherits:
Array
  • Object
show all
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.

Author:

  • Sylvain Daubert

Constant Summary collapse

HUMAN_SEPARATOR =

Separator used in #to_human. May be ovverriden by subclasses

','

Class Method Summary collapse

Instance Method Summary collapse

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



32
33
34
35
# File 'lib/packetgen/types/array.rb', line 32

def initialize(options={})
  super()
  @counter = options[:counter]
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)


26
27
28
# File 'lib/packetgen/types/array.rb', line 26

def self.set_of(klass)
  @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:



73
74
75
76
77
# File 'lib/packetgen/types/array.rb', line 73

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

#delete(obj) ⇒ Object

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

Parameters:

  • obj (Object)

Returns:

  • (Object)

    deleted object



82
83
84
85
86
# File 'lib/packetgen/types/array.rb', line 82

def delete(obj)
  deleted = super
  @counter.read(@counter.to_i - 1) if @counter && deleted
  deleted
end

#force_binary(str) ⇒ String

Force binary encoding for str

Parameters:

Returns:

  • (String)

    binary encoded string



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

def force_binary(str)
  PacketGen.force_binary str
end

#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:



58
59
60
61
62
63
64
65
66
# File 'lib/packetgen/types/array.rb', line 58

def push(obj)
  obj = case obj
        when Hash
          record_from_hash obj
        else
          obj
        end
  super(obj)
end

#read(str) ⇒ self

Populate object from a string

Parameters:

Returns:

  • (self)


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/packetgen/types/array.rb', line 40

def read(str)
  clear
  return self if str.nil?
  force_binary str
  klass = self.class.class_eval { @klass }
  while str.length > 0
    obj = klass.new.read(str)
    self.push obj
    str.slice!(0, obj.sz)
  end
  self
end

#szInteger

Get size in bytes

Returns:

  • (Integer)


102
103
104
# File 'lib/packetgen/types/array.rb', line 102

def sz
  to_s.size
end

#to_humanString

Get a human readable string

Returns:



96
97
98
# File 'lib/packetgen/types/array.rb', line 96

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

#to_sString

Get binary string

Returns:



90
91
92
# File 'lib/packetgen/types/array.rb', line 90

def to_s
  map(&:to_s).join
end