Class: PacketGen::Types::Array Abstract
- Inherits:
-
Array
- Object
- Array
- PacketGen::Types::Array
- Defined in:
- lib/packetgen/types/array.rb
Overview
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.
Direct Known Subclasses
Header::DNS::Name, Header::DNS::RRSection, Header::TCP::Options
Constant Summary collapse
- HUMAN_SEPARATOR =
Separator used in #to_human. May be ovverriden by subclasses
','
Class Method Summary collapse
-
.set_of(klass) ⇒ void
Define type of objects in set.
Instance Method Summary collapse
-
#<<(obj) ⇒ Array
abstract
Add an object to this array, and increment associated counter, if any.
-
#delete(obj) ⇒ Object
Delete an object from this array.
-
#force_binary(str) ⇒ String
Force binary encoding for
str. -
#initialize(options = {}) ⇒ Array
constructor
A new instance of Array.
-
#push(obj) ⇒ Array
abstract
Add an object to this array.
-
#read(str) ⇒ self
Populate object from a string.
-
#sz ⇒ Integer
Get size in bytes.
-
#to_human ⇒ String
Get a human readable string.
-
#to_s ⇒ String
Get binary string.
Constructor Details
#initialize(options = {}) ⇒ Array
Returns a new instance of Array.
32 33 34 35 |
# File 'lib/packetgen/types/array.rb', line 32 def initialize(={}) super() @counter = [:counter] end |
Class Method Details
Instance Method Details
#<<(obj) ⇒ Array
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
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
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
109 110 111 |
# File 'lib/packetgen/types/array.rb', line 109 def force_binary(str) PacketGen.force_binary str end |
#push(obj) ⇒ Array
depend on private method #record_from_hash which should be declared by subclasses.
Add an object to this array
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
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 |
#sz ⇒ Integer
Get size in bytes
102 103 104 |
# File 'lib/packetgen/types/array.rb', line 102 def sz to_s.size end |
#to_human ⇒ String
Get a human readable string
96 97 98 |
# File 'lib/packetgen/types/array.rb', line 96 def to_human map(&:to_human).join(self.class::HUMAN_SEPARATOR) end |
#to_s ⇒ String
Get binary string
90 91 92 |
# File 'lib/packetgen/types/array.rb', line 90 def to_s map(&:to_s).join end |